Thread: printing and scaning char variables

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    printing and scaning char variables

    hi guys..
    i m getting a problem in executing this particular code..
    plz help...

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
         clrscr();
         char gen,qua;
         int yos;
    
         printf("enter gender");
         scanf("%c",&gen);
    
         printf("enter yos");
         scanf("%d",&yos);
         
         printf("enter qualification");
         scanf("%c",&qua);
    
         printf("%c%d%c",gen,yos,qua);
    
         getch();
         return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    someone please verify my answer

    ok assume I enter like this

    Code:
    enter gender: M	
    enter yos: 23
    enter qua: x
    %c does NOT skip whitespaces or characters.

    you've typed this:
    [M][\n][23][\n][x][\n]
    line 1: scanf("%c",&gen) looks at the input buffer, sees the M. gen = M. Correct
    line 2: scanf("%d",&qua) looks at input buffer, sees the \n. It does not belong to %d, so it skips it and puts 'yos' = 23. correct
    line 3: scanf("%c",&yos) looks at input buffer. sees the \n (before x) and assumes that as %c

    last line printf is now executed
    M23
    newline...

    also, please view
    http://cboard.cprogramming.com/c-pro...-expected.html
    Last edited by bos1234; 09-18-2013 at 06:29 PM. Reason: please verify answer

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    this will solve your problem, however, not sure if its the best solution for future programs.

    try this:
    Code:
    #include<stdio.h>
    
     
    int main()
    {
        
         char gen,qua;
         int yos;
     
         printf("enter gender");
         scanf("%c",&gen);
     
         printf("enter yos");
         scanf("%d",&yos);
          
    	 getchar(); //this will soak the \n from the buffer
    
         printf("enter qualification");
         scanf("%c",&qua);
     
         printf("%c%d%c",gen,yos,qua);
     
         getchar();getchar();
         return 0;
    }
    If you have Modern C Programming by KING, page 42 has good treatment of scanf

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I wish people would forget that conio.h exists because essentially it doesn't. I see it in a large number of posts here #included just to make sure the program is broken from the start I guess.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    2
    @bos1234- yes the getchar function solves my problem.
    but sum1 told me to use "flushf or fflush" (not sure of the name) function...i dont know bout the function...if u could help with that function.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    fflush() is the name of the function.
    The function is for the output stream, not the input stream.

    If you want to eat the trailing input someone left, in order the next scanf, fgets or whatever input function you use, gets a fresh input, you should do something like this:
    Code:
    /* Clear trailing input */
        while(getchar() != '\n')
            /* discard */ ;
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a certain number of variables.
    By HaydeezPluto in forum C Programming
    Replies: 5
    Last Post: 10-31-2012, 12:35 PM
  2. scaning a word
    By kiros88 in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 08:10 PM
  3. printing variables
    By scwizzo in forum Game Programming
    Replies: 8
    Last Post: 04-07-2007, 04:33 AM
  4. passing variables into function and printing them
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-30-2003, 10:34 AM
  5. Printing Variables in Win32
    By Laguna in forum Windows Programming
    Replies: 2
    Last Post: 03-10-2003, 03:27 PM