Professional Documents
Culture Documents
Programming - I (CSL106)
Contents
1)
2)
3)
4)
5)
6)
7)
8)
9)
Introduction to C
C Fundamentals
Data types and operators
I/O Operations
Decision Control Structure
Case Control Structure
Loops
Arrays
Functions
1. Introduction to C
C
is
a
general-purpose
structured
programming language
C was originally developed in the 1970s by
Dennis Ritchie at Bell Telephone Laboratories,
Inc.
It is an outgrowth of two earlier languages
BCPL and B
C can be used for systems programming as
well as application programming
2. C Fundamentals
1. The C Character set:
A simple C Program
#include<stdio.h>
main()
{
printf(welcome to C Programming\n);
}
The first statement uses # as a compiler directive and
instructs the compiler to take necessary actions to
handle input/output operations
Second line uses the keyword main() , which denotes
the starting point for execution of the program
printf is an output statement used to display any
message on the screen
Description
Memory
requirement
int
Integer quantity
2 bytes
char
single character
1 byte
float
floating-point number
4 bytes
double
double-precision
floating-point number
8 bytes
Types of operators
Arithmetic
Unary
Relational
Logical
Assignment
Conditional
Operators
1. Arithmetic operators
Operator
Purpose
addition
subtraction
multiplication
division
Operators contd.
2. Unary operators
Operator
Purpose
unary minus
++
increment
--
decrement
sizeof
Operators contd.
3. Relational operators
Operator
purpose
<
less than
<=
>
greater than
>=
==
equal to
!=
not equal to
Operators contd.
4. Logical operators
Operator
Purpose
&&
and
||
or
not
Operators contd.
5. Assignment operators
Operator
Example
a=b
+=
a += b equivalent to a = a +
b
-=
a -= b equivalent to a = a b
*=
a *= b equivalent to a = a *
b
/=
a /= b equivalent to a = a / b
%=
a %= b equivalent to a = a
%b
Operators contd.
6. Conditional operator
C offers a conditional operator (?:) that
stores a value depending upon a condition.
The operator is ternary operator as it
requires three operands.
Syntax:
Expression1 ? Expression2: Expression3
If expression1 evaluates to true i.e.1, then
the value of whole expression is the value of
expression2, otherwise, the value of the
whole expression is the value o0f the
exspression3.
Operator Precedence
Groups
Operator Category
Operators
Associativity
Unary
Arithmetic *, /, %
*, /, %
L to R
Arithmetic +, -
+, -
L to R
Relational
L to R
Equality
==, !=
L to R
Logical and
&&
L to R
Logical or
||
L to R
Conditional operator
?:
R to L
Assignment
gets()
puts()
scanf()
printf()
getchar()
putchar()
String
I/O
Formatt
ed I/O
Single
characte
r
4. I/O operations
syntax : ch = getchar();
2. putchar() is used to display a single character
syntax : putchar(ch);
else statement;
An example of one alternative:
if ( x != 0)
product = product * x;
Nested if Statements
Example :
if (experience >= 20)
if (salary > 1000000)
increment = 0.08 * salary;
else
increment = 0.06 * salary;
else
salary = 0.05 * salary;
Syntax :
case labeln:
statementn;
break;
default:
statementd;
}
Example
#include<stdio.h>
main()
{
int day;
scanf(%d, &day);
switch(day)
{
case 1: printf(Monday\n);
break;
case 2: printf(Tuesday\n);
break;
7. Loops
Repetitive execution of a set of statements in
for loop
Initialize the
control variable
Syntax :
check
Fals
the
e
for (initialization; test condition; step value)conditio
n
{
True
// body of the loop
Execute the
}
body of the
loop
Example: (to find 10!)
Increment/Decr
int factorial = 1;
ement the
for (i = 1; i < =10; i++)
control variable
factorial = factorial * i;
while loop
Syntax :
while (test condition)
{
// body of the loop
}
Example: (to find 10!)
int factorial = 1, i = 1;
while (i <= 10)
{
factorial = factorial * i;
i++;
}
check
Fals
the
e
conditio
n
True
Execute the
body of the
loop
do while loop
Syntax :
do
{
// body of the loop
}
while (test condition);
Example: (to find 10!)
int factorial = 1, i = 1;
do
{
factorial = factorial * i;
i++;
}
while (i <= 10);
Execute the
body of the
loop
check
Fals
the
e
conditio
n
True
8. Arrays
Array
c[0]
Group of consecutive memory locations
c[1]
Same name and type
c[2]
c[3]
c[4]
c[5]
c[6]
c[7]
c[8]
c[9]
c[10]
c[11]
3
6
0
72
21
-89
0
62
-3
1
90
78
\0
10 11
9. Functions
A function is a sub-unit of a program
An example
#include <stdio.h>
int maximum (int, int);
main()
{
int i= 4;
int j= 5;
int k;
k= maximum (i,j); /* Call maximum function */
printf ("%d is the largest between %d and %d\n",k,i,j);
}
void functions
A function doesn't have to take or return
Thank you