Thread: IF Condition Check

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    IF Condition Check

    Hi

    I have a code:

    Code:
    #include<stdio.h>
    #define assert(cond) if(!(cond))\
            printf("assertion failed for condition %s\n",#cond)
    main()
    {
        int x=100;
    	
        if(x == 0)
            assert(x<100);
        else
            printf("No assert call\n");
    	getchar();
        return 0;
    }
    In this assert macro will call only if x is 0.
    So normally as first if condition fail, it should come in else and print "No assert Call".

    But it is not coming in else part and directly comes to getchar().
    Can any body help why this happen?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    It works when you put brackets on the if/else:
    Code:
    	if (x == 0) {
    		    assert(x<100);
    	} else { 
    		    printf("No assert call\n");
    	}
    If you don't want to have brackets, you should encapsulate your assert function in brackets (with a do-while).

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Your else in code "belongs" to the if in the macro

    Code:
    if (x == 0)
        if (x < 100) printf(...);
        else printf("No assert call\n");
    getchar();

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Normally, when you write multi-line macros, you express them as a while loop.
    Code:
    #define assert(cond) do { \
      if(!(cond)) \
        printf("assertion failed for condition %s\n",#cond); \
    } while ( 0 ) /* trailing ; is deliberately omitted */
    When used in something like this
    Code:
        if(x == 0)
            assert(x<100);
        else
            printf("No assert call\n");
    it has the effect of making it seem to the outer level if statement that the assert is a single statement, and the else will bind to the obviously intended if (and NOT the if now buried inside a while loop).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check to check string for a series of characters
    By robi in forum C Programming
    Replies: 1
    Last Post: 02-28-2012, 05:42 PM
  2. check for y condition
    By alperen1994 in forum C Programming
    Replies: 3
    Last Post: 03-28-2009, 02:04 PM
  3. check every condition
    By alperen1994 in forum C Programming
    Replies: 9
    Last Post: 03-21-2009, 03:34 PM
  4. precondition and post condition check
    By George2 in forum C++ Programming
    Replies: 10
    Last Post: 03-19-2008, 10:38 PM
  5. get and check input as while condition
    By Marksman in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2006, 09:22 PM