Professional Documents
Culture Documents
Structured Programming Language
Structured Programming Language
1. Introduction……..…………………………………….02
2. Operators in C Programming……………..…...08
3. Loops in C programming………………………….15
4. Arrays in C programming…………………………25
5. Switch case in C programming…………………31
6. Functions in C programming……………………36
7. Recursion in C programming……………………45
8. String in C programming………………………….50
9. Pointer in C programming……………………….57
10. Patterns Display…………………………………….63
Siyam
EEE 16th
Structured Programming Language (CSE113)
Lecture # 01
Introduction to C Programming
C Programming Language
C is a powerful general-purpose programming language. It is fast, portable and available in all platforms.
It is used for wide range of applications from Operating systems like Windows and iOS to software that
is used for creating 3D movies.
C programming is considered as the base for other programming languages, that is why it is known as
mother language.
It can be defined by following ways:
Mother language
System programming language
Procedure-oriented programming language
Structured programming language
Mid-level programming language
History of C Language
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T
(American Telephone & Telegraph), located in U.S.A.
C programming language features were derived from an earlier language called “B” (Basic
Combined Programming Language – BCPL)
C language was invented for implementing UNIX operating system
Comments in C:
Comments in C language are used to provide information about lines of code. It is widely used for
documenting code. Comments are explanatory notes for user. They are ignored by the compiler. There
are two types of comments:
1. Single line comment
Example: //Creator: Bulbul Ahamed
2. Multiple line comment
Example:
/* Creator: Bulbul Ahamed
Date: February 12, 2017 */
Variables in C:
A variable is a name of memory location. It is used to store data. Its value can be changed and it can be
reused many times.
The syntax to declare a variable:
type variable_list;
The example of declaring variable is given below:
int a;
float b;
char c;
Here, a, b, c are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
int a = 10, b = 20; //declaring 2 variable of integer type
float f = 20.8;
char c = 'A';
3 Bulbul Ahamed, Associate Professor of CSE, SU www.bulbulcse.com
Rules for defining variables
A variable can have alphabets, digits and underscore.
A variable name can start with alphabet and underscore only. It can't start with digit.
No white space is allowed within variable name.
A variable name must not be any reserved word or keyword e.g. int, float etc.
Valid variable names: Invalid variable names:
int a; int 2;
int _ab; int a b;
int a30; int long;
Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Local Variable : A variable that is declared inside the function or block is called local variable.
void main() {
int x = 10; //local variable
}
You must have to initialize the local variable before it is used.
Global Variable: A variable that is declared outside the function or block is called global variable. Any
function can change the value of the global variable. It is available to all the functions.
int value = 20; //global variable
void main () {
int x = 10; //local variable
}
Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating, character etc.
There are 4 types of data types in C language.
……………………..
#include<stdio.h>
int main()
{
int x = 13, y = 6, z;
z = x / y; // division
printf("Division = %d\n", z);
z = x % y; // modulus
printf("Modulus = %d\n", z);
return 0;
}
Output:
Source Code:
#include<stdio.h>
int main()
{
int x = 13;
printf("%d\n", x--); //13 but in memory 12
printf("%d\n", x); //12
printf("%d\n", --x); //11
printf("%d\n", x);//11
return 0;
}
Output:
Q3. Write a C program to display You are an adult if the value of age is 18 or more,
otherwise it will display You are a child.
Source Code:
#include<stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d",&age);
if(age >= 18)
printf("You are an adult. Your age is %d\n",age);
else
printf("You are a child. Your age is %d\n",age);
return 0;
}
Output:
4. Assignment Operators
The following table lists the assignment operators supported by the C language –
Operator Description Example
Simple assignment operator. Assigns values C = A + B will assign the value of
=
from right side operands to left side operand A + B to C
Add AND assignment operator. It adds the
+= right operand to the left operand and assign C += A is equivalent to C = C + A
the result to the left operand.
Subtract AND assignment operator. It
subtracts the right operand from the left
−= C -= A is equivalent to C = C - A
operand and assigns the result to the left
operand.
Multiply AND assignment operator. It
multiplies the right operand with the left
*= C *= A is equivalent to C = C * A
operand and assigns the result to the left
operand.
Divide AND assignment operator. It divides
/= the left operand with the right operand and C /= A is equivalent to C = C / A
assigns the result to the left operand.
Modulus AND assignment operator. It takes
%= modulus using two operands and assigns the C %= A is equivalent to C = C % A
result to the left operand.
5. Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The following table lists the
bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then −
1. for Loop
The for loop is commonly used when the number of iterations is known.
The syntax of for loop is:
for(initialization; condition ; increment/decrement)
{
statement-block;
}
#include<stdio.h>
int main()
{
int i,n;
printf("Enter any numebr: ");
scanf("%d",&n); //Input upper limit from user
for(i = 1; i <= n; i++)
printf("%d\t",i);
return 0;
}
Output:
Source Code:
#include<stdio.h>
int main()
{
int i,n;
printf("Enter any numebr: ");
scanf("%d",&n); //Input upper limit from user
for(i = 1; i <= n; i = i + 2)
printf("%d\t",i);
return 0;
}
Output:
#include<stdio.h>
int main()
{
int i,n;
printf("Enter any numebr: ");
scanf("%d",&n); //Input upper limit from user
for(i = 2; i <= n; i = i + 2)
printf("%d\t",i);
return 0;
}
Output:
#include<stdio.h>
int main()
{
char i;
for(i = 'A'; i <= 'Z'; i = i + 1)
printf("%c\t",i);
return 0;
}
Output:
Source Code:
#include<stdio.h>
int main()
{
int i;
for(i = 65; i <= 90; i = i + 1)
printf("%c\t",i);
return 0;
}
Output:
while loop iterates the code until condition is false. Here, condition is given before the
code. So code may be executed 0 or more times.
It is better if number of iteration is not known by the user.
#include<stdio.h>
int main()
{
int i = 1,n;
printf("Enter any number: ");
scanf("%d",&n);
while(i <= n){
printf("%d\t",i);
i++;
}
return 0;
}
Output:
do{
//code to be executed
} while(condition);
As per the above illustration, following are the important points to be considered.
Array length = 10, First Index = 0, Last Index = 9
Index starts with 0.
Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 27.
Basic Operations
Following are the basic operations supported by an array.
Traverse − print all the array elements one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by the value.
Update − Updates an element at the given index.
int main()
{
int a[MAX] = {7,2,3,9,5};
int i;
for(i = 0; i < MAX; i++)
printf("a[%d] = %d\n",i,a[i]);
return 0;
}
Source Code:
#include<stdio.h>
main()
{
int avg, i, sum=0;
int marks[5] ; //array declaration
for ( i = 0 ; i < 5 ; i++ )
{
printf ( "Enter marks: " ) ;
scanf ( "%d", &marks[i] ) ; //store data in array
}
for ( i = 0 ; i < 5 ; i++ )
sum = sum + marks[i] ; //read data from an array
avg = sum / 5 ;
printf ( "\nAverage marks = %d\n", avg ) ;
}
Output:
Source Code:
#include<stdio.h>
int main()
{
int a[100];
int i,n;
printf("Enter total number of elements(1 to 100): ");
scanf("%d",&n);
// Stores number entered by the user
for(i = 0; i < n; i++){
printf("Enter number %d: ",i+1);
scanf("%d",&a[i]);
}
// Loop to store largest number to arr[0]
for(i = 0; i < n; i++){
if(a[0] < a[i]){
a[0] = a[i];
}
}
printf("Largest Element is: %d\n",a[0]);
}
Output:
int main()
{
int a[3][2] = { {10,20}, {30,40}, {50,60} };
int i,j;
int main()
{
int a[3][2] = { {10,20}, {30,40}, {50,60} };
int i,j;
Source Code:
#include<stdio.h>
int main()
{
int a[3][2] = { {5,0}, {1,2}, {2,3} };
int b[3][2] = { {1,2}, {2,3}, {3,6} };
int c[3][2];
int i, j;
Output:
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.
• Switch statement is used to solve multiple option type problems for menu like program,
where one value is associated with each option.
• Switch case statements mostly used when we have number of options (or choices) and we
may need to perform a different task for each choice.
Syntax of switch...case
switch(n)
{
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
Break statement
Break statements are useful when you want your program-flow to come out of the switch
body. Whenever a break statement is encountered in the switch body, the execution flow
would directly come out of the switch.
3) The expression provided in the switch should result in a constant value otherwise it
would not be valid.
4) Nesting of switch statements are allowed, which means you can have switch statements
inside another switch. However nested switch statements should be avoided as it makes
program more complex and less readable.
Uses of C functions:
• Used to avoid rewriting same logic/code again and again in a program.
• There is no limit in calling C functions to make use of same functionality wherever
required.
• We can call functions any number of times in a program and from any place in a
program.
• A large C program can easily be tracked when it is divided into functions.
• The core concept of C functions are re-usability, dividing a big task into small pieces to
achieve the functionality and to improve understandability of very large C programs.
int main()
{
add(); // function call
return 0; output:
}
//function definition
void add()
{
int a,b,sum;
printf("Enter the value of a & b : ");
scanf("%d %d",&a,&b);
sum = a + b;
printf("a + b = %d\n",sum);
}
int main()
{
int a,b;
printf("Enter the value of a & b : "); output:
scanf("%d %d",&a,&b);
add(a,b); // function call
return 0;
}
//function definition
void add(int x, int y)
{
int sum;
sum = x + y;
printf("a + b = %d\n",sum);
}
4. C function without arguments and with return value
Example: Add Two Numbers
Source Code:
#include<stdio.h>
int add(); //function declaration
int main()
{
int k;
k = add(); // function call
printf("a + b = %d\n",k); output:
return 0;
}
//function definition
int add()
{
int a,b, sum;
printf("Enter the value of a & b : ");
scanf("%d %d",&a,&b);
sum = a + b;
return sum;
}
Source Code:
#include<stdio.h>
int main()
{
swap(); //function call
return 0;
}
//function definition
void swap(){
int x, y, temp;
temp = x;
x = y;
y = temp;
Output:
a) Source Code:
#include<stdio.h>
int main()
{
swap();//function call
return 0;
}
//function definition
void swap(){
int x,y;
Output:
int main()
{
swap();//function call
return 0;
}
//function definition
void swap(){
int x,y;
Output:
int main()
{
swap();//function call
return 0;
}
//function definition
void swap(){
int x,y;
Output:
#include<stdio.h>
void square();
void area();
int main(){
square();
area();
}
A function that calls itself is known as a recursive function. And, this technique is
known as recursion.
Recursive function is very useful to solve many mathematical problems like to
calculate factorial of a number, generating Fibonacci series, etc.
To prevent infinite recursion, if...else statement (or similar approach) can be used where
one branch makes the recursive call and other doesn't.
return 0;
}
Note: In order to prevent infinite recursive call, we need to define proper exit condition in
a recursive function.
int main()
{
printf("Hello world");
main();
return 0;
}
In this program, we are calling main() from main() which is recursion. But we haven't
defined any condition for the program to exit. Hence this code will print "Hello world"
infinitely in the output screen.
printf("%s\n",str);
printf("%s\n",str1);
return 0;
}
Example: Write a C program to read & write your full name using gets() and puts()
Source Code Output
#include<stdio.h>
int main()
{
char str[100];
printf("Enter your full name: ");
gets(str);
printf("Full Name: ");
puts(str);
}
▪ strcmp() function in C compares two given strings and returns zero if they are same.
Negative value is returned when str1 < str2 and positive value is returned when str1 >
str2.
▪ strcmp() function is case sensitive. i.e, “A” and “a” are treated as different characters.
Example: Write a C program to compare two string whether they are same or not
Output
Output
4. Write a C program to count alphabets, digits, spaces & special characters in a string.
Uses of isalpha(), isdigit() & isspace()
Source Code Output
#include<stdio.h>
int main(){
char str[100];
int alpha = 0, digit = 0, space=0, special = 0;
gets(str);
int i = 0;
while(str[i] != '\0'){
if(isalpha(str[i]) != 0 ){
alpha++;
} You can also write the following in while:
else if(isdigit(str[i]) != 0){
digit++; while(str[i] != '\0'){
} if(isalpha(str[i])){
else if(isspace(str[i]) != 0){ alpha++;
}
space++;
else if(isdigit(str[i])){
}
digit++;
else{ }
special++; else if(isspace(str[i])){
} space++;
i++; }
} else{
printf("Alphabet(s): %d\n", alpha); special++;
printf("Digit(s): %d\n", digit); }
printf("Space(s): %d\n", space); i++;
printf("Special character(s): %d\n", special); }
return 0;
}
strlen() Return value: This function returns the length of string passed.
strcmp() Return value: This function return values that are as follows −
• if Return value = 0 then it indicates str1 is equal to str2.
• if Return value < 0 then it indicates str1 is less than str2.
• if Return value > 0 then it indicates str2 is less than str1.
Thank You☺
What is a pointer?
A pointer is a variable that stores the address of another variable. Pointers are used in C
program to access the memory and manipulate the address.
Pointer Syntax:
data_type *pointer_variable_name;
Example: int *p; char *p;
Where, * is used to denote that “p” is pointer variable and not a normal variable.
Address in C:
If you have a variable var in your program, &var will give you its address in the memory, where
& is commonly called the reference operator. The & operator is also known as “Address of”
Operator.
Source Code Output
#include <stdio.h>
int main()
{
int var = 10;
printf("Value: %d\n", var);
printf("Address: %x\n", &var);
return 0;
}
Note: You may obtain different value of address while using this code.
10 Value of var
60ff0c Address of var
c = 22;
printf("Address of c: %x\n", &c);
printf("Value of c: %d\n\n", c);
pc = &c;
printf("Address of pointer pc: %x\n", pc);
printf("Content of pointer pc: %d\n\n", *pc);
c = 11;
printf("Address of pointer pc: %x\n", pc);
printf("Content of pointer pc: %d\n\n", *pc);
*pc = 2;
printf("Address of c: %x\n", &c);
printf("Value of c: %d\n\n", c);
return 0;
}
1. int* pc, c;
This assigns 22 to the variable c, i.e., 22 is stored in the memory location of variable c.
Note that, when printing &c (address of c), we use %x rather than %d since address is
usually expressed as a hexadecimal number. We can also use %u unsigned integer
(always positive).
3. pc = &c;
4. c = 11;
5. *pc = 2;
Output
Thank You☺
6 Bulbul Ahamed, Associate Professor, SU www.bulbulcse.com
Structured Programming Language (CSE113)
Lecture # 10
Patterns Display in C Programming
Create a program to display the following cases using for loops:
case 1: case 2: case 3: case 4:
1 1 2 3 4 5 1 5 4 3 2 1
1 2 1 2 3 4 2 1 4 3 2 1
1 2 3 1 2 3 3 2 1 3 2 1
1 2 3 4 1 2 4 3 2 1 2 1
1 2 3 4 5 1 5 4 3 2 1 1
…………………………………….