jrasa
jrasa

Reputation: 45

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 specifying 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.

Upvotes: 4

Views: 3274

Answers (10)

jp165
jp165

Reputation: 35

#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; 
   } 

Upvotes: 1

Karl Knechtel
Karl Knechtel

Reputation: 61519

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.

Upvotes: 0

murgatroid99
murgatroid99

Reputation: 20277

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.

Upvotes: 1

normeus
normeus

Reputation: 111

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

Upvotes: 1

user142019
user142019

Reputation:

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.

Upvotes: 5

gibraltar
gibraltar

Reputation: 1708

I think your program should look more like this

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

Upvotes: 2

JeremyP
JeremyP

Reputation: 86651

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
}

Upvotes: 4

EricLeaf
EricLeaf

Reputation: 902

If that is the actual code then fix it by moving the paren.

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

Upvotes: 3

Jerry Coffin
Jerry Coffin

Reputation: 490108

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.

Upvotes: 7

Tim
Tim

Reputation: 60110

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.

Upvotes: 4

Related Questions