Semantic error of numbers

Write your question here.
Hello everyone,I wonder why the result of %d is www=536870912 instead of www=90 ??Since %d represents integral.I'll be appreciated if you tell me how 536870912 appears.
1
2
3
4
5
 float www=90.333f;
 printf("www=%d\n",www); 
 system("pause");        
 return 0;
}

Last edited on
Check you printf statement. You're trying to print www as an int, not a float.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
To elaborate, a little bit:
std::printf can accept any number of arguments but it is not type-safe. It doesn't have any idea of a concept of "type" of the things you pass to it, except through the format string.

When you call printf with some arguments:
- The caller pushes all the arguments on the stack
- printf gets a va_list (i.e., probably a pointer) to the first argument (the format string) using the macro va_start.
- printf looks through the format string to find format specifiers.
- when it finds each format specifier, printf increments it's va_list (with va_arg) to point to the next argument on the stack. va_arg gets passed the requested type of the next argument, and spits it out. In other words, any typecast is likely performed through the varargs mechanism, which cannot preserve the original type of the argument and therefore can't do a conversion sequence. This is why the result is unpredictable.
- Once there are no more arguments, the caller cleans the stack.

Please ask if any of this is unclear.
Last edited on
Topic archived. No new replies allowed.