Very very basic query on programming in C -- displaying int value
When I compile this little program instead of displaying "num1:7
, num2: 2
",
it displays "num1:-1218690218
, num2:-1217453276
". I think I'm not spec开发者_Python百科ifying what the program should display so its just giving me the int range instead.
I'm sorry.
#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 got any warnings. That parenthesis is so easy to miss.
You've put the closing parenthesis before num1
and num2
, so they're not being passed to printf
. You need to change this:
printf("num1:%d , num2:%d\n"), num1, num2;
to this:
printf("num1:%d , num2:%d\n", num1, num2);
Yes, the parenthesis is the only change, but it's crucial.
You are using the comma operator instead of arguments to a function call. printf
will output garbage values but it could have crashed as well.
So it should be:
printf("num1:%d , num2:%d\n", num1, num2);
Notice the )
-character.
You want to move num1
and num2
inside your parentheses:
printf("num1:%d , num2:%d\n", num1, num2);
The reason is that num1
and num2
are part of the call to the printf
function - without them, printf
uses random data from elsewhere, giving you those large negative values.
Try this:
#include <stdio.h>
main() {
int num1 = 7, num2 = 2;
printf("num1:%d , num2:%d\n", num1, num2);
// ^ num1 and num2 go inside the parentheses
}
If that is the actual code then fix it by moving the paren.
printf("num1:%d , num2:%d\n", num1, num2);
I think your program should look more like this
int main(){
int num1 = 7, num2 = 2;
printf("num1 : %d num2 : %d\n",num1,num2);
}
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 something a c editor would have told you
What is happening is that printf
is looking at numbers in memory adjacent to the memory the program is working in (the stack). These numbers are there for some other reason when printf
just happens to look at them, so it prints them instead of num1
and num2
. As others have pointed out, your arguments (num1
and num2
) need to be inside of the parentheses so that printf
can use them.
#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;
}
What you wanted was "call printf
with this format string, using num1
as the first value to substitute in and num2
as the second value to substitute in; ignore the value that printf
returns". (printf
normally returns a count of how many bytes it printed; the actual printing is a side effect.)
What you wrote was "call printf
with this format string; ignore the value that it returns, and evaluate num1
; ignore that value and evaluate num2
; ignore that value". This is undefined behaviour: printf
hasn't been given any values to substitute in, and will normally blindly reach around in memory for the values that it's expecting to receive (sometimes resulting in a crash) - but the language standard says your program is allowed to do literally anything at this point. Yes, this is a very dangerous language to be working with :)
To pass the values to printf
, they need to be within the parentheses, as illustrated in other answers.
精彩评论