Friday 14 October 2011

Chapter 2: [B] Point out the errors, if any, in the following programs:



(a) 

main( )
{
    float   a = 12.25, b = 12.52 ;
    if ( a = b )
       printf ( "\na and b are equal" ) ;
}

No syntax error however logical error can occur due to missing relational operator == inside if condition.

 
 
(b)  

main( )
{
  int   j = 10, k = 12 ;
  if ( k >= j )
 {
   {
      k = j ;
      j = k ;
   }
 }

No Error



(c)

main( )
{
    if ( 'X' < 'x' )
    printf ( "\nascii value of X is smaller than that of x" ) ;

No Error



(d)  

 main( )
{
    int  x = 10 ;
    if ( x >= 2 ) then
       printf ( "\n%d", x ) ;
}

then is not a keyword in c



(e)

main( )
{
    int  x = 10 ;
    if x >= 2
       printf ( "\n%d", x ) ;
}


condition is not enclosed in braces 


  
(f)

main( )
{
    int  x = 10, y = 15 ;
    if ( x % 2 = y % 3 )
          printf ( "\nCarpathians" ) ;
}


Relational operator (==) is missing inside condition.



(g)
 
main( )
{
    int x = 30 , y = 40 ;
    if ( x == y )
       printf( "x is equal to y" ) ;
    elseif ( x > y ) 
       printf( "x is greater than y" ) ;
    elseif ( x < y )
       printf( "x is less than y" ) ;
}


There should be a space between else and if

(h) 

main( )
{
    int  x = 10 ;
    if ( x >= 2 ) then
       printf ( "\n%d", x ) ;
}

then is not a keyword in c.


(i)

main( )
{
    int a, b ;
       scanf ( "%d %d",a, b ) ;
    if ( a > b ) ;
       printf ( "This is a game" ) ;
    else
       printf ( "You have to play it" ) ;
}

& missing inside scanf



3 comments:

  1. (i)
    .
    if(a>b);
    ; terminator can be the second error
    right?

    ReplyDelete
  2. (i)
    Error is else without a previous if.

    ReplyDelete
  3. int i=0
    if (i=0)
    print("pls bring further notes")
    else
    printf("thank u for this")


    ReplyDelete