Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The output of the following C code is T T , why not t t?

Tags:

c

macros

The output of the following C code is T T, but I think it should be t t.

#include<stdio.h>
#define T t
void main()
{
     char T = 'T';
     printf("\n%c\t%c\n",T,t);
}
like image 573
Anjani Kumar Avatar asked Dec 14 '16 19:12

Anjani Kumar


1 Answers

The preprocessor does not perform substitution of any text within quotes, whether they are single quotes or double quotes.

So the character constant 'T' is unchanged.

From section 6.10.3 of the C standard:

9 A preprocessing directive of the form

# define identifier  replacement-list  new-line

defines an object-like macro that causes each subsequent instance of the macro name 171) to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive. The replacement list is then rescanned for more macro names as specified below.

171) Since, by macro-replacement time, all character constants and string literals are preprocessing tokens, not sequences possibly containing identifier-like subsequences (see 5.1.1.2, translation phases), they are never scanned for macro names or parameters.

like image 172
dbush Avatar answered Sep 22 '22 02:09

dbush