You are on page 1of 35

Fundamentals of Computer

Programming - I (CSL106)

Dr. A. Charan Kumari


Department of CSE & IT
The NorthCap University
Gurugram

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:

C uses upper case letters A to Z, the lower case letters


a to z, the digits 0 t0 9 and certain special character s
as building blocks to from basic program elements
2. Identifiers:
Identifiers are names that are given to various program
elements, such as variables, functions and arrays
3. Keywords:
Keywords are the reserved words that have standard,
pre-defined meaning and are used for their intended
purpose

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

3. Data types and


Operators
Data type

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

remainder after integer


division

Operators contd.
2. Unary operators
Operator

Purpose

unary minus

++

increment

--

decrement

sizeof

returns sizeof its


operand in bytes

Operators contd.
3. Relational operators
Operator

purpose

<

less than

<=

less than or equal to

>

greater than

>=

greater than or equal to

==

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

-, ++, --, !, sizeof R to L

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

=, +=, -=, *=, /= R to L


, %=

gets()
puts()
scanf()
printf()
getchar()
putchar()

String
I/O

Formatt
ed I/O

Single
characte
r

4. I/O operations

I/O operations contd.


1. Single Character I/O
1. getchar() is used to input a single character

syntax : ch = getchar();
2. putchar() is used to display a single character
syntax : putchar(ch);

I/O operations contd.


2. Formatted I/O
1. scanf():
syntax : scanf(control string, arg1, arg2,
.argn);
Example : scanf(%d %f %c, &no, &value,
&ch);
2. printf():
syntax : printf(control string, arg1, arg2,
., argn);
Example : printf(%d %f %c, no, value,

I/O operations contd.


3. String I/O
1. gets():
syntax : gets(string_variable);
Example : char str[20];
gets(str);
2. puts():
syntax : puts(string_variable);
Example : char str[20] = Welcome 2 C;
puts(str);

5. Decision Control structure - The if


Statement
Syntax: if (condition) statement;

else statement;
An example of one alternative:
if ( x != 0)
product = product * x;

An example of two alternatives:


if ( x%2 == 0)
printf(x is even);
else
printf(x is odd);

Nested if Statements

Nested if statement is an if statement with another

if statement as its true task or false task.

Example :
if (experience >= 20)
if (salary > 1000000)
increment = 0.08 * salary;
else
increment = 0.06 * salary;
else
salary = 0.05 * salary;

6. Case control structure - The


switch Statement

The switch statement is used to select one of several

alternatives when the selection is based on the value of a


single variable or an expression.

Syntax :

switch (controlling expression) {


case label1:
statement1;
break;
case label2:
statement2;
break;

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;

default : printf(Invalid input\n);


}
}

7. Loops
Repetitive execution of a set of statements in

a program is known as a Loop


Types of loop constructs
for
while
do while

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]

To refer to an element specify


Array name
Position number
Format:
arrayname[ position number ]

c[3]
c[4]
c[5]
c[6]
c[7]
c[8]
c[9]
c[10]
c[11]

First element at position 0


Last element position n-1 (with size n)
27

3
6
0
72
21
-89
0
62
-3
1
90
78

One dimensional arrays


Syntax:
datatype arrayname [size]
Example : (to find average of n numbers)
int a[20], i, sum = 0, n;
float avg;
scanf(%d, &n);
for(i = 0; i < n ; i++)
{
scanf(%d, &a[i]);
sum += a[i];
}
avg = sum/n;

Two dimensional arrays


Syntax:
datatype arrayname [rowsize][columnsize]
Example : (to find sum of even numbers in an m x n matrix)
int a[20][20], i, j, sum = 0, n;
scanf(%d, &n);
for(i = 0; i < m ; i++)
for (j = 0; j < n; j++)
{
scanf(%d, &a[i][j]);
if (a[i][j] % 2 == 0)
sum += a[i];
}

Character array - String


A string can be represented as a one-

dimensional character-type array.


The last character in a string is always \0, a
null character
Syntax :
char variablename[size];
Example :
char str[] = PROGRAMMING;
P

\0

10 11

9. Functions
A function is a sub-unit of a program

which performs a specific task.


Functions take arguments (variables)
and may return an argument.

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);
}

int maximum (int a, int b)


/* Return the largest integer */
{
if (a > b)
return a;
return b;

Functions can access other


functions
Once you have written a function, it can be accessed

from other functions. We can therefore build more


complex functions from simpler functions
int max_of_three (int, int, int);
/* Main and rest of code is in here */
int max_of_three(int i1, int i2, int i3)
/* returns the maximum of three integers */
{
return(maximum (maximum(i1, i2), i3));
}

void functions
A function doesn't have to take or return

arguments. We prototype such a function


using void.

void print_hello (void);

void print_hello (void)


/* this function prints hello */
{
printf ("Hello\n");
}

Thank you

You might also like