%u printf

S

saurabh

#include<stdio.h>
int main()
{
int i;
printf("\n%u%u",sizeof(i));
}

output 42433316

plz explain the output
with 1 %u o/p =4bytes
with 2 %u o/p=42433316 ????
 
I

Ian Collins

saurabh said:
#include<stdio.h>
int main()
{
int i;
printf("\n%u%u",sizeof(i));

Here you tell printf to expect two more parameters, but you only pass one.
}

output 42433316

plz explain the output

Please use real words.
with 1 %u o/p =4bytes
with 2 %u o/p=42433316 ????
The first 4 is the valid output, the rest is undefined garbage.
 
G

Giorgio Silvestri

Ian Collins said:
Here you tell printf to expect two more parameters, but you only pass one.


Please use real words.

The first 4 is the valid output, the rest is undefined garbage.

Moreover "%u" is valid in order to print unsigned int.

printf("%u\n",(unsigned int)sizeof(i));

size_t can be "bigger" than unsigned int.

In C99 you can use %zu for size_t.

printf("%zu\n",sizeof(i));
 
H

Herbert Rosenau

#include<stdio.h>
int main()

int main(void)
{
int i;
printf("\n%u%u",sizeof(i));
}

output 42433316

You're multiple times in undefined behavior:
1. You use uninitialised i.
2. You use more conversion flags in the format string as defined in
actual parameters.
3. sifeof returns type size_t not unsigned int.

Another error is that you lets mis the '\n' immediately before the
format string ends.

The output of your program means nothing. It is completely random
based on the errors. Undefined behavior means that all and anything
one can thing about may occure.

I've seen seldom so manny errors in so little number of lines of code.

Fix all the errors and try again.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
C

Clark S. Cox III

Herbert said:
int main(void)


You're multiple times in undefined behavior:
1. You use uninitialised i.
2. You use more conversion flags in the format string as defined in
actual parameters.
3. sifeof returns type size_t not unsigned int.

While #2 and #3 are indeed undefined, there is nothing undefined about
#1 (Note, that the OP doesn't use the *value* of i)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top