#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3

    Newbie question(s), C programming


    Hi, i'm new to this forum and to C programming in general.
    a have a few questions, and i was hoping to get some help here. :)

    first of all - what is the difference between these to functions?
    Code:
    ch=getchar();
    scanf("%c", &ch)
    and second, i'm having a problem when i try to obtain input from the user with both of the functions above.
    here's the code segment:
    Code:
    printf("enter a letter:");
    letter=getchar();
    printf("ASCII decimel value is: %d\n",letter);
    the idea is to obtain a letter as input, and to print as output its decimal value.
    the problem is that the program just skips the input part (it's not stopping to get input from the user), and instead just printing out this:
    Code:
    enter a letter:ASCII decimal calue is: 10
    (same thing happens when i use scanf() function)
    the rest of the code is running properly, btw.

    any help would greatly appreciated!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Normally when you type in things to your program, you also press return.

    So for example,
    Code:
    printf("Enter number");
    scanf("%d", &num);
    Which you respond to by typing
    1234<return>

    The scanf functions are frugal - they take only what they need. So the %d consumes the 1234 and leaves the \n on the input stream for the next input call to deal with.

    Most of the time, this isn't an issue. All the scanf formats (except %c) automatically skip over leading whitespace (which includes newlines). For them, it isn't an issue.

    %c takes the next character, regardless of what kind of character it is. The same goes for getchar().

    If you want to get the next non-space character using %c, then use this
    Code:
    scanf(" %c", &ch);  //!! note the leading space
    A single space character in scanf formats triggers the "skip any kind of space, tab, newline" processing, and then feeds %c a single character (which isn't whitespace).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by salem
    All the scanf formats (except %c) automatically skip over leading whitespace (which includes newlines).
    I think you mean: (except "%c", "%[...]", and (for very different reasons) "%n").
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Originally Posted by salem
    Normally when you type in things to your program, you also press return.

    So for example,
    Code:
    printf("Enter number");
    scanf("%d", &num);
    Which you respond to by typing
    1234<return>

    The scanf functions are frugal - they take only what they need. So the %d consumes the 1234 and leaves the \n on the input stream for the next input call to deal with.

    Most of the time, this isn't an issue. All the scanf formats (except %c) automatically skip over leading whitespace (which includes newlines). For them, it isn't an issue.

    %c takes the next character, regardless of what kind of character it is. The same goes for getchar().

    If you want to get the next non-space character using %c, then use this
    Code:
    scanf(" %c", &ch);  //!! note the leading space
    A single space character in scanf formats triggers the "skip any kind of space, tab, newline" processing, and then feeds %c a single character (which isn't whitespace).
    :)
    thanks!
    worked like a charm...

IMN logo majestic logo threadwatch logo seochat tools logo