Scanf and ordinary characters.

This is a discussion on Scanf and ordinary characters. within the C Programming forums, part of the General Programming Boards category; Hello to all. I have some particular questions about scanf and how its works. Assuming that we have the code ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    306

    Scanf and ordinary characters.

    Hello to all. I have some particular questions about scanf and how its works.

    Assuming that we have the code :

    Code:
     
    
    #include<stdio.h>
    int main(void)
    {
    char mychar;
     
    printf(" Give a character : ");
    scanf("%c" , &mychar);
     
    printf(" Your char is : %c" , mychar);
     
    return 0;
    }
    Assuming that we have the INPUT :

    Code:
     Give a character:     <- I pushed ENTER 
     Character is:         <- What is here?
    Well , ENTER character have been stored into mychar variable?

    On the other hand if I have

    Code:
     scanf(" %c" , &mychar);
    If I give ENTER ( '\n') + A (character) scanf will match ENTER with space ordinary into format string of scanf and then will match A with mychar variable (due to %c conversion specification ??)

    Thank you in Advance

    Mr. Lnx

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,664
    In the first case (with no leading whitespace in the format string) mychar will have the value '\n' (typically ASCII 10). If you print that out, it gives a newline.

    In the second case, the leading whitespace causes any whitespace to be discarded before placing a character in mychar. '\n' is one of the whitespace characters, so will be skipped. If there are multiple whitespace characters pending, all will be discarded, and scanf() will wait until a non-whitespace character is entered.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    306
    Well ,yes this explanation is great .

    I think white-space characters are equivalent with new line characters in scanf... is it right?

    Mr. Lnx

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,664
    No. A newline character is one of the characters that are categorised as whitespace. The reverse is not true. There are whitespace characters that are not newlines (such as the space character itself, tab characters, etc).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    306
    Yes. It was my fault sorry. I mean a new line character (one) is equivalent to a space... and I say that because if we put a space in format string to scanf next to conversion specification like this ->

    Code:
     scanf(" %c" , &mychar);
    either give '\n' or space as input and then a character is the same. Scanf will accept your input in both of cases. Ok?

    And last question... you said before that scanf discards white space characters.. (since we have included space in format string) I think that first scanf will match one or more white space characters with space in format string and then will discard them... is it right? I have read it in page 46 of KIng's book C programming a modern approach second edition.

    Thank you for your time.

    Mr. Lnx
    Last edited by Mr.Lnx; 03-04-2012 at 09:37 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Mr.Lnx View Post
    scanf discards white space characters
    That depends on how you use it. The only specifiers that will read a whitespace character are %c and %[], if the set in [] includes whitespace characters (eg., [ \t\n]). For other specifiers, leading whitespace is discarded until a non-whitespace value is found. If that fits the specifier, it is read. If not, it is left in the buffer and scanf() fails.

    " %c" is sort of a special case, in that it also discards leading whitespace, even thought those values could be read into a %c.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    306
    Quote Originally Posted by grumpy View Post
    No. A newline character is one of the characters that are categorised as whitespace. The reverse is not true. There are whitespace characters that are not newlines (such as the space character itself, tab characters, etc).
    hmmm but space and whitespace characters are the same thing. Aren't they?

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    306
    Well , Finally I understand it thank you very much guys.

    Scanf is a strong headache sometimes!!!

    Have a nice day.

    Mr. Lnx

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195
    @Mr.lnx for my codeblocks c compiler does gives undefined behaviour when %c specifier is used i think its best to try this %1s and it's going to be best for collecting characters or your " %1s" to skip whitespace characters before input.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    306
    Quote Originally Posted by Nyah Check View Post
    @Mr.lnx for my codeblocks c compiler does gives undefined behaviour when %c specifier is used i think its best to try this %1s and it's going to be best for collecting characters or your " %1s" to skip whitespace characters before input.
    Thank you. I only wanted to understand how scanf works with ordinary characters in its format string . I am reading the book of King simultaneously
    On page 46 of C programming a modern approach second edition it is said that scanf reads / matches white space characters from user's input (if input has some of them because it is not necessary) you may mean when you say that scanf discards these white space characters that they don't be printed .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-02-2011, 10:17 PM
  2. white space characters and scanf
    By bthomson900 in forum C Programming
    Replies: 4
    Last Post: 11-10-2010, 06:12 AM
  3. pthread_cleanup_push/pop for windows ( CE or ordinary )
    By mynickmynick in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 09:05 AM
  4. Masking Characters In Scanf
    By coolshyam in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 08:55 AM
  5. ordinary function
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2001, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21