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

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    1

    Char reading problem


    //my code

    char m[4];
    scanf("%c",&m[0]);
    scanf("%c",&m[1]);
    scanf("%c",&m[2]);
    scanf("%c",&m[3]);

    But when i execute using gcc compiler it only takes 1 value.
    Then the result is printed and program finishes.

    I tried fflush(stdin) and getchar but no effect.
    Why this happens and how can i fix it?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    its because after entering first char there is \n in your buffer.
    to solve this problem add white space before %c.

    Comments on this post

    • salem agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    1
    Thank you bro.
    I got it.

IMN logo majestic logo threadwatch logo seochat tools logo