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
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
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.
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
Eg: while
do ………….while
for
Syntax:
{
statements;
}
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
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C