0

I was revising some C concepts and tried this:

printf("%d %d %d", "%da", 'a', 'A'); //16079992 97 65 

I can understand that for a and A, it prints ASCII values : 97 and 65. However I cant understand why it prints 16079992 for "%da". In fact I dont understand how C interpretes strings when outputing them as integer with %d. There has to be some algorithm / logic behind that. Anyone?

1
  • 5
    "There has to be some algorithm / logic behind that." No, there doesn't. If you cross the street without looking both ways, what will happen? Who knows. You might get hit by a car. You might get hit by a truck. Anything can happen. If you process information in a way that's specifically prohibited, you can get complete garbage. Commented Aug 4, 2014 at 12:32

2 Answers 2

6

String literals in C are arrays of char which in this context will decay to a pointer to the first element.

What you have is undefined behavior though, since you are misspecifying the format string in printf and telling it that the pointer is an int which it is not. Most compiler given the correct warning flags(which we should always be using) will provide a warning for this, for example gcc provides the following warning when using -Wall:

warning: format '%d' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat=] printf("%d %d %d", "%da", 'a', 'A'); ^ 

clang provides a similar warning it seems by default.

For reference, the draft C99 standard tells us a string literal is a array of char in section 6.4.5 String literals:

[...]initialize an array of static storage duration[...]For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence;[...]

decay to pointer is covered in section 6.3.2.1 Lvalues, arrays, and function designators:

[...]an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array[...]

and we get undefined behavior from section 7.19.6.1 The fprintf function:

[...]If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

Sign up to request clarification or add additional context in comments.

4 Comments

!!! what are you? walking index of language specifications? Was just curious, if you have read formal specs of all these C variants?
@Mahesha999 no, I know enough to answer questions that can not be properly answered without a decent knowledge of the standards. There are plenty of SO users that know the standards very well, check out the laguage-lawyer tag, many who know it far better than I.
ok but still did u tried reading Formal Language Grammar of these C variants? May be that will encourage me.
@Mahesha999 well, the grammar is part of the standard. So the sections that deal with expressions, statements and declarations for example all include formal grammar.
0

A string in C is a pointer, which has a numeric value (the address). So printf is just printing the address of the string there.

1 Comment

Actually, it is undefined behaviour. On a 64 bit compiler %d will definitely not print the address of the string, and on other compilers it might not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.