Thread: unused variable in a program?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    unused variable in a program?

    Hi,

    This is an easy program for the answer to the exercise of K&R C 1-10:
    Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.
    Code:
    #include <stdio.h>
    
    int main()
    {
        int c, d;
    
        while ( (c=getchar()) != EOF) {
            d = 0;
            if (c == '\\') {
                putchar('\\');
                putchar('\\');
                d = 1;
            }
            if (c == '\t') {
                putchar('\\');
                putchar('t');
                d = 1;
            }
            if (c == '\b') {
                putchar('\\');
                putchar('b');
                d = 1;
            }
            if (d == 0)
                putchar(c);        
        }
        return 0;
    }
    The original page is here.

    What's the use of the variable `d'?

    Thanks,

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It's used as a flag to keep track of whether the character has already been printed. It is set to 0 for each character, then if it's one of the "special" characters, you print out the special escape sequence and set the flag to 1. Then, after that, if the flag is not 1 (i.e. you didn't get a special character), you just print out the character.

    I looked at the link, and I don't know what they mean by "category 0" and "category 1" solutions, but this is probably not the best solution you could have picked to learn from. I would just use if-else if setup, or a switch as the other guy used:
    Code:
    while c = getchar...
        if c is a backslash
            print a backslash
            print a backslash
        else if c is a tab
            print a backslash
            print a 't'
        else if c is a new line
            print a backslash
            print a 'n'
        else
            print whatever is in c

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    @anduril462
    thank you. But why do we need the variable `d' as a flag? It makes no significant sense.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by password636
    But why do we need the variable `d' as a flag? It makes no significant sense.
    You don't need a flag in this case. It is merely the method employed by the author of the code example to decide when the original character should be printed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unused variable causes lots of problems
    By Isnalla in forum C Programming
    Replies: 1
    Last Post: 02-25-2012, 10:40 AM
  2. Replies: 7
    Last Post: 04-16-2011, 12:28 PM
  3. what to do with unused array elements
    By droseman in forum C Programming
    Replies: 4
    Last Post: 11-05-2009, 06:30 AM
  4. How to dispose of unused characters
    By C++Newb in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2009, 09:38 PM
  5. Where do Unused function returns go?
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2006, 10:54 AM