Thread: Question regarding tabs

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    33

    Question regarding tabs

    Hey there everyone. I was provided an example by a friend today that puzzled me.

    What will the following code print if x = 4 and y = 2?
    if(x == 3 && y == 2)
    <tab>printf("x is 3")
    <tab>printf("y is 2")


    I put <tab> to represent pressing the tab key, because it changes system to system. I don't know if that's logical or not, but I didn't want to confuse. Anyways, this problem is supposed to be about using incorrect tabs. Any input? I would think that nothing is printed, because only y=2, but x =/= 3.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Joshua Green
    Anyways, this problem is supposed to be about using incorrect tabs. Any input? I would think that nothing is printed, because only y=2, but x =/= 3.
    I could just tell you the answer, but you will remember this better if you compiled and ran a program to find out:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 4;
        int y = 2;
    
        if (x == 3 && y == 2)
            printf("x is 3");
            printf("y is 2");
    
        printf("\n");
        return 0;
    }
    Quote Originally Posted by Joshua Green
    I put <tab> to represent pressing the tab key, because it changes system to system. I don't know if that's logical or not, but I didn't want to confuse.
    Keep in mind that whitespace in not significant in this context, i.e., whether you use an actual tab character, or a number spaces equivalent to pressing the tab key on your text editor, or do not indent at all, the code is essentially the same. Indentation is for readability in C. So, this is an example of how the same indentation that can help in readability can also hinder by visually fooling the reader. Hence, I recommend that braces always be used, even when they are not necessary.
    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. Win32 Tabs
    By Sorin in forum Windows Programming
    Replies: 2
    Last Post: 10-03-2012, 03:16 AM
  2. Replies: 2
    Last Post: 04-02-2009, 04:40 AM
  3. using tabs in SDI
    By axr0284 in forum Windows Programming
    Replies: 0
    Last Post: 02-01-2005, 01:19 PM
  4. Tabs as Characters
    By jrahhali in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2004, 10:45 AM
  5. Vim tabs
    By PutoAmo in forum Linux Programming
    Replies: 2
    Last Post: 10-30-2002, 04:35 PM