Statements in C

By: Ram  

All the statements in C ends with a “;”. The various statements in C are

1)      Declaration Statement

2)      Assignment Statement

3)      Output  Statement

4)      Input Statement

5)      Control Statement

6)      Loop Statement etc.

 

Declaration Statement:-

It is used to specify the data type of variables used in the program

 

Syntax:

 Datatype Variable or variables separated by , ;

 

Eg- int b;

Here the variable b is int and it allots 2 bytes

 

Eg- int c,e;

Here the variables c and e are int and it allots 4 bytes

 

Eg- float n1,n2;

Here n1, and n2 are declared as float and hence allotted 4 bytes each

 

 

Assignment Statement:-

The assignment statement is used to assign a constant to a variable.

 

Syntax:

                     Constant;

                          or

Variable =    Variable;

                          or

                     Expression;

 

Eg: (assigned to a constant)

            a= 5;

             b =10;

Eg: (assigned to a variable)

             x=y;

             a= 2*e;

Eg: (assigned to a expression)

            y= c*d

            s= 4*20

 

Output Statement:

The output statement displays the values and after printing the values it points on the same line.

 

Syntax:

 printf(“string format”, variable or variables separated by ,);

 

Here the format means the data specification of the variable. Say, for example

 

  integer                  format

----------------------------------

 Decimal                  %d

 Octal                       %o

 Hexadecimal           %x

 

float                           format

-----------------------------------

Without exponent       %f

With exponent            %e

 

Char                            %c

 

 

 

Eg:   printf(“Hello World”);

The output is Hello World 

 

Eg:  printf(“%d”,x)

The output is the value of x 

 

Eg: printf(“sum %d”,s)

The output is sum value of s

 

Input Statement:

The input statement is used to read(accept) constant at the variable at the time of program execution.

 

Syntax:

      scanf(“format”, & variable or &variables separated by comma,);

 

Eg: scanf(“%d”,&n);

      It asks for the value of n

Eg: scanf(“%d %d”,&a,&b);

         run 20

                40 (ie.a=20 and b=40)

 

Control Statement:

The control statement denotes the sequence of execution of the program. There are two types of control statements namely: conditional control statement and unconditional control statement.

 

(1)Conditional control statement:

Here, the control is transferred to any part of the program without checking for any condition.

 

1. --------------

2. goto end

3. -------------

4. end:

5. -------------

 

 

Eg: goto labelname

The control is transferred to the line where the label starts as shown above.

 

(2) Unconditional control statement:

Here, the condition is first checked and if it is true then the executes that particular part and if false, then another part is executed.

 

Eg:  if

       if……………else

       switch

 

Loop Statement:

Loop statements are mainly used in programs to do repeated calculations.

Eg: while

       do ………….while

        for

 

Syntax:

               while (conditions)

                {

                     statements;

                 }

 

Structure of a Program:

 

Documentation Section

(remarks- comments)

 

Link section

(to link with other programs)

 

Global declaration Section

(common declaration)

 

main()

{

Declaration statement;

Statement part;

}

Sub programs

--------------

---------------

 

Sample Program:

/* program to find area of a square*/

main()

{

 int s,a;

printf(“enter the side “);

scanf(“%d”,&s);

a=s*s

printf(“Area of a square= %d”,a);

}

 




Archived Comments


Most Viewed Articles (in C )

Latest Articles (in C)

Comment on this tutorial