Thread: While implementing a #define a bug emerged that I cannot find

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    While implementing a #define a bug emerged that I cannot find

    Hey Guys,
    I'm learning about marcros. Specifically #define with parameters. I cannot seem to get my loop to work using the code provided by the book. The Goal: print ABCD from abcd but, I'm supposed to use this code:
    Code:
    putchar(TOUPPER(s[++i]));
    .

    Here's the complete program:

    Code:
    //ANSWER: http://knking.com/books/c2/answers/c14.html
    // Problem 5
    #include <stdio.h>
    #include <string.h>
    
    
    #define TOUPPER(c) ('a'<=(c)&&(c)<='z'?(c)-'a'+'A':(c))
    
    
    int main(){
        char s[6];
        strcpy(s, "abcd");
        int len=strlen(s), i;
        for(i=0;i<len;){          // DOESN'T WORK, PRINTS " D"           
            printf(" ");
            putchar(TOUPPER(s[++i]));
        }
        /*for(i=0;i<len;i++){     // THIS CODE WORKS
            printf("%c, ",TOUPPER(s[i]));
        }*/
        return 0;
    }
    Somehow the looping syntax is flawed. Please help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > #define TOUPPER(c) ('a'<=(c)&&(c)<='z'?(c)-'a'+'A':(c))
    Look what happens when you use an expression with a side effect.

    putchar(TOUPPER(s[++i]));

    you get
    putchar( ('a'<=(s[++i]))&&(s[++i]))<='z'?(s[++i]))-'a'+'A':(s[++i]))) );

    You end up with rather more ++i's than you bargained for.

    It's one of the hazards of using macros.

    Oh, and if you're ever confused about what a macro is doing, gcc -E is your friend.
    Code:
    $ gcc -E foo.c | tail
        int len=strlen(s), i;
        for(i=0;i<len;){
            printf(" ");
            putchar(('a'<=(s[++i])&&(s[++i])<='z'?(s[++i])-'a'+'A':(s[++i])));
        }
        
    
    
        return 0;
    }
    It shows you what your code looks like after all the pre-processing has happened.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Ah, gcc -E I should have tried that as it made the problem obvious. Thank you very much. I just setup my environment to allow me to use this going forward.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-23-2020, 12:03 PM
  2. Cannot find where to define HAVE_STDIO_H in SDL
    By njitram2000 in forum C Programming
    Replies: 2
    Last Post: 11-21-2010, 06:47 AM
  3. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  4. the dreaded blue screen has emerged!
    By Yarin in forum Tech Board
    Replies: 14
    Last Post: 03-21-2008, 11:11 AM
  5. Infinite #define loop i can't find a way around
    By IonBlade in forum C++ Programming
    Replies: 6
    Last Post: 12-18-2003, 09:38 PM

Tags for this Thread