Can I check my understanding of scanf?
int a;
scanf("%d",&a);
If I input 13, does conversion specifier convert 13 to binary and stored it in a
?
If input is 13.3, then does it convert decimal fraction 13.3 to binary and store it in a?
Can I check my understanding of scanf?
int a;
scanf("%d",&a);
If I input 13, does conversion specifier convert 13 to binary and stored it in a
?
If input is 13.3, then does it convert decimal fraction 13.3 to binary and store it in a?
I don't know about your knowledge in C, but how data stores in C is really is very important and interesting topic. whenever data stores in variable it is always in binary format, it is not possible to see that binary data in simple C language but you can see the binary in embedded C. Where we pass data in any port or any program providing data internally and then sending the output to any port which is connected to the led. Suppose the if you passed -1 in a port and the port is sending output to led connected with it then you can see all leds connected with it will glow. In C you can observe like that
char c=-1;
printf("%u",c);
output=255 %u is used for unsigned integer it is not an error you can check it. int and char always store in same fashion means if you try store char A then I think so it's ASCII value 65 will stored in binary way.
char ch='A';
printf("%d",ch);
output 65
same
int i=65;
printf("%c",i);
output A; But float is stored completely different from int it has three different parts one is sign bit, second is mantissa and then exponent. suppose
float f=5.55;
if(f==5.55)
pf("true");
else
pf("false");
output : false
now again
float f=5.25;
if(f==5.25)
pf("true");
else
pf("false");
output : true so it's all about data stores in memory very important topic generally teacher not discuss in their classes. Hope use full for you.
OP: If I input 13, does conversion specifier convert 13 to binary and stored it in a?
A: Yes. When user enters 1, 3, Enter, scanf("%d", ...)
receives optional leading white-space (which there is none) then the char
s '1'
, '3'
, and '\n'
. Seeing '\n'
is not a digit, it is put back into stdin
for the next IO operation. The '1'
, '3'
are converted to an int
with the value of 13 **. The 13 is stored at &a
. scanf()
then returns a value of 1 which the code unfortunately does not use.
OP: If input is 13.3, then does it convert decimal fraction 13.3 to binary and store it in a?
A: No. scanf("%d", ...)
reads the text as described above but instead of stopping at '\n'
, it stops at '.'
and puts that back for subsequent IO. The '1'
, '3'
is converted to 13 and stored in &a
. The next IO operation will start with '.'
. and likely cause trouble.
Suggest instead:
char buf[40];
fgets(buf, sizeof buf, stdin);
if (sscanf(buf, "%d", &a) != 1) Handle_IntNotEntered(buf);
Other approaches exist , like using strtol()
, but the important issue is that users enter all sorts of unexpected text and the input needs validation and error handling.
** The 13 is typically stored in binary - I know of no machine that that does otherwise. But the details of how the 13 is stored should not make a difference. a
will have the value of 13, if it is stored in binary, BCD or with 13 chipmunks running on 13 wheels.
In short, yes. There are different encoding by which computer can store data. Some machine are little endian some are big endian.
For, floating point number you can refer,
IEEE floating point
In short no. But you can change the code to almost work.
float a;
scanf("%f",&a);
However .3
can never be stored exactly in a float.
There is also a decimal type, a decimal based float (as opposed to the binary based one). It can store 0.3 exactly (and all other numbers you can type, up to its precision). But you can still not store 1/3
(you would need a fraction store to store that precisely). Or π ( you need a symbol store to store that). However you don't always need to be precise.