Simple C programming problem, variable stuck at initialised value

In summary, the programmer is new to C programming and is trying to understand an if statement. The if statement takes y as always 0, but if y is initialised to a different value it works.
  • #1
nobahar
497
2
Simple C programming problem, variable "stuck" at initialised value

Hello!

This is most likely an error in my code but I just can't see what it is! I am new to C programming and maybe I have just been staring at it for so long that I can't see the obvious error. I was playing around with a simple if statement; basically, the user enters three numbers assigned to the variables y, x and z and the programme identifies if x is equal to either y or z, or neither, and also prints on screen the values for y, x, and z, as follows:

Code:
#include <stdio.h>

//Declare and initialise variables
short x = 0, y = 0, z = 0;

main( void )
{

//User sets the variable values
printf("Enter a value for y: \n");
scanf("%d", &y);
printf("Enter a value for x: \n");
scanf("%d", &x);
printf("Enter a value for z: \n");
scanf("%d", &z);

//Compare x to y and z
if ((x==y) || (x==z))
{printf("x is equal to either y or z");}
else
{printf("x is not equal to either");}

//Print values of the three variables
printf("\ny,x,z are equal to %d,%d,%d", y, x, z);
}

The peculiar thing is, no matter what is entered for y, the value appears to remain 0, as the final printf shows when the programme is run; consequently, the if statement takes y as always 0, so the statement seems to "work", but only with y set to 0. However, if I initialiase y to a different value, such as 5, it will change and the programme works fine. Why is it that when y is set to 0 to begin with it remains 0, but if I initialise it to a different value it works?

I am using code blocks on a linux system if that is of any help.

Thanks in advance.
 
Technology news on Phys.org
  • #2
x, y and z are declared as short int, but you are using "%d" to read and write their values. For short int you should use "%hd".
 
  • #3
Ah, thanks DrGreg. I haven't covered that yet: the only examples I have come across in the book I am using have used int and double with %d and %f. It's strange how it still works for some values! That threw me.

Many thanks.
 

Related to Simple C programming problem, variable stuck at initialised value

What is the cause of a variable being stuck at its initialised value in a simple C programming problem?

The most common cause of a variable being stuck at its initialised value is when the value is not being updated or changed within the program's code. This can happen if the variable is not being properly referenced or if there is an error in the code that is preventing the variable from being updated.

How can I troubleshoot and fix a variable stuck at initialised value in my C program?

To troubleshoot and fix a variable stuck at initialised value, you should first check your code for any errors or issues that may be preventing the variable from being updated. You can also try using debugging tools or print statements to track the value of the variable and see where it may be getting stuck. Additionally, make sure that the variable is being properly referenced and assigned a new value in your code.

Can a variable be stuck at its initialised value due to memory issues in my C program?

Yes, it is possible for a variable to be stuck at its initialised value due to memory issues in a C program. This can happen if the variable is not being allocated enough memory or if there are memory leaks in the code that are preventing the variable's value from being updated.

Is it possible for a variable to be stuck at its initialised value even if my code appears to be correct?

Yes, it is possible for a variable to be stuck at its initialised value even if your code appears to be correct. This can happen if there are logical errors in your code that are preventing the variable from being updated, or if there are errors in other parts of the program that are affecting the variable's value. It is important to thoroughly check your code and use debugging tools to identify and fix any potential issues.

How can I prevent a variable from getting stuck at its initialised value in my C program?

To prevent a variable from getting stuck at its initialised value, make sure that it is being properly referenced and assigned a new value in your code. It is also important to thoroughly test your code and use debugging tools to identify and fix any potential issues. Additionally, make sure to properly allocate memory for the variable and avoid any potential memory leaks in your code.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
969
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
858
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
4K
Back
Top