0

My program contains input code for character , but during debugging its not considering it.

It considers input for other datatypes(int,float,etc)

program:

               #include<stdio.h>
               int main()
                 {
                    int n,i=0;
                    char c;                               
                    scanf("%d",&n);
                    int a[20];
                    while(1)
                        {
                           scanf("%c",&c);
                           if(c=='\n')
                           break;
                           else
                             {
                                if(c!=32)
                                a[i++]=c-48;
                             }

                        }
                   for(i=0;i<10;i++)
                   printf("%d ",a[i]);

                    return 0;
               }

debugging screen: enter image description here

  • 1
    I suggest `scanf("%c",&c);` should be `scanf(" %c",&c);` with an added space to clear off leading whitespace. Please see [scanf() leaves the newline char in buffer?](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer) Most format specifiers like `%d` and `%f` filter out that leading whitespace but `%c` does not, unless you instruct it with the space. – Weather Vane May 15 '18 at 19:52
  • Aside: `c-48` suggests there is ASCII coding and you want to extract a digit, in which case `c - '0'` is both clear and portable. – Weather Vane May 15 '18 at 19:53
  • @user3121023 yes but that might mess with several characters entered on one line, maybe with more than one space separating. The `" %c"` is clean and reliable. – Weather Vane May 15 '18 at 19:59
  • @user3121023 I don't understand your concept of "breaking on a newline". Data for `scanf` can be entered all on one line. I suggest using `fgets`, as ever, if you want an emtpy line to stop the input. – Weather Vane May 15 '18 at 20:07
  • 1
    @user3121023 I see, the *first* `%c` entry will pick up the newline after the `%d` entry before the loop. – Weather Vane May 15 '18 at 20:10
  • @WeatherVane whats difference between fgets and gets? –  May 19 '18 at 15:27
  • `gets` is obsolete, no longer part of C. – Weather Vane May 19 '18 at 17:55
  • why im not able to ask further questions, they ban me . what should I do to remove my ban? plz upvote my question. –  May 20 '18 at 12:43

1 Answers1

1

Your scanf("%d",...) leaves a new line character in the buffer, which is then immediately consumed by the subsequent scanf("%c",...). To overcome this, let only one scanf after the scanf("%d",...) consume white spaces:

int main()
{
    int n,i=0;
    scanf("%d",&n);

    int a[20];
    char c=0;
    scanf(" %c",&c);  // Consume white spaces including new line character before the value for c.
    while(c!='\n' && i < 20)
    {
        if(c!=32) {
            a[i++]=c-'0';
        }
        scanf("%c",&c);
    }
    for(int x=0;x<i;x++)
        printf("%d ",a[x]);

    return 0;
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58