C is a powerful, flexible and portable programming language created by Dennis Ritchie in 1972. It supports various data types and has a variety of operators to perform arithmetic, relational, logical and bitwise operations. C programs are compiled into machine-independent code that can run on a variety of hardware and operating system platforms. Key features of C include structured programming, functions, arrays and pointers.
1. C
C is a powerful,flexible,portable and elegantly
structured programming language.
The creation of c programming language is
credited to Dennis Ritchie(sep.1941-oct.2011)
2. History of C
C is a programming language.
C is a structured, high level language,
Machine independent language.
C was evolved from ALGOL,BCPL and B by
Dennis Ritchie at the Bell Laboratories in
1972.
C is running under a variety of operating
system and hardware platforms.
3. IMPORTANCE OF C
Programs written in c are efficient and fast.
This is due to its variety of datatypes and
powerful operators.
Several standard functions are available.
C is highly portable.
C language is well suited for structured
programming.
4. General Form
main() /* Function name
{ /* Start of program
------------------
------------------ /* program statements
------------------
} /*End of program
6. Main Function
main()
int main()
void main()
main(void)
void main(void)
int main(void)
7. Sample program 2: ADDING Two Numbers
/* Program ADDITION
main()
{
int number;
float amount;
number=100;
amount=30.75+75.35;
printf(“ %d n ”, number);
printf(“%5.2f”,amount);
}
OUTPUT
100
106.10
8. Use of math function
#include<math.h>
#define PI 3.14
#define MAX 50
Main()
{
int angle;
Float x,y;
angle=0;
printf (“ angle Cos(angle)nn”);
While(angle<=MAX)
{
x=(PI/MAX)*angle;
y=cos(x);
Printf(“%15d %13.4fn”,angle,y);
10. Basic structure of c programs
Documentation Section
Link Section
Definition Section
Global Declaration Section
Main() function Section
{
}
Declaration Part
Executable Part
05-09-2017
17. • Every C word is classified as either a keyword
or an identifier .
• All keywords have fixed meanings and these
meaning cannot be changed.
• Keywords serve as basic building blocks for
program statements.
18. auto double int struct
Break
Case
Char
Const
Continue
Default
do
Else
Enum
Extern
Float
For
Goto
if
Long
Register
Return
Short
Signed
Sizeof
static
Switch
Typedef
Union
Unsigned
Void
Volatile
while
ANSI C Keyword
19. CONSTANTS
Constants in C refers to fixed values that do not
changes during the execution of a program.
constants
Numeric constants
Character constants
Integer
constants
Real
constants
Single
character
constant
String constants
20. Integer constants
An Integer constant refers to a sequence of digits.
There are three types of integer,namely
I. Decimal integer
ii. Octal integer
iii. Hexadecimal integer
21. Decimal integer
Decimal integers consists of a set of digits,0
through 9,preceded by an optional – or + sign.
Eg:
123 -321 0 654321 +78
Octal integer:
An octal integer constants consists of any
combination of digits from the 0 through 7,with a
leading 0.
Eg:
037 0 0435 0551
22.
23. Real constant
Integer numbers are inadequate to represent
quantities that vary continuously, such as distances,
heights,temperatures,prices,and so on.
These quantities are represented by numbers
containing fractional parts like 17.548.such numbers
are called Real(or floating point)constants.
Eg:
0.0083 -0.75 435.36+247.0
24. SINGLE CHARACTER CONSTANTS
A single character constant(or simply character
constant)contains a single character enclosed within a
pair of single quotes marks.
Eg:
‘5’ ‘X’ ‘;’ ‘’
25. String Constants
A string constants is a sequence of characters enclosed
in double quotes.
The characters may be letters,numbers,special
characters and blank spaces.
Eg:
“Hello” “1987” “WELLDONE” “?....1” “5+3” “X”
26. Backslash character constants
C supports some special backslash character constants
that are used in output function.
Eg:
The symbol ’n’ stands for newline character.
27. BACKSLASH CHARACTER CONSTANTS
Constant Meaning
‘b’
‘n’
‘r’
‘t’
‘”
‘’”
‘?’
‘’
‘10’
Back space
New line
Carriage return
Horizontal tab
Single quote
Double quote
Question mark
Back slash
Null
28. VARIABLES
A variable is a data name that may be used to store
a data value.
A variable name can be chosen by the programmer
in a meaningful way so as to reflect its function or
nature in the program.
Eg:
Average
Height
Total
Counter_1
29. Variables names may consists of letters,digits,and
underscore(_)character,subject to the following conditions:
They must begin with a letter. Some system permit
underscore as the first character.
ANSI standard recognizes a length of 31
characters.However,length should not be normally
more than eight character,since only the first eight
characters are treated as significant by many
compliers.
Uppercase and lowercase are significant.That is,the
variable Total is not the same as total or TOTAL.
It should not be a keyword.
White space is not allowed.
30.
31. C language is rich in its data types.
ANSI C support three classes of data types
I, Primary(or fundamental)data types.
ii, Derived data types
iii, User defined data types.
All C compilers support five fundamental data types,
namely
I, integer ( int)
ii, character(char)
iii,floating point(float)
iv,double precision floating point(double)
v,void
32. INTEGER TYPES
Integer are whole numbers with a range of values
supported by a particular machine.
C has three classes of integer storange,namely
I,short int
ii,long
iii,int
Short int
int
Long int
33. Floating point types
Floating point(or real)number are stored in 32 bits (on
all 16 bit and 32 bit machines),with 6 digits of
precision.
Floating oint numbers are defined in C by the keyword
float.
float
double
Long double
34. VOID TYPES
The Void type has no values.This is usually
used to specify the type of function.
The type of a function is said to be void when
it does not return any value to the calling
function.
35. CHARACTER DATATYPES
A single character can be defined as a
Character(char)type data.
Characters are usually stored in 8 bits (one byte)of
internal storage.
The qualifier Signed or unsigned may be explicitly
applied to char.
While unsigned chars have between 0 and
255,signed chars have values from -128 to 127
36. DECLARATION OF VARIABLES
After designing suitable variable names,we must
declare them to the complier.Declaration does two
things:
i.Its tells the complier what the variable name is.
ii, its specifies what type of data the variable will
hold.
The declaration of variables must be done before they
are used in the program.
37. PRIMARY TYPE DECLARATION
A variable can be used to store a value of any data
type.That is,the name has nothing to do with its type.
Syntax:
data-type v1,v2,…….vn;
v1,v2…..vn are the names of variables. Variables are
separated by commas.
A declaration statement must end with a semicolon.
Eg;
Int count;
Int number,total;
double ratio;
38. Declaration of variables
Main() /*……program name…….*/
{
/*……….Declaration……….*/
Float x,y;
Int code;
Short int count;
Long int amount;
Double deviation;
Unsigned n;
Char c;
/* ………computation…………..*/
………..
………..
39. DECLARATION OF STORAGE CLASS
Storage class that provides information about
their location and visibility.
The storage class decides the portion of the
program within which the variables are
recognized.
40. /* Example Of Storage Class*/
Int m;
Main()
{
int i;
Float balance;
……..
……..
Function1()
}
Function1()
{
int i;
Float sum;
………
………
}
41. STORAGE CLASSES AND THEIR
MEANING
Storage
class
Meaning
Auto
Static
Extern
register
Local variable known only to the function in
which it is declared. Default is auto.
Local variable which exists and retains its value
even after the control is transferred to the calling
function.
Global variable known to all functions in the file.
Local variable which is stored in the register.
42. ASSIGNING VALUES TO VARIABLES
Variables are created for use in program statements such
as
value=amount+inrate*amount;
while (year<=PERIOD)
{
……..
……..
Year=year+1;
}
43. program
Main()
{
Float x ,p;
Double y , q;
Unsigned k;
int m=54321;
Long int n=1234567890;
X=1.234567890000;
Y=9.87654321;
K=54321;
P=q=1.0;
printf(“m=%dn”,m);
printf(“n=%dn”,n);
printf(“x=%.121fn”,x);
printf(“x=%fn”,x);
printf(“y=%.121fn”,y);
printf(“y=%1fn”,y);
printf(“k=%u p=%f q=%.121fn”,k,p,q);
}
45. READING DATA FROM KEYBORAD
Another way of giving values to variables is to input
through keyboard using the scanf
its works much like an INPUT statement in BASIC.
The general format of scanf is as follows:
scanf(“control string”,&variable1,&variable2,….);
46. PROGRAM
Main()
{
int number;
printf (“Enter an integer numbern”);
Scanf(“%d”,&number);
If(number<100)
printf (”Your number is smaller than 100nn”);
else
printf(“your number contains more than two digitsn”);
}
47. OUTPUT
Enter an integer number
54
Your number is smaller than 1000
Enter an integer number
108
Your number contains more than two digits
48.
49. Introduction
An operator is a symbol that tells the computer to
perform certain mathematical or logical manipulation.
Operators are used in programs to manipulate data
and variables.
They usually form a part of the mathematical or logical
Expression.
50. C operators can be classified into a number of
categories.They include
1.Arithmetic operators.
2.Relational operators.
3.Logical operators.
4.Assignment operators.
5.Increment and decrement operators.
6.Conditional operators.
7.Bitwise operators.
8.Special operators.
51. Arithmetic operators
C provides all the basic arithmetic operators.
These can operate on any built in datatype allowed in
C.
Operator Meaning
+
-
*
/
%
Addition or unary plus
Subtraction or unary minus
Multiplication
Division
Modulo division
52. Types of arithmetic operators
1. Integer Arithmetic
2. Real Arithmetic
3. Mixed Mode Arithmetic
53. Integer Arithmetic
When both the operands in a single arithmetic
expression such as a+b are integer,the expression is
called an integer expression,
And the operation is called integer arithmetic. a=14
b=4
a-b=10
a+b=18
a*b=56
a/b=3
a%b=2
56. Real Arithmetic
An arithmetic operation involving only real operands is
called real arithmetic.
A real operand may assume values either in decimal or
exponential notation.
X=6.0/7.0=0.857143
y=1.0/3.0=0.333333
z=-2.0/3.0=-0.666667
The operator % cannot be used with real operands.
57. MIXED –MODE ARITHMETIC
When one of the operands is real and the other is
integer,the expression is called a mixed –mode
arithmetic expression.
15/10.0=1.5
Whereas
15/10=1
58. RELATIONAL OPERATOR
We may compare the age of two persons ,or the price of two items and so on.
These Comparisons can be done with the help of relational operators.
An expression such as
a<b or 1<20
Containing a relation operator is termed as a reletional expression.
The value of a relation expression is either one or zero.its one if the specified
relation is true and zero If the relation is false.
10<20 is true
But
20<10 is false
60. LOGICAL OPERATORS
C has the following three logical operators
&& meaning logical AND
|| meaning logical OR
! Meaning logical NOT
61. Truth table
Op-1 Op-2 Value of the expression
Op-1&&op-2 Op-1||op-2
1
1
0
0
1
0
1
0
1
0
0
0
1
1
1
0
62. ASSIGNMENT OPERATORS
Assignment operators are used to assign the result of an
expression to a variable.
Assignment operator is ,==.
C has a set of ‘shorthand’ assignment operators of the
form
v op= exp;
Where V is a variable ,exp is an expression and
op is a C binary arithmetic operator.
The operator op= is known as the shorthand
assignment operator.
66. INCREMENT AND DECREMENT OPERATORS
C allows two very useful operators not generally
found in other languages.
These are the increment and decrement operators;
++ and –
Eg;
++m; or m++
-- m; or m—
67. CONDITIONAL OPERATORS
A ternary operator pair ”?:” is available in C to
construct conditional expressions of the form
exp1?exp2:exp3
where exp1,exp2 and exp3 are expression
Eg
a=10;
b=15;
x=(a>b)?a:b;
68. BITWISE OPERATORS
C has a distinction of supporting special
operators known as bitwise operator for
manipulation of data at bit level.
Bitwise operators may not be applied to float or
double.
70. SPECIAL OPERATORS
C supports some special operators of interest
such as comma operator,Sizeof
operator,pointer operator (& and *) and
member selection operators.(.and ->).
71. The comma operator
The comma operator can be used to link the related
expression together.
A comma-linked list of expressions are evaluated left
to right and the value of right-most expression is the
value of the combined expression.
Eg:
value =(x=10,y=5,x+y);
72. THE SIZEOF OPERATOR
The sizeof is a compile time operator and ,when used
with an operand,it returns the numberof bytes the
operand occupies.
Eg:
m=sizeof(sum);
n=sizeof(long int);
k=sizeof(235L);
73. ARITHMETIC EXPRESSION
An arithmetic expression is a combination of
variables,constants,and operators arranged as
per the syntax of the language.
78. PRECEDENCE OF ARITHMETIC OPERATORS
An expression without parentheses will be evaluated
from left to right using the rules of precedence of
operators.
The are two distinct priority levels of arithmetic
operators in C.
High priority */%
Low priority +-
79.
80. C language possesses such decision-making capabilities by
supporting the following statements.
1. if statement
2. switch statement
3. conditional operator statement
4. goto statement
These statement are popularly known as decision –
making statements.
81. IF STATEMENT
The if statement is a powerful decision –making
statement and is used to control the flow of execution of
statements.
General form:
if(test expression)
83. THE DIFFERENT FORMS ARE
I. Simple if statement
II. if…..else statement
III. Nested if…..else statement
IV. Else if ladder
84. SIMPLE IF STATEMENT
The general form of a simple if statement is
if(test expression)
{
statement-block;
}
statement-x;
85. EXPLAIN
The ‘statement-block’ may be single statement or
group of statements or a group of statements.
if the test expression is true,the statement-block will
be executed.
otherwise the statement-block will be skipped and the
execution will jump to the statement-x
86. FLOW CHART OF SIMPLE IF CONTROL
entry
true
false
Test
exp?
Statement-
block
Statement-x
Next statement
87. PROGRAM
main()
{
int a,b,c,d;
float ratio;
printf (“Enter four integer valuesn”);
scanf (“%d%d%d%d”,&a,&b,&c,&d);
if(c-d!=0)/*Execute statement block */
{
ratio=(float)(a+b)/(float)(c-d);
printf(“Ratio=%fn”,ratio);
}
}
89. THE IF……ELSE STATEMENT
The if…else statement is an extension of the simple if
statement.
The general form is
if(test expression)
{
True-block statement(s)
}
else
{
false-block statement(s)
}
Statement-x
90. If the test expression is ture block
satement(s),immediately following the if
statements are executed.
Otherwise,the false-block statement(s) are
executed.
In their case,either ture-block or false-block
will be excecuted,not both.
In both the cases,the control is transferred
subsequently to the statement-X.
91. Flow chart of if ….else control
entry
true falseTest
exp?
True block
statement
False block
statement
Statement-X
92. Nesting of if….else statements
when a series of decisions are involved,we may have to use more than one
if…else statement.
General form:
if(test condition-1)
{
if(test condition-2);
{
statement-1;
}
else
{
statement-2;
}
}
else
{
Statement-3;
}
98. The switch statement
General form
Switch(expression)
{
Case value-1:
block-1;
break;
Case value-2:
block-2
break;
Default:
default-block
break;
}
statement-x;
100. The ?: operator
This operator is a combination of ? and :, and takes
three operands.
This operator is popularly known as the Conditional
operator.
General form:
conditional expression?expresssion1:expression2
The conditional expression is evaluated first.
102. LOOP
A looping process,in general,would include the
following four steps:
Setting and initialization of a condition variable.
Execution of the statements in the loop.
Test for a specified value of the condition variable
for execution of the loop.
Incrementing or updating the condition variable.
103. The c language provides for three constructs for performing loop
operations.
They are:
i. The while statement
ii. The do statement
iii. The for statement
104. THE WHILE STATEMENT
The simplest of all the looping structures in C is the
while statement.
General form:
While (test condition)
{
body of the loop
}
105. The while is an entry controlled loop statement.
The test-condition is avaluated and if the condition is
true,then the body of the loop is executed.
After the execution of the body ,the test condition is once
again evaluated and if its true,the body is executed again.
106. program
main()
{
int count ,n;
float x,y;
printf(“Enter the values of x and n:”);
scanf(“%f%d”,&x and &n);
y=1.0;
count=1;
while(count<=n)
{
y=y*x;
count++;
}
Printf(“nx=%f;n=%d;x to power n=%fn”,x,n,y);
}
107. OUTPUT
Enter the values of x and n:2.5 4
x=2.500000;n=4; x to power n=39.062500
Enter the values of x and n: 0.5 4
x=0.500000;n=4; x to power n=0.062500
111. THE FOR LOOPS
The for loop is another entry-controlled loop that
provides a more concise loop control structure
GENERAL MODEL
for(initialization;test-condition;increment)
{
body of the loop
}
114. INTRODUCTION
An array is a fixed –size sequenced
collection of elements of the same data
types.
An array is a sequenced collection of
related data items that share the common
name.
115. TYPES OF ARRAY
1. One dimensional arrays
2. Two dimensional arrays
3. Multidimensional arrays
116. One dimensional arrays
A list of items can be given one variable name using only one
subscript and such a variable is called a Single subscripted
variable or a one dimensional array.
Eg
int number[5];
And the computer reserves five storage location as shown
below:
number[0]
number[1]
number[2]
number[3]
number[4]
117. The values to the array elements can be assigned
as follows :
Number[0]=35;
Number[1]=40;
Number[2]=20;
Number[3]=57;
Number[4]=19;
118. This would causes the array number to store
the values as shown below
number[0]
number[1]
number[2]
number[3]
number[4]
35
40
20
57
19
119. DECLARATION OF ONE DIMENTIONAL ARRAY
Array must be declared before they are used so that the
compiler can allocate space for them in memory.
The general form of array declaration is
type variable-name[size];
The type specifies the type of element that will be
contained in the array,such as int,float,or char and the
size indicates the maximum number of elements that
can be stored in the array.
Eg
float height[50];
122. Initialization of one –dimensional
array
after an array is declared ,its elements must be
initialized.
At compile time
At run time
123.
124. Compile time initialization
We can initialize the elements the of arrary in the
same way as the ordinary variables when they are
declared.
The general form:
type array-name[size]={list of values};
The values in the list are separated by commas.
Eg
int number[3]={0,0,0};
125. Run time initialization
an arrar can be explicitly initialized at run time.
This is approach usually applied for initializing large arrays.
eg
for(i=0;i<100;i+1)
{
if i<50
sum[i]=0.0;
else
sum[i]=1.0;
}
126. Two dimensional arrays
C allows us to define such tables of items by
using two dimensional arrays.
Two dimensional arrays are declared as
follows:
type array
_name[row_size][column_size];
127. INITIALIZING TWO DIMENSIONAL ARRAY
Two-dimensional array may be initialized by
following their declaration with a list values
enclosed in braces.
Eg:
int table [2][3]={0,0,0,1,1,1};
The initialization is done row by row,
int table[2][3]={{0,0,0},{1,1,1}};
128. Multi -dimensional arrays
C allows array of three or more dimensions.The exact
limit is determined by the compiler.
The general form is:
type array_name[s1][s2][s3]…[sm];
sᵢ is the size of the i dimension
Eg:
int survey[3][5][12];
float table[5][4][5][3];