-2

This code here gives incorrect value of int num if my input for num is for example 11,the printf function will output 0. However if I add static to int num the output produced by printf is correct. Can someone please explain the reason.Also if I make the format specifier for second scanf as %c , then also int value is printed correctly.

#include<stdio.h>
    int main()
     {
      int num;//making it static gives correct result
      char ch;int c;
      printf("enter the value of num and ch:\n");
      scanf("%d",&num);    
      scanf("%d",&ch);
      printf("num = %d and ch = %c",num,ch);
      return 0;
    }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

4 Answers4

2

This

char ch;
scanf("%d", &ch);

will invoke Undefined Behavior, since you are using the format for an integer, to store it in a character.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
2

It's not specifically with printf(), the issue is caused by the erroenous call to scanf().

Quoting C11, chapter §7.21.6.2

[...] Unless assignment suppression was indicated by a *, the result of the conversion is placed in the object pointed to by the first argument following the format argument that has not already received a conversion result. If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.

For %d conversion specifier, the expected type of argument is

d [....] The corresponding argument shall be a pointer to signed integer.

But, all you're supplying is a pointer to a char. Mismatch. Undefined behavior.

OTOH, for %c conversion specifier,

c Matches a sequence of characters of exactly the number specified by the field width (1 if no field width is present in the directive). If no l length modifier is present, the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence. No null character is added.

so using

 scanf("%c",&ch);

is correct. Alternatively, you can also use

 scanf("%hhd",&ch);    //char is signed
 scanf("%hhu",&ch);   //char is unsigned
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    I didn't down vote this answer, but (as I see it) this answer is not correct. The real problem is the posted code does not cleanly compile. The OP should not have ask this question. Rather the OP should have compiled with warnings enable, fixed the warnings, and the 'problem' goes away. – user3629249 May 24 '17 at 15:32
  • 1
    @user3629249 so that is a problem with the question, that's not a reason to downvote a valid answer. Just my two cents. Though I know it all starts from missing keys.... – Sourav Ghosh May 24 '17 at 15:44
  • 1
    @SouravGhosh your answer is valid, do not mind for the downvote...It was probably the one we all the answers received here! :/ – gsamaras May 24 '17 at 21:21
  • @SouravGhosh, As I stated, I did not downvote this answer. However, this answer is not the aimed at the source of the problem. – user3629249 May 25 '17 at 15:05
  • @user3629249, why the problem being that it does not cleanly compile is enough reason to mark this question as _should not be asked_ here? Most probably the OP has not been able to interpret the compiler message, or does not fully understand the peculiarities of `scanf()`. Don't be so strict. – Luis Colorado May 26 '17 at 06:00
  • When a program does not cleanly compile, the results could be anything. In this case, the observed problems are from the program NOT cleanly compiling – user3629249 May 27 '17 at 17:32
1

What you observe is very likely because second scanf with the wrong (tool large for char) format specifier overwrites the automatic variables memory where num is located.

Making num static moves it to the global variables memory and it (kind of) works, but it's still undefined behaviour, some memory have been overwritten somewhere and you may pay the price later on. So the only option is to specify the correct format specifier, here %c as you noted.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

Try to add \n, like scanf("%d\n", &num);. Maybe it could help. And compiler cannot pass if you use that expression, like scanf("%d", &ch);

Andreas
  • 2,455
  • 10
  • 21
  • 24