C variable assignment issue -
I think the title is not suitable for my question. (I appreciate it, if someone suggests a suggestion) I am learning with "CC Hard Way" I am using printf
for output values using the format specifier. This is my code snippet:
#include & lt; Stdio.h & gt; Int main () {int x = 10; Float y = 4.5; Four c = 'c'; Printf ("x =% d \ n", x); Printf ("y =% f \ n", y); Printf ("c =% c \ n", c); Return 0; }
This works because I expect when it comes to conversion, then I want to deal with it, it was all right until I got it done char
to float
was created to break it by converting this line:
printf ("c =% f \ N", c);
OK, I am compiling it and this is output:
~ $ cc ex2.c -o ex2 ex2.c: in the function 'Main': ex2.c: 13: 3: WARNING: Format '% f' is expected to 'double' type argument, but 'int' [-Wformat =] printf ("c =% f \ N "c); ^
The error clearly tells me that it can not convert from int
to float
, but this compiler will have an object The file, and the deceptive part here, is where I run the object file:
~ $ / EX2x = 10 y = 4.500000 c = cc = 4.500000
As you can see printf
last float
prints the value Is before I tested it with other values for y
and in each case it prints the value of y
the value of c
. Why does this happen
Your compiler is warning you about undefined behavior. Anything can happen. Expecting to work for nasal demons. There is a good reference on this topic
Generally, int can convert just to fine:
int i = 10; Double d = i; // work fine
printf
is a special type of function because it can take any number of arguments, so the types must match exactly. When char
is given, it is promoted to int
. printf
, however, uses % f
you used to get it to double
Int add_nums (int count, ...) {int is one of the results which is not going to work. = 0; Va_list args; Va_start (args, count); For (int i = 0; i
The number of arguments that followed count
is no way to tell the function without knowing it. Can extract it from the format specifiers in the printf
string.
The other relevant part is the loop it will execute calculation
times each time, it uses va_arg
to get the next logic. Note how this gives the va_arg
so it is considered that the function needs to depend on the caller, which is int
in va_arg
Promoted for the call, so that he can work properly.
In the case of printf, there is a defined list of format specifications that each tell you how to use
. % d
< Code> int % f
is double
. % c
is also int
because char
is promoted to int
, but printf
During the formation of the output, that integer needs to be represented as a character.
In this way, for some different arguments in any function, some collar collaboration is required. One more thing that may be wrong printf
is giving too many format specifiers It goes blindly and receives the next argument, but there is no other logic. uh oh.
If this is not enough, the standard explicitly asks for fprintf
(in which it defines printf
) C11 (N1570) ) §7.21.6.1 / 9:
If any argument is not the right type for this conversion specification, then the behavior is undefined.
Everything, when you are not cooperating with printf
, thank you to your compiler to warn you that it will save you some very bad results. Could.
Comments
Post a Comment