; problem near if statement

This is a discussion on ; problem near if statement within the C Programming forums, part of the General Programming Boards category; when i wrote this a took nothing Code: #include <stdio.h> int main( ) { int x = 10, y = ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    18

    ; problem near if statement

    when i wrote this a took nothing
    Code:
    #include <stdio.h>
    
    int main( )
    {
        int x = 10, y = 20 ;
        if ( x == y )
           printf ( "\n%d %d", x, y ) ;
        system("pause");
    }
    Code:
    #include <stdio.h>
    
    int main( )
    {
        int x = 10, y = 20 ;
        if ( x == y ) ;
           printf ( "\n%d %d", x, y ) ;
        system("pause");
    }
    when i wrote this i took the correct result.I think it should give the correct result in first beacuse of ; near if expression.What's my fault?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > if ( x == y ) ;
    This is the same as
    Code:
    if ( x == y ) {
      // there isn't anything to do here
    }
    You can indent the printf statement, but it won't make it part of the if statement.

    Oh, and for consistent behaviour across all platforms, put your \n at the end of the format string.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    18
    thanks a lot i understand my fault

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    
    int main( )
    {
        int x = 10, y = 20 ;
        if ( x == y )
           printf ( "\n%d %d", x, y ) ;
        system("pause");
    When's the last time that 10 = 20?

    X does not equal y ... therefore the printf statement is never executed.
    It worked perfectly... however; not in the way you expected.
    Last edited by CommonTater; 01-17-2011 at 07:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 03:05 PM
  2. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  3. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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