0

My code is...

#include <stdio.h>
int main(){
    char name[20];
    int age;
    char department;
    double key;
    int i=0;
    while( (scanf("%c", &name[i])) == 1 ){
        i++;
    }
    name[i] = '\0';

    scanf("%d", &age);
    scanf("%c",&department);
    scanf("%lf",&key);


    puts(name);
    printf ("%d\n",age);
    printf("%c\n",department);
    printf("%g",key);

    return 0;
}

and input value is

mark
20
A
3.154

The result output should be same as input

mark
20
A
3.154

but what i got is ...

mark
20
A
3.154

32766
�

I wonder where

32766
�

comes from...

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
BoHyunK
  • 59
  • 6
  • 2
    Your first loop with `while (scanf("%c", &name[i]) == 1)` should read all of standard input, quite probably overflowing the `name` array. The `scanf()` calls afterwards should fail — you should be checking the return value from each of those. That you get the input reprinted is because you first print `name`; the rest then is from the uninitialized (or overwritten) variables. It might be easier to see if you used `printf("[[%s]]\n", name);` instead of `puts(name);` — you'd see the square brackets around all your input. – Jonathan Leffler Feb 07 '19 at 01:24
  • this is the key to your problem: `while( (scanf("%c", &name[i])) == 1 ){ i++; }` Suggest: `scanf( "%*s", sizeof(name)-1, name);` the =1 because '%s' and '%[...]' always append a NUL byte to the input. This avoids any chance of a buffer overflow and the resulting undefined behavior – user3629249 Feb 07 '19 at 19:20
  • regarding: `scanf("%d", &age); scanf("%c",&department);` The input of `age` will leave the trailing newline in `stdin`. however, the input format specifier `%c` does not consume leading 'white space' (like the newline) so `department` will contain something like '\n'. then the input of `key` will fail. Suggest: `scanf("%d", &age); scanf(" %c",&department);` Notice the space before `%c` That space will consume any leading white space. The `scanf()` family of functions has a lot of details. Suggest reading the MAN page until you fully understand those details – user3629249 Feb 07 '19 at 19:27

2 Answers2

2

The code

while( (scanf("%c", &name[i])) == 1 ){
    i++;
}

will read all characters that it can, it will not stop at the end of mark.

Hence, the entire four lines are being read by that loop into name, then the scan calls following that are failing because there's no more input, meaning that the age, department and key values are left at whatever arbitrary value they had when they were created.

In other words, your output can be explained thusly:

mark  \
20     \
A       >-- name
3.154  /
      /
32766    -- age (and probably nul for department)
�       -- key

If you want to do line-based input, this answer provides a handy function for doing so, with buffer overflow protection and so on. For the strings, you just use them as entered, while the non-strings can be converted using sscanf on the buffer.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

Your first loop with while (scanf("%c", &name[i]) == 1) should read all of standard input, quite probably overflowing the name array. The scanf() calls afterwards should fail — you should be checking the return value from each of those. That you get the input reprinted is because you first print name; the rest then is from the uninitialized (or overwritten) variables. It might be easier to see if you used printf("[[%s]]\n", name); instead of puts(name); — you'd see the square brackets around all your input.

You could fix it by adding:

if (name[i] == '\n')
    break;

inside that initial while loop.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278