Like Tree1Likes
  • 1 Post By anduril462

unused variable in a program?

This is a discussion on unused variable in a program? within the C Programming forums, part of the General Programming Boards category; Hi, This is an easy program for the answer to the exercise of K&R C 1-10: Write a program to ...

  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,484
    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
    memcpy likes this.

  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
    21,715
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09: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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21