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

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Basic C programming doubt with getchar


    Code:
    include <stdio.h>
    
    int main(void)
    {
         int i;
         printf("enter the no \n");
         scanf("%d",&i);
         int c = getchar();
         return 0;
    }
    Hey, My question is why getchar is taking value by itself instead if asking it from user.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Did you investigate what ch might be set to?

    Code:
         int c = getchar();
         printf("Final ch=%d(%c)\n", ch, ch );
    My guess is you'll see the decimal and visual representation of a newline.
    Final ch=10(
    )
    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
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    re-doubt


    no it is showing $ sign in between brackets

    output looks :

    enter the no
    Final ch=36($)
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    I guess it depends on what you type.
    Code:
    $ ./a.out 
    enter the no 
    1234
    Final ch=10(
    )
    $ ./a.out 
    enter the no 
    $nothing
    Final ch=36($)
    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

IMN logo majestic logo threadwatch logo seochat tools logo