preprocessor problem

O

onkar

$ cat test.c

#include<stdio.h>
#define PRINT(s) printf(#s);
int main(void){
PRINT(use \ ("backslash") not /);
return 0;
}

$ gcc -E test.c |tail
# 679 "/usr/include/stdio.h" 3

# 2 "test.c" 2

int main(void){
printf("use \ (\"backslash\") not /");
return 0;
}

$gcc -g -o test test.c
test.c:4:40: warning: unknown escape sequence: '\040'

Why is \ not put in before \ ?
observe that \ is put before " i.e., in (\"backslash\")
 
G

Guest

onkar said:
$ cat test.c

#include<stdio.h>
#define PRINT(s) printf(#s);
int main(void){
PRINT(use \ ("backslash") not /);
return 0;
}

$ gcc -E test.c |tail
# 679 "/usr/include/stdio.h" 3

# 2 "test.c" 2

int main(void){
printf("use \ (\"backslash\") not /");
return 0;
}

$gcc -g -o test test.c
test.c:4:40: warning: unknown escape sequence: '\040'

Why is \ not put in before \ ?
observe that \ is put before " i.e., in (\"backslash\")

\ will get doubled if and only if it is part of a string or character
constant (there's one possible exception, but you'll probably not have
to care about that yet). If \ always got doubled, you wouldn't be able
to use any escape sequences. So:

PRINT(\n) will expand to printf("\n");

However,

PRINT("\n") will expand to printf("\"\\n\"");
PRINT('\n') will expand to printf("'\\n'");
 
O

onkar

Why isn't my program compiling
\ will get doubled if and only if it is part of a string or character
constant (there's one possible exception, but you'll probably not have
to care about that yet). If \ always got doubled, you wouldn't be able
to use any escape sequences. So:

PRINT(\n) will expand to printf("\n");

However,

PRINT("\n") will expand to printf("\"\\n\"");
PRINT('\n') will expand to printf("'\\n'");
 
S

Suman

onkar said:
Harald said:
onkar said:
$ cat test.c

#include<stdio.h>
#define PRINT(s) printf(#s);
int main(void){
PRINT(use \ ("backslash") not /);
return 0;
}

$ gcc -E test.c |tail
# 679 "/usr/include/stdio.h" 3

# 2 "test.c" 2

int main(void){
printf("use \ (\"backslash\") not /");
return 0;
}

$gcc -g -o test test.c
test.c:4:40: warning: unknown escape sequence: '\040' [ snip explanation ]
Why isn't my program compiling

It does compile; are you by any chance doing
../a.out
instead of
../test
(note the command line you've used).
However, the compiler is not too happy with your code.
The compiler doesn't know what '\<space>' means: and
gives out that `warning: ...'\040' `. Look up the ASCII chart
for space.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top