Playing With Characters

Sort by

recency

|

851 Discussions

|

  • + 1 comment
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() 
    {
        char ch;
        char s[24];
        char t[100];
        
        scanf("%c", &ch);
        scanf("%s", s);
        getchar();
        scanf("%[^\n]%*c", t);
        
        printf("%c\n", ch);
        printf("%s\n", s);
        printf("%s\n", t);
         
        return 0;
    }
    
  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() 
    {
        char ch;
        char s[24];
        char t[100];
        
        scanf("%c", &ch);
        scanf("%s", s);
        getchar();
        scanf("%[^\n]%*c", t);
        
        printf("%c\n", ch);
        printf("%s\n", s);
        printf("%s\n", t);
         
        return 0;
    }
    
  • + 0 comments

    include

    include

    include

    include

    int main() {
    char ch,s[20],sen[100];

    scanf("%c\n",&ch);
    scanf("%s\n",&s);
    scanf("%[^\n]%*c",&sen);
    
    printf("%c\n",ch);
    printf("%s\n",s);
    printf("%s\n",sen);
    
    
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
    

    }

  • + 0 comments

    In order to take a line as input, you can use scanf("%[^\n]%*c", s); where is defined as char s[MAX_LEN] where MAX_LEN is the maximum size of s. Here, [] is the scanset character. ^\n stands for taking input until a newline isn't encountered. Then, with this %*c, it reads the newline character and here, the used * indicates that this newline character is discarded. Note: The statement: scanf("%[^\n]%*c", s); will not work because the last statement will read a newline character,

    Can someone please explain these 2 lines to me? Don't they seem contradictory?

  • + 0 comments

    int main() { printf("C\nLanguage \nWelcome To C!!");

    return 0;
    

    }