Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does printf work?

Tags:

c

memory

printf

I looked over but couldn't find a decent answer.

I was wondering how printf works in case like this:

char arr[2] = {5,6};

printf ("%d%d",arr[0],arr[1]);

I was thinking that printf just walks through the format and when it encouter %d for example it reads 4 bytes from the it's current position... however that's gotta be misconcepition cause that above works perfectly.

so, where am I wrong ?

like image 888
Idan Avatar asked Feb 04 '10 10:02

Idan


1 Answers

You're right. But there's argument promotion that converts (among other things) your char:s into int:s when they are used with a "varargs" function like printf().

like image 104
unwind Avatar answered Oct 18 '22 12:10

unwind