A very simple basic query when programming in C is to display an int value

When I compile this small program instead of displaying " num1:7 , num2: 2 ", it displays " num1:-1218690218 , num2:-1217453276 ". I think that I do not specify what the program should display, so it just gives me an int range. Excuse me.

 #include <stdio.h> main() { int num1 = 7, num2 = 2; printf("num1:%d , num2:%d\n"), num1, num2; } 

EDIT: Thank you so much! The purpose of the exercise was to correct syntax errors, but whenever I compiled it, I never received any warnings. This bracket is easy to skip.

+4
source share
10 answers

You put closing parentheses in front of num1 and num2 , so they are not passed to printf . You need to change this:

  printf("num1:%d , num2:%d\n"), num1, num2; 

:

  printf("num1:%d , num2:%d\n", num1, num2); 

Yes, the bracket is the only change, but it is important.

+7
source

You use a comma operator instead of arguments to call a function. printf will output garbage values, but it can also be broken.


So this should be:

 printf("num1:%d , num2:%d\n", num1, num2); 

Pay attention to ) -character.

+5
source

You want to move num1 and num2 to parentheses:

 printf("num1:%d , num2:%d\n", num1, num2); 

The reason is that num1 and num2 are part of the printf function call - without them, printf uses random data from other sources, giving you these big negative values.

+4
source

Try the following:

 #include <stdio.h> main() { int num1 = 7, num2 = 2; printf("num1:%d , num2:%d\n", num1, num2); // ^ num1 and num2 go inside the parentheses } 
+4
source

If this is the actual code, then correct it by moving it.

 printf("num1:%d , num2:%d\n", num1, num2); 
+3
source

I think your program should look bigger

 int main(){ int num1 = 7, num2 = 2; printf("num1 : %d num2 : %d\n",num1,num2); } 

code>

+2
source

use a compiler that checks your syntax something like pellesc for windows

 #include <stdio.h> int main(){ int num1 = 7, num2 = 2; printf("num1:%d , num2:%d\n", num1, num2); return 0; } 

your printf format was wrong and the c editor would tell you

+1
source

What happens is that printf looks for numbers in memory adjacent to the memory in which the program (stack) is running. These numbers exist for some other reason, when printf just looks at them, so it prints them instead of num1 and num2 . As others have pointed out, your arguments ( num1 and num2 ) must be inside the brackets so printf can use them.

+1
source
 #include <stdio.h> int main() { //int num1 = 7, num2 = 2; this is static intialisation //u want it as dynamic u have to use int num1,num2; scanf("%d%d",&num1,&num2); //get the values from user printf("num1:%d , num2:%d\n", num1, num2); return 0; } 
+1
source

You would like to "call printf with this format string, using num1 as the first value to replace in and num2 as the second value to replace in; ignore the value that printf returns." ( printf usually returns the number of all bytes it printed, actual printing is a side effect.)

What you wrote was "call printf with this format string: ignore return value and evaluate num1 ; ignore this value and evaluate num2 ; ignore this value". This behavior is undefined: printf not provided with any replacement values ​​and will usually blindly propagate in memory to the values ​​it expects to receive (sometimes leading to a crash), but the language standard says your program is allowed to do literally everything at this stage. Yes, this is a very dangerous language to work with :)

To pass printf values, they must be in parentheses, as shown in other answers.

0
source

All Articles