-3

code

#include <stdio.h>
int main(void)
{
   int a=2;
   float b=9.99f;
   printf("1:%d\n",a,b);
   printf("2:%d、%f、%f\n",a);
   return 0;
}

compilation option

The GCC compilation option is GCC -m32 test.c -o test

The output

$./test

1:2 2:2、9.990000

doubt

In the second printf statement, there is only one expression, a, but it prints the values of two variables

stack data

enter image description here

d16ug-a1l
  • 1
  • 1
  • 1
    In this case "wrong" includes not providing the required variables. – kaylum Aug 05 '21 at 05:18
  • Why providing more than required number of variables to the format specifier of the printf function ? – Kavindu Vindika Aug 05 '21 at 05:20
  • For variadic functions, providing excess arguments is fine but not providing enough is bound to cause undefined behavior. – mediocrevegetable1 Aug 05 '21 at 05:24
  • 2
    Change compilation arguments to: `GCC -Werror -Wall -m32 test.c -o test`. Read the warnings, try to understand what they are telling you, and then fix your code accordingly. – Cheatah Aug 05 '21 at 05:30
  • 1
    I think this answer correctly clears your doubt https://stackoverflow.com/a/23104629/8504592 – PunyCode Aug 05 '21 at 06:09

1 Answers1

0

You are using incorrect format specifiers which results in compiler warnings. In the second printf statement format ‘%f’ expects a matching ‘double’ argument Here is the correct version:

#include <stdio.h>
int main(void)
{
   int a=2;
   float b=9.99f;
   printf("1:%d %f\n",a,b);
   printf("2:%d\n",a);
   return 0;
}
HiEd
  • 160
  • 3
  • I know the correct way to do it, but this is a problem finding process, I don't know how to implement printf, use GDB to debug, you can see 9.99 is also on the stack, %f is not pushed but still output – d16ug-a1l Aug 05 '21 at 05:40