-1
#include<stdio.h>

int main()
{
    int i,t;
    char c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",&c);
        printf("%c",c);
    }
    return 0;
}

inside the while loop why it skips some of values. but it works okay with integer values

Rishi Sahu
  • 39
  • 1
  • 7
  • Possible duplicate of [C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf](http://stackoverflow.com/questions/9562218/c-multiple-scanfs-when-i-enter-in-a-value-for-one-scanf-it-skips-the-second-s) – Thomas Padron-McCarthy Jul 03 '16 at 17:40
  • @ThomasPadron-McCarthy it is not the same. this is UB. – BLUEPIXY Jul 03 '16 at 17:45
  • *"inside the while loop why it skips some of values"* What values does it skip? Is there some kind of pattern? Can you describe the problem in more detail? Perhaps by giving sample inputs and outputs? – Cody Gray - on strike Jul 03 '16 at 17:50
  • i guess it reads the new line character '\n' when enter is pressed. that is the problem.. – Rishi Sahu Jul 03 '16 at 17:57

2 Answers2

2

%s is used to scan strings (not char's), change to %c

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • actuly it is %c instead of %s. but still. using %c it skips a value. – Rishi Sahu Jul 03 '16 at 17:51
  • 2
    @RishiSahu as well as correcting the format spec, put a space in front, as `scanf(" %c",&c);` then it will skip whitespace. `%d` and `%s` do automatically, but `%c` does not, without that little space. – Weather Vane Jul 03 '16 at 18:01
1

this could be a solution. use:

scanf(" %c",&c);

instead of:

scanf("%c",&c);
Community
  • 1
  • 1
Matthias Gwiozda
  • 505
  • 5
  • 14