Professional Documents
Culture Documents
Programming language:
A programming language is a computer language programmers use to develop applications, scripts, or other set of
instructions for a computer to execute. Programming language is the intermediate language with programmer to system
(Normal High label language (syntactically English language) to binary language).
Example of Programming language:
1.
2.
3.
4.
5.
C
C++
Java
Fortran
Visual Basic
So in a procedural system our functions use data they are "given" (as parameters) but also directly access any shared
data they need.
Example: C , Cobol, Basic etc.
1954 FORTRAN ,1958 - FORTRAN II, 1958 - FORTRAN III (Never released to public) ,1961- FORTRAN
IV, 1972 - FORTRAN 66, 1980 - FORTRAN 77, 1991 - FORTRAN 90,1997 - FORTRAN 95
Example:
program hello
print *, 'Hello World!'
end program hello
Variable
Operator
Decision making / Condition
Loops
Function
Array
File Handling
Structure
1. Variable
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C
has a specific type, which determines the size and layout of the variable's memory; the range of values that can
be stored within that memory; and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with
either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive. Based on
the basic types explained in previous chapter, there will be the following basic variable types:
Type
char
int
float
double
void
Description
Typically a single octet(one byte). This is an integer type.
The most natural size of integer for the machine.
A single-precision floating point value.
A double-precision floating point value.
Represents the absence of type.
Example:
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main ()
{
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
Ans:
value of c : 30
value of f : 23.333334
2. Operator:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C language is rich in built-in operators and provides the following types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators
Operator
+
*
/
%
++
--
==
!=
>
<
>=
<=
||
!
Description
Checks if the values of two operands are equal or not, if yes
then condition becomes true.
Checks if the values of two operands are equal or not, if
values are not equal then condition becomes true.
Checks if the value of left operand is greater than the value
of right operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value of
right operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to
the value of right operand, if yes then condition becomes
true.
Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.
Example
(A == B) is not true.
(A != B) is true.
(A > B) is not true.
(A < B) is true.
(A >= B) is not true.
(A <= B) is tr
Logical Operators
Operator
&&
Example
Adds two operands
A + B will give 30
Subtracts second operand from the first
A - B will give -10
Multiplies both operands
A * B will give 200
Divides numerator by de-numerator
B / A will give 2
Modulus Operator and remainder of after an integer division B % A will give 0
Increments operator increases integer value by one
A++ will give 11
Decrements operator decreases integer value by one
A-- will give 9
Relational Operators
Operator
Description
Description
Example
Called Logical AND operator. If both the operands are non(A && B) is false.
zero, then condition becomes true.
Called Logical OR Operator. If any of the two operands is
(A || B) is true.
non-zero, then condition becomes true.
Called Logical NOT Operator. Use to reverses the logical
state of its operand. If a condition is true then Logical NOT !(A && B) is true.
operator will make false.
Bitwise Operators
Operator
&
|
^
~
<<
>>
Assignment Operators
Operator
=
+=
-=
*=
/=
%=
<<=
>>=
&=
^=
|=
Description
Example
Binary AND Operator copies a bit to the result if it exists in (A & B) will give 12, which is 0000
both operands.
1100
(A | B) will give 61, which is 0011
Binary OR Operator copies a bit if it exists in either operand.
1101
Binary XOR Operator copies the bit if it is set in one
(A ^ B) will give 49, which is 0011
operand but not both.
0001
Binary Ones Complement Operator is unary and has the
(~A ) will give -61, which is 1100
effect of 'flipping' bits.
0011 in 2's complement form.
Binary Left Shift Operator. The left operands value is moved A << 2 will give 240 which is 1111
left by the number of bits specified by the right operand.
0000
Binary Right Shift Operator. The left operands value is
A >> 2 will give 15 which is 0000
moved right by the number of bits specified by the right
1111
operand.
Description
Simple assignment operator, Assigns values from right side
operands to left side operand
Add AND assignment operator, It adds right operand to the
left operand and assign the result to left operand
Subtract AND assignment operator, It subtracts right
operand from the left operand and assign the result to left
operand
Multiply AND assignment operator, It multiplies right
operand with the left operand and assign the result to left
operand
Divide AND assignment operator, It divides left operand
with the right operand and assign the result to left operand
Modulus AND assignment operator, It takes modulus using
two operands and assign the result to left operand
Left shift AND assignment operator
Right shift AND assignment operator
Bitwise AND assignment operator
bitwise exclusive OR and assignment operator
bitwise inclusive OR and assignment operator
Example
C = A + B will assign value of A + B
into C
C += A is equivalent to C = C + A
C -= A is equivalent to C = C - A
C *= A is equivalent to C = C * A
C /= A is equivalent to C = C / A
C %= A is equivalent to C = C % A
C <<= 2 is same as C = C << 2
C >>= 2 is same as C = C >> 2
C &= 2 is same as C = C & 2
C ^= 2 is same as C = C ^ 2
C |= 2 is same as C = C | 2
Misc Operators
Operator
Description
sizeof()
&
Pointer to a variable.
?:
Conditional Expression
Example
sizeof(a), where a is integer, will
return 4.
&a; will give actual address of the
variable.
*a; will pointer to a variable.
If Condition is true ? Then value X :
Otherwise value Y
Description
if statement
if...else statement
nested if statements
switch statement
You can use one switch statement inside another switch statement(s).
if statement
An if statement consists of a boolean expression followed by one or more statements.
Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
Ans:
a is less than 20;
value of a is : 10
if...else statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
Ans: a is not less than 20;
value of a is : 100
nested if statements
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions
using single if...else if statement.
When using if , else if , else statements there are few points to keep in mind:
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.
expression 1 is true */
expression 2 is true */
expression 3 is true */
Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a == 10 )
{
/* if condition is true then print the following */
printf("Value of a is 10\n" );
}
else if( a == 20 )
{
/* if else if condition is true */
printf("Value of a is 20\n" );
}
else if( a == 30 )
{
/* if else if condition is true */
printf("Value of a is 30\n" );
}
else
{
/* if none of the conditions is true */
printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
return 0;
}
switch statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the
variable being switched on is checked for each switch case.
The expression used in a switch statement must have an integral or enumerated type, or be of a class type in
which the class has a single conversion function to an integral or enumerated type.
You can have any number of case statements within a switch. Each case is followed by the value to be compared
to and a colon.
The constant-expression for a case must be the same data type as the variable in the switch, and it must be a
constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will execute until a
break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line
following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent
cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default
case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
char grade = 'B';
switch(grade)
{
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}
Ans: Well done
Your grade is B