How does printf work?

I looked, but could not find a decent answer.

I was wondering how printf works in this case:

char arr[2] = {5,6}; printf ("%d%d",arr[0],arr[1]); 

I thought printf was just looking at the format and when it gets% d, for example, it reads 4 bytes from the current position ... however, this should be wrong because it works fine.

So where am I mistaken?

+6
c memory printf
source share
2 answers

You're right. But an argument advancement that converts (among other things) your char : s to int : s when used with "varargs", like printf() .

+9
source share

When you speak:

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

the string and the result of evaluating two array expressions are pushed onto the stack and printf is called. printf takes a line from the stack and uses the% formatters in it to access the other complex arguments sequentially. Exactly how it depends, as you say, on the actual value of% - for example, %d reads 4 bytes, but %f reads 8 (for most 32-bit architectures).

+1
source share

All Articles