I created a very simple progam whith a menu,
that take a value, then memorize it into the
local variable value, and finally with the
second option the progam prints the value.
my question is:
Why does the program work only if I add an "h"
to the scanf parameter?
In other words: what kind of relation there is
between scanf() and my local int value variable?
thanks!
p.S. (I used Dev-C++ (GCC) to compile it.
With Visual Studio it works)
#include <stdio.h>
main () {
int value = 0;
short choice = 0;
do {
printf("\nYour Choice ---> ");
scanf("%d", &choice); /* replace with "%hd" and it works */
switch (choice) {
case 1:
printf("\nEnter a volue to store ");
scanf("%d", &value);
getchar();
printf("\nValue: %d", value);
break;
case 2:
printf("\nValue: %d", value);
break;
}
} while (choice < 3);
getchar();
}
With scanf, the "h" modifier indicates that it's reading a short integer, which your variable choice just happens to be. So the "%hd" is necessary to write only two bytes (on most machines) instead of the 4 bytes that "%d" writes.
For more info, see this reference page on scanf
The variable choice is of type short so that's why you need the %h specifier in scanf to read into it (in fact you don't need the d here). The int type just requires %d. See the notes on conversions here
You're reading into a short. The h is necessary because %d is the size of an int by default. See this reference page on scanf.
It looks like your problem is that choice is a short, which is (generally) 2 bytes long, while %d expects an integer, which is (generally) 4 bytes long… So the scanf clobbers whatever comes after choice on the stack.
choice is a short and %d specifies an int.
When you specify %d, scanf has to assume that the associated argument is a pointer to an int sized block of memory, and will write an int to it. When that happens it will likely be writing to data adjacent to but not part of choice and the results are undefined and probably not good! If it works in one compiler and not another that is simply the nature of undefined behaviour!
In GCC -Wformat should give you a warning when you make this error.
From the comp.lang.c FAQ:
Why doesn't the code short int s; scanf("%d", &s); work?
Someone told me it was wrong to use %lf with printf. How can printf use %f for type double, if scanf requires %lf?
%d is for reading an int, not a short. Your code never really "worked" -- it just appears that in this case you didn't notice any difference between what you wanted and the undefined behavior you got.
The modifier for scanf to input a variable of type short is %hd. Hence you need to specify the correct modifier.
scanf("%d",&integer); // For integer type
scanf("%hd",&short_int); // For short type
Hence it doesnt work.
Depending upon numeric padding, endian-ness, and other such issues, you may be storing either the upper or lower part of the input value into choice; you are storing the rest of the input value into memory that may or may not be being used for anything else.
Related
I apologies in advance if there is a stupid error in this code, but I can't seem to trouble shoot it. My problem is this, I compile with GCC-8 (installed on Mac via home-brew), then execute in the terminal. When using int do define variables s & a, I get zeros as output using the print statements below. If I declare s & a variables as double I still get zeros for the first two print statements, and 1024 for the last print statement. I'm just lost as to what is going on. Appreciate any help!
/* square code */
#include <stdio.h>
int main() {
int s, a;
printf("enter the length of your square \n");
scanf("%f", &s);
a= s * s;
printf("the area of your square is %f cm using f placeholder \n", a);
printf("the area of your square is %lf cm uning fl placeholder\n", a);
printf("the area of your square is %d cm using d placeholder \n", a);
return(0);
}
int s;
scanf("%f", &s);
If s is an int then you need to scan it in like an int using "%d" instead of "%f". What you are doing is undefined behavior, both in scanf() and printf(). An integer needs to be scanned and printed as an integer.
I think the misunderstanding is that you treat the conversion specifiers %d and %f as defining only the output formatting, regardless of the type of value passed.
Actually each conversion specifier is tightly coupled with a particular type of argument it will accept; For example, %d is defined for integral types only, and all other types like floating point or string used together with %d will yield undefined behaviour (cf, for example, cpp-reference of printf for a more detailed explanation).
So double f=1.2;printf("%d",f) will not format a floating point value 1.2 as integral (someone might expect 1 being the output); It rather yields undefined behaviour, having any output (even no output), a crash, a... whatever.
Same applies to scanf-format specifiers, of course.
So if data type is int, use %d; if data type is double, use %f.
In your code you use s as an int, and when you use scanf you are using %f instead of %d. That's the reason why you are getting the first two outputs as a zero.
As for the third output, you will get the expected value, because in that case you are correctly using %d. See the code below:
int s;
printf("enter the value");
scanf("%d", &s);
printf("the area of square is=", s*s);
Hey guys thanks so much for the help. Before checking back this morning I tried one more thing, and alas it was a stupid error.
When declaring "double s,a", I realized my scanf() function was using scanf("%f",s) as opposed to the correct scanf("%lf", s)".
Also thanks for the help for use of %d when using "int s,a".
This code here gives incorrect value of int num if my input for num is for example 11,the printf function will output 0. However if I add static to int num the output produced by printf is correct. Can someone please explain the reason.Also if I make the format specifier for second scanf as %c , then also int value is printed correctly.
#include<stdio.h>
int main()
{
int num;//making it static gives correct result
char ch;int c;
printf("enter the value of num and ch:\n");
scanf("%d",&num);
scanf("%d",&ch);
printf("num = %d and ch = %c",num,ch);
return 0;
}
It's not specifically with printf(), the issue is caused by the erroenous call to scanf().
Quoting C11, chapter §7.21.6.2
[...] Unless assignment suppression was indicated by a *, the
result of the conversion is placed in the object pointed to by the first argument following
the format argument that has not already received a conversion result. If this object
does not have an appropriate type, or if the result of the conversion cannot be represented
in the object, the behavior is undefined.
For %d conversion specifier, the expected type of argument is
d [....] The corresponding argument shall be a pointer to
signed integer.
But, all you're supplying is a pointer to a char. Mismatch. Undefined behavior.
OTOH, for %c conversion specifier,
c Matches a sequence of characters of exactly the number specified by the field
width (1 if no field width is present in the directive).
If no l length modifier is present, the corresponding argument shall be a
pointer to the initial element of a character array large enough to accept the
sequence. No null character is added.
so using
scanf("%c",&ch);
is correct. Alternatively, you can also use
scanf("%hhd",&ch); //char is signed
scanf("%hhu",&ch); //char is unsigned
This
char ch;
scanf("%d", &ch);
will invoke Undefined Behavior, since you are using the format for an integer, to store it in a character.
What you observe is very likely because second scanf with the wrong (tool large for char) format specifier overwrites the automatic variables memory where num is located.
Making num static moves it to the global variables memory and it (kind of) works, but it's still undefined behaviour, some memory have been overwritten somewhere and you may pay the price later on. So the only option is to specify the correct format specifier, here %c as you noted.
Try to add \n, like scanf("%d\n", &num);. Maybe it could help.
And compiler cannot pass if you use that expression, like scanf("%d", &ch);
Here in this code, when I execute it, it sometimes display correct value of nT and nF, sometimes it display correct nF but incorrect nT(i.e 0).
Why is it so??
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
unsigned char nT,nF;
puts("Enter No. of Testcases & Faults");
scanf(" %hhu %hhu",&nT,&nF);
printf("\n %hhu %hhu",nT,nF);
}
You need to use %hhu as the format specifier for an unsigned char. (Obscure I know: one for the pub quiz.) Also, you might want to introduce some spaces between your formatters:
int read = scanf("%hhu %hhu %s", &nT, &nF, extension);
Currently the behaviour of your program is undefined.
Prior to C99 you're pretty much at the mercy of your compiler.
Further notes:
Always check the return value of scanf which gives you useful information about the number of inputs that are read successfully.
extension is only good for 4 characters plus the nul-terminator.
I was teaching the C programming language to a friend and we came up with something I could not explain. This is the code we wrote:
#include <stdio.h>
int main(void)
{
char num1;
char num2;
printf("%s", "Enter the first number: ");
scanf("%d", &num1);
printf("%s%d\n", "The number entered is:", num1);
printf("%s", "Enter the second number: ");
scanf("%d", &num2);
printf("%s%d\n", "The number entered is:", num2);
printf("%s%d\n", "The first number entered was:", num1); /* This was done for testing */
printf("%s%d\n", "The sum is:", num1+num2);
return 0;
}
The weird thing is that we tried to do 5 + 6 and we expected to get 11 but instead got 6, I added a line to see what's going on with the first number and it becomes 0 after the second number is read.
I am aware that the variables should be an int (in fact the original code was like that and worked) but my understanding is that a char is a small integer so I thought it would be 'safe' to use if we were adding small numbers.
The code was tested and compiled on a Linux machine with cc and on a Windows machine with cl. The output was the same. On the Windows machine the program throw an error after the addition.
I would like an explanation on why this code is not working as I expected. Thanks beforehand.
You cannot pass a pointer to a different datatype to scanf. scanf will write to memory assuming you gave it a pointer to what it expected (e.g. int for %d), and will exhibit wonderful undefined behaviour if you give it a pointer to a different datatype.
Here, what is most likely happening is that scanf is overwriting e.g. 4 bytes on your stack when your chars only take up 1 byte, so scanf will just be happily writing right over some other variable on your stack.
a char is a small integer so I thought it would be 'safe' to use it if we were adding small numbers.
That is correct, char is a small integral type , and it's OK to use it in integer arithmetic(although char may be signed or unsigned which may causes the result unexpected).
But the problem is, a pointer to char can NOT be used in a place where a pointer to int is expected. And this is the case for scanf("%d", &num1);, the second parameter is expected to a of type int *.
What is wrong with the following code written in c language?
I encountered a segmentation fault. what is it?
int a[2];
for(i=0;i<2;i++)
{
scanf("%d",a[i]);
printf("%d",a[i]);
}
Why couldn't it run? leave about declarations. Does scanf have any delay problems?
This:
scanf("%d",a[i]);
is wrong. The %d format specifier requires a pointer to where the value should be stored after conversion, i.e. it should be:
scanf("%d", &a[i]);
This is required since otherwise you pass the value of a[i] to scanf(), giving it no way to change the value. By passing the address of the value, scanf() can simply write to the provided memory address to change the value that is stored there. With printf(), you don't want your values to be changing, so passing them directly to printf() is fine.
Also, conversions (like many other forms of I/O) can fail, so you should check the return value before relying on the conversion having succeeded:
if( scanf("%d", &a[i]) == 1 )
printf("%d\n", a[i]);
You should probably read the manual page for scanf() a couple more times. :)
Pass the address of a[i] to scanf instead of the value of a[i].
scanf("%d",&a[i]);