Simple C programming problem, variable stuck at initialised value

Click For Summary
SUMMARY

The discussion centers on a C programming issue where the variable 'y' remains at its initialized value of 0, regardless of user input. The code provided uses the incorrect format specifier "%d" for reading and writing 'short' variables, which should be "%hd". This mismatch causes unexpected behavior in the program. The user learns that using the correct format specifier resolves the issue, allowing the program to function as intended.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with variable types, specifically 'short int'
  • Knowledge of input/output functions in C, such as 'printf' and 'scanf'
  • Comprehension of format specifiers in C
NEXT STEPS
  • Learn about C format specifiers, focusing on "%hd" for 'short int'
  • Explore debugging techniques in C to identify variable issues
  • Study variable scope and initialization in C programming
  • Practice writing and testing C programs using different data types
USEFUL FOR

Beginner C programmers, educators teaching C programming, and anyone troubleshooting variable initialization issues in C code.

nobahar
Messages
482
Reaction score
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
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".
 
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.
 

Similar threads

Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
47
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K