1.

Solve : IF Statement in C Language?

Answer»

Hi everybody

Recently I've started learning to program . I've checked a tutorial about IF Statement in C language . the first tutorial was the very basics of the language , and the second about the IF Statement . I tried to marriage both tutorials and apply what I learned . So I tried to write a code that uses (( in this_is_a_number )) variable , and IF statement in the same time . This is the code :

================================================================

#include

int main ()
{
     int this_is_a_number;


  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
if ( 5 < %d ) ;
    printf( "Five is now less than %d , that's a big surprise" , this_is_a_number );
if ( 5 > %d ) ;
    printf( "Five is more than %d , that's bad" , this_is_a_number ) ;

    getchar();
  return 0;
}

================================================================

The compiler (Code Blocks) gave me the following error message :

error : expected expression before '%' token - Line 10
error : expected expression before '%' token - Line 12

The thing I NOTICED is that the (%) is in red , but the (d) is in black , although they should be in the same color both .

I use Windows 7 if it's important to mention


Please help guysPlease remove blank lines and use a code block when postiong.
Like this:
Code: [Select]#include <stdio.h>
int main ()
{
     int this_is_a_number;
  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
if ( 5 < %d ) ;
    printf( "Five is now less than %d , that's a big surprise" , this_is_a_number );
if ( 5 > %d ) ;
    printf( "Five is more than %d , that's bad" , this_is_a_number ) ;
    getchar();
  return 0;
The semi colon symbol ';' is used to mark the end of a statement.
The brackest eanclose a block.
Like this:
Code: [Select]#include <stdio.h>
main(){
  int i = 3,j = 5;
  if(i < j){
     printf("i < j \n");
  }   
}This example has an IF balock in side the main block.
Use  blocks for your IF statemtns.More simply. Lines of code that start with if, for, or while end with a { instead of a ;

This is because these lines start what is CALLED a code block.

ex:

if(a == 1) {
   code goes here;
}Hi guys m thanks for your help


But I do not think that was the problem .

Check this link of the tutorial :
http://www.cprogramming.com/tutorial/c/lesson2.html

Check this code in the link above .
  Code: [Select]if ( 5 < 10 )
    printf( "Five is now less than ten, that's a big surprise" );

I tried to compile this code this way , and that worked :
Code: [Select]#include <stdio.h>

int main ()

{
if ( 5 < 10 )
    printf( "Five is now less than ten, that's a big surprise" );
    getchar ;
return 0 ;
}

I mean without the {} before and after the IF statement . But anyway , I did what you said guys , and tried the code this way :
Code: [Select]#include <stdio.h>

int main ()
{
     int this_is_a_number;


  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
if ( 5 < %d ) {
    printf( "Five is now less than %d , that's a big surprise" , this_is_a_number ) ; }
if ( 5 > %d ) {
    printf( "Five is more than %d , that's bad" , this_is_a_number ) ; }

    getchar();
  return 0;
}

The compiler (Code Blocks) is still giving the same error message , which shows the same two lines of code , which are line 10 and line 12 . And the % is still in red , while the (d) is in black , they should be both in the same color , so the code recognize them as one object .

I attached a screenshot of the compiler do you can see the color of the (%) and the (d) , as well as the error messages below in the picture .

Aren't there any other ideas ? 

Please help guys   

[regaining space - attachment deleted by admin]%d is only a placeholder within the printf format string.

It is meaningless outside of that.

for example- say, in your if statement itself. if(5 > %d) basically translates to 'if 5 is GREATER than modulus d' which makes no sense.

refer to the variable name, this_is_a_number when you want to refer to that variable. if(5
But I do not care about wether it works or not . Because the reason of my posting here isn't to make work , it's just to learn where's the problem and how to write the code correctly . Could you explain how variables work with IF Statement , and why should I use the variable in IF Statement , and use %d in Scanf and Printf function ??



Discussion

No Comment Found