what shud be the condition?

This is a discussion on what shud be the condition? within the C Programming forums, part of the General Programming Boards category; if(cond) printf("hello"); else printf("world"); what should be the condition so that output becomes helloworld.....

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    what shud be the condition?

    if(cond)
    printf("hello");
    else
    printf("world");

    what should be the condition so that output becomes helloworld..

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    This is not C#, it's C.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Moved

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    There is no condition that will execute both the 'if'. and the 'else'. By definition, the 'else' will execute only if the 'if' doesn't.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    I see the students suffering at the hands of a "teacher" who thinks teaching stupid programming tricks is somehow worthwhile.

    Here, since you're not going to learn anything where you are, a pointless freebie
    Code:
    if( !printf("hello") )
        printf("hello");
    else
        printf("world");
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by Salem View Post
    I see the students suffering at the hands of a "teacher" who thinks teaching stupid programming tricks is somehow worthwhile.

    Here, since you're not going to learn anything where you are, a pointless freebie
    Code:
    if( !printf("hello") )
        printf("hello");
    else
        printf("world");
    I agree, it is quite stupid. Though I can see a C teacher mentioning it, just to show that a lot of functions have return values, and that sometimes unexpectedly, expressions return a non-void value.

    In case the OP is wondering as to the reasoning, the reason being is that printf(), while being a function who's return value is often ignored, DOES NOT have a void return, it returns an int, like most functions in the std libs.

    On failure printf() returns a value less than 0 (negative).
    On success printf() returns the number of characters printed.

    So if you print some chars, ie.
    Code:
    int a = printf("Hello\n");
    printf("%i\n", a);
    You might end up with some output similar to:
    Code:
    Hello
    6
    The sixth char is because of the linebreak escape character.

    In C any NON-ZERO VALUE is true as far as conditionals are concerned, this includes any number, such as ... 6. So printf("Hello") returns 5, 5, not being equal to 0, means the IF conditional is TRUE, the exclamation is the logical NOT operator, making the total evaluation FALSE, so the IF is not true, it goes into the ELSE, and prints "World" - another method could be:
    Code:
    if (printf("Hello")) // printf returns 5, 5 != 0, therefore condition is TRUE
       printf("World");
    else
       printf("Hello");
    Which would have the same output.

    As you may or may not be able to tell, even though the function is inside the IF conditional as so, it still executes.

    Hope that clears it up if there were any ambiguities regarding the rationale behind why that works.

    EDIT:
    That is a very big thing with C. Any NON-ZERO VALUE is TRUE.

    For example, in the old days, before C99, before they replaced the NULL macro to be a void pointer to address 0, people simply compared pointers to 0 in order to test if they were null.

    Old style would be:
    Code:
    char* ptr_to_string = 0;
    if (ptr_to_string != 0)
       ;// do some magic
    else
       printf("PTR IS NULL\n"); // error case, which will be printed
    
    ptr_to_string = (char*)malloc(512); // Now "ptr_to_string" is pointing to an address on the heap, a NON-ZERO ADDRESS, it now evaluates to true.
    if (ptr_to_string)
       printf("Pointer is NOT null!\n"); // Which will be printed
    else
       printf("PTR IS NULL\n"); // Not printed anymore, the pointer is not null.
    Not much has changed in terms of use, but people rarely compare pointers to 0 anymore, simply they compare them to NULL, or don't comare them to anything, ie.
    Code:
    if (ptr_to_string)
       ; // Perfectly valid, if "ptr_to_string" is NULL (0), it's false, if it's an address (any address), it's true.
    Last edited by Syndacate; 09-08-2010 at 11:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex, Cond and Thread questions (pthread, linux)
    By thebearot in forum C Programming
    Replies: 14
    Last Post: 04-23-2010, 01:10 PM
  2. Condition variables
    By sethjackson in forum Windows Programming
    Replies: 16
    Last Post: 03-19-2008, 12:42 PM
  3. SDL Condition variables.
    By antex in forum Game Programming
    Replies: 3
    Last Post: 11-11-2005, 07:11 AM
  4. Looping condition
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 03:06 PM
  5. Race condition
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 10-24-2004, 10:42 PM

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