preprocessor problem

Discussion in 'C Programming' started by onkar, Jun 6, 2006.

  1. onkar

    onkar Guest

    $ 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\")
     
    onkar, Jun 6, 2006
    #1
    1. Advertising

  2. onkar wrote:
    > $ 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'");
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Jun 6, 2006
    #2
    1. Advertising

  3. onkar

    onkar Guest

    Why isn't my program compiling

    Harald van Dijk wrote:
    > onkar wrote:
    > > $ 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'");
     
    onkar, Jun 6, 2006
    #3
  4. onkar

    Suman Guest

    onkar wrote:
    > Harald van Dijk wrote:
    > > onkar wrote:
    > > > $ 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.
     
    Suman, Jun 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cronus
    Replies:
    1
    Views:
    695
    Paul Mensonides
    Jul 15, 2004
  2. Johnny Willemsen

    Preprocessor problem with Visual Age 6

    Johnny Willemsen, May 2, 2005, in forum: C++
    Replies:
    0
    Views:
    352
    Johnny Willemsen
    May 2, 2005
  3. Henry Townsend

    token pasting problem in K&R preprocessor

    Henry Townsend, Jun 19, 2006, in forum: C Programming
    Replies:
    13
    Views:
    662
  4. Subra
    Replies:
    7
    Views:
    438
    Subra
    Jul 4, 2007
  5. C preprocessor problem

    , Nov 12, 2007, in forum: C Programming
    Replies:
    4
    Views:
    359
Loading...

Share This Page