Unpredictable output while working with numeric user input in C

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Unpredictable output while working with numeric user input in C

1,492 Views
net11
Contributor III

Hi,

 

I am new to the CodeWarrior IDE and trying to get familiar with the IDE by writing simple C programs.

The board I am using is MCF5234BCCKIT eval board and the version of CodeWarrior is the special freely downloadable version 7.1

 

I tried writing very simple code with scanf() and fgets() type of functions to get user input and do some arithmetic operations on them. The target is console internal RAM. The output of the program is usually large numbers which could possibly be garbage values or addresses. for example, if a value of 20 in entered in a scanf() and assigned to a varibale, and I try printing the variable value to the console with a printf() is shows up as something like 536935188. Also, within the code if the value id incremented by say, 1, I see the output as 1 added to this large value.

 

What am I doing wrong?  

Labels (1)
0 Kudos
4 Replies

410 Views
net11
Contributor III

Hi pgo,

 

Thanks for taking a look at my post... I will try to be more specific this time. The major problem I am facing is that I am not able to use printf() statements to correctly print the value of a variable or scanf() to get user input for the value. For example, in the code :

 

#include <stdio.h>

 

int main(void)

{

 

int correct_ans = 53;

int user_ans;

 

printf("Enter an integer between 1 and 100 as guess :");

scanf("%d", &user_ans);

printf("The number entered by the user is : %d", &user_ans);

 

if(user_ans == correct_ans)

{

 

printf("Correct!");

}

else

{

printf("Wrong!");

}

 

return 0;

 

}

 

The value of the variable user_ans shows some large value(536935188) at every step of the code execution in the debugger's "register values" pane. The same value is printed to the console when the printf() is executed. However, if this code is modified to hard-code the variable values like

 

#include <stdio.h>

 

int main(void)

{

 

int correct_ans = 53;

int user_ans = 53;

 

if(user_ans == correct_ans)

{

 

printf("Correct!");

}

else

{

printf("Wrong!");

}

 

 

return 0;

 

}

 

it works, and at every step, the variable values(53) shown are as expected. I suppose then, that there is a problem with the way printf() and scanf() are working ? If so, I am not sure how to read and write register values to the console correctly... 

 

0 Kudos

410 Views
CompilerGuru
NXP Employee
NXP Employee

Not sure if this is your issue, but the printf shown prints the address of the variable, not the value.

Should be

 

> printf("The number entered by the user is : %d", user_ans);

 

Also decimal 536935188 is in hex 0x2000FB14, looks like a regular address. Is the stack in this area?

 

Daniel

0 Kudos

410 Views
net11
Contributor III

Thanks Daniel, my bad for an error like that. That was not the issue here though, when I fixed and ran the code. Apparently not flushing the buffer between the printf and scanf statements caused the problems. I fixed it though.... 

Yes the stack is in this area btw.

 


CompilerGuru wrote:

Not sure if this is your issue, but the printf shown prints the address of the variable, not the value.

Should be

 

> printf("The number entered by the user is : %d", user_ans);

 

Also decimal 536935188 is in hex 0x2000FB14, looks like a regular address. Is the stack in this area?

 

Daniel


 

0 Kudos

410 Views
pgo
Senior Contributor V

Dear net11,

 

 Please post a simple example of the code you are having a problem with.  It's a bit hard to know if its a setup or code problem otherwise :smileyhappy:

 

bye

0 Kudos