0% found this document useful (0 votes)
24 views

C programing ppt

The document outlines the fundamentals of computer programming and applications, focusing on algorithm and program development, including steps like problem definition, analysis, design, coding, testing, and maintenance. It introduces various programming languages, particularly C, and explains concepts such as machine language, assembly language, and high-level languages, along with their advantages and applications. Additionally, it covers the structure of C programming, including syntax, keywords, identifiers, constants, and operators.

Uploaded by

Rohilla shivam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C programing ppt

The document outlines the fundamentals of computer programming and applications, focusing on algorithm and program development, including steps like problem definition, analysis, design, coding, testing, and maintenance. It introduces various programming languages, particularly C, and explains concepts such as machine language, assembly language, and high-level languages, along with their advantages and applications. Additionally, it covers the structure of C programming, including syntax, keywords, identifiers, constants, and operators.

Uploaded by

Rohilla shivam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

DEE-402 COMPUTER PROGRAMMING AND

APPLICATIONS

1. Algorithm and Program Development:

Steps in development of a program, Flow-charts,


algorithm development, Introduction to various computer
languages, Concept of interpreter, compiler, high level
language, machine language and Assembly Language.
Syllabus
REFERENCE BOOKS

• LET US C by Yashavant Kanetkar.

• C programming by e balagurusamy.
Steps in development of a program
Problem Definition:-Identity the problem, what is requirement, what
should be the output.

Problem Analysis:- Finding/gathering of resources like variables, data


types, functions. Language to be used etc.

Design:- It can be done it two ways , Algorithm and Flowcharts, Algorithm


is list of instructions/steps to solve the problem and Flowcharts is pictorial
representation of the problem.

Coding:-Here coding of the program is done in the desired programming


language i.e. C , C++, Java etc.

Testing:- Here we test the code developed in previous steps and check
whether we are getting the desired result.

Maintenance:-During this phase program is used by end users and changes


if any may be made after repeating all the steps
ALGORITHM
The word “algorithm” relates to the name of the mathematician Al-
khowarizmi, which means a procedure or a technique.
Software Engineer commonly uses an algorithm for planning and
solving the problems. An algorithm is a sequence of steps to solve a
particular problem

Algorithm has the following characteristics.

Input:- An algorithm may or may not require input.

Output:- : Each algorithm is expected to produce at least one result.

Definiteness:- Each instruction must be clear and unambiguous.

Finiteness:-It should terminate after finite steps.


Advantages of algorithm
• It is a step-wise representation of a solution to a
given problem, which makes it easy to understand.

• An algorithm uses a definite procedure.

• It is not dependent on any programming


language, so it is easy to understand for anyone
even without programming knowledge.

• Every step in an algorithm has its own logical


sequence so it is easy to debug.
HOW TO WRITE ALGORITHMS
Step 1 Define your algorithms input: Many algorithms take in data to
be processed, e.g. to calculate the area of rectangle input may be the
rectangle height and rectangle width
Step 2 Define the variables: Algorithm's variables allow you to use it
for more than one place. We can define two variables for rectangle
height and rectangle width as HEIGHT and WIDTH (or H & W). We
should use meaningful variable name e.g. instead of using H & W use
HEIGHT and WIDTH as variable name.
Step 3 Outline the algorithm's operations: : Use input variable for
computation purpose, e.g. to find area of rectangle multiply the HEIGHT
and WIDTH variable and store the value in new variable (say) AREA.
An algorithm's operations can take the form of multiple steps and even
branch, depending on the value of the input variables.
Step 4 Output the results of your algorithm's operations: In case of
area of rectangle output will be the value stored in variable AREA. if the
input variables described a rectangle with a HEIGHT of 2 and a WIDTH
of 3, the algorithm would output the value of 6
FLOWCHART
Unlike an algorithm, Flowchart uses different symbols to design a solution to a
problem. Graphical representation of sequence of steps to solve a problem. It is
another commonly used programming tool. By looking at a Flowchart one can
understand the operations and sequence of operations performed in a system.
Flowchart is often considered as a blueprint of a design used for solving a specific
problem.

Advantages of flowchart:
Flowchart is an excellent way of communicating the logic of a program.
Easy and efficient to analyze problem using flowchart.
During program development cycle, the flowchart plays the role of a blueprint,
which makes program development process easier.
After successful development of a program, it needs continuous timely maintenance
during the course of its operation. The flowchart makes program or system
maintenance easier.
It is easy to convert the flowchart into any programming language code.
Symbols used in Flowchart
Example
Exercise

• Write an algorithm to find the largest of three


numbers
• Draw a flowchart to find the largest of three
numbers.
Programming Language- Computer is an electronic device which works on the
instructions provided by the user. Such a computer understandable language is known as
Programming language.
A computer programming language consists of a set of symbols and characters, words, and
grammar rules that permit people to construct instructions in the format that can be
interpreted by the computer system Computer

Machine Language- Since computers are digital devices, they only recognize binary
data. Every program, video, image, and character of text is represented in binary. This binary
data, or machine code, is processed as input by the CPU. The resulting output is sent to the
operating system or an application, which displays the data visually. For example, the ASCII
value for the letter "A" is 01000001 in machine code, but this data is displayed as "A" on the
screen

Example: The program to add two numbers in memory and print the result look something
like the Following:
00100000000000110011100
001111000000111111000111
10011110001110110011010
Assembly Language- Assembly languages are also known as second generation
languages. These languages substitute alphabetic symbols for the binary codes of machine
language. In assembly language, symbols are used in place of absolute addresses to
represent memory locations. Mnemonics are used for operation code, i.e., single letters or
short abbreviations that help the programmers to understand what the code represents.
Example MOV AX, DX
Machine language
Machine language is the low level programming
language. Machine language can only be represented
by 0s and 1s. In earlier when we have to create a picture
or show data on the screen of the computer then it is
very difficult to draw using only binary digits(0s and
1s). For example: To write 120 in the computer system
its representation is 1111000. So it is very difficult to
learn. To overcome this problem the assembly language
is invented.
Assembly language
Assembly language is the more than low level and
less than high-level language so it is intermediary
language. Assembly languages use numbers, symbols,
and abbreviations instead of 0s and 1s.For example: For
addition, subtraction and multiplications it uses symbols
likes Add, sub and Mul, etc.
Sr.no Machine Language Assembly Language
1
Machine language is only understand Assembly language is only understand
by the computers. by human beings not by the computers.

2
In machine language data only
In assembly language data can be
represented with the help of binary
represented with the help of mnemonics
format(0s and 1s), hexadecimal and
such as Mov, Add, Sub, End etc.
octadecimal.

3
Assembly language is easy to
Machine language is very difficult to
understand by the human being as
understand by the human beings.
compare to machine language.

4
Modifications and error fixing cannot Modifications and error fixing can be
be done in machine language. done in assembly language.

5
Machine language is very difficult to Easy to memorize the assembly
memorize so it is not possible to learn language because some alphabets and
the machine language. mnemonics are used.
The time and cost of creating machine and assembly languages was quite high. And
this was the prime motivation for the development of high level languages. Because of
the difficulty of working with low-level languages, high-level languages were
developed to make it easier to write computer programs. High level programming
languages create computer programs using instructions that are much easier to
understand than machine or assembly language code because you can use words that
more clearly describe the task being performed. When writing a program in a high-
level language, then the whole attention needs to be paid to the logic of the problem.
A compiler is required to translate a high-level language into a low-level language.
High-level languages include FORTRAN, COBOL, BASIC, PASCAL, C, C++ and JAVA.

Advantage of high level Language:- Readability, portability, easy debugging, user


friendly , easy to understand by human .

Introduction to C Programming
• C was originated in Bell Telephone Laboratories presently known as AT & T
Bell Laboratories by Dennis Ritchie in 1970
• Both high level and Low level language
Applications of C Language
Mainly C Language is used for Develop Desktop application and system software.
Some application of C language are given below.
•C programming language can be used to design the system software like operating
system and Compiler.
•To develop application software like database and spread sheets.
•For Develop Graphical related application like computer and mobile games.
•To evaluate any kind of mathematical equation use c language.
•C programming language can be used to design the compilers.
•UNIX Kernel is completely developed in C Language.
•For Creating Compilers of different Languages which can take input from other
language and convert it into lower level machine dependent language.
•C programming language can be used to design Operating System
•C programming language can be used to design Network Devices.
•To design GUI Applications. Adobe Photoshop, one of the most popularly used
photo editors since olden times, was created with the help of C
Compiler and Interpreter
• Compilers and interpreters are programs that take a program
written in a language as input and translate it into machine
language.
Thus a program that translates a C program into machine code
is called C compiler;
• BASIC program into machine code is called a BASIC
compiler and so on.
• A number of different compilers are available these days for C
language.
• GCC, ANSI, Borland C, Turbo C, etc. are only few of the
popular C compilers.
Program Development in C
Editor
This program is used for writing the Source Code, the first thing that any
programmer writing a program in any language would be doing.
Debugger
This program helps us identify syntax errors in the source code.
Pre Processor
There are certain special instructions within the source code identified by the #
symbol that are carried on by a special program called a preprocessor.
Compiler
The process of converting the C source code to machine code and is done by a
program called Compiler.
Linker
The machine code relating to the source code you have written is combined with
some other machine code to derive the complete program in an executable
file. This is done by a program called the linker
History of C
• Developed by Brian Kernighan and Dennis Ritchie
of AT&T Bell Labs in 1972
• In 1983 the American National Standards
Institute began the standardisation process
• In 1989 the International Standards Organisation
continued the standardisation process
• In 1990 a standard was finalised, known simply as
“Standard C”
• Everything before this is known as “K&R C”
C Program
tells compiler about standard input and output functions (i.e. printf + others)

#include <stdio.h> /* comment */


main function
int main(void)
{
printf("Hello\n");
“begin” printf("Welcome to the Course!\n");

return 0;

flag success to } Hello


operating Welcome to !
“end”
system
• #include <stdio.h>

• int main()
• {
• // declaring a variable
• int x = 100;

• // printing the address of the variable
• printf("The address of x is %p", &x);
• return 0;
• }
The Format of C
• Statements are terminated with semicolons
• Indentation is ignored by the compiler
• C is case sensitive - all keywords and Standard
Library functions are lowercase
• Strings are placed in double quotes
• Newlines are handled via \n
• Programs are capable of flagging success or
error,
The Character set of ‘C’
C language consist of some characters set, numbers and
some special symbols. The character set of C consist of all
the alphabets of English language. C consist of
• Alphabets a to z, A to Z
• Numeric 0,1 to 9
• Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
The words formed from the character set are building
blocks of C and are sometimes known as tokens. These
tokens represent the individual entity of language.

The following different types of token are used in C


1) Identifiers 2)Keywords 3)Constants
4) Operators 5)string 6)special symbols
Identifiers

• Identifiers are used as the general terminology for the naming of


variables, functions, and arrays. These are user-defined names
consisting of an arbitrarily long sequence of letters and digits with
either a letter or the underscore(_) as a first character
Certain rules should be followed while naming c identifiers which are
as follows:
• They must begin with a letter or underscore(_).
• They must consist of only letters, digits, or underscore. No other
special character is allowed.
• It should not be a keyword.
• It must not contain white space.
• It should be up to 31 characters long as only the first 31 characters
are significant.
Keywords
auto double int struct
• Keywords are nothing but
system defined identifiers.
• Keywords are reserved break else long switch
words of the language.
• They have specific case enum register typedef
meaning in the language
and cannot be used by the char extern return union
programmer as variable or
constant names const float short unsigned
• C is case senitive, it means
these must be used as it is
continue for signed void
• 32 Keywords in C
Programming
default goto sizeof volatile

do if static while
Strings in C
Strings are nothing but an array of characters
ended with a null character (‘\0’). This null
character indicates the end of the string. Strings
are always enclosed in double quotes. Whereas, a
character is enclosed in single quotes in C and
C++.
char string[20] = {‘c’, ’s’, ‘i’, ‘r’, ‘c’, ‘s’, ‘i’, ‘o’, ‘i’, ’s’, ‘t’,
‘c’, ‘c’, ‘\0’};
char string[20] = “csioistc”;
char string [] = “csioistcst”;
Special Symbols

• Brackets[]: Opening and closing brackets are used as array element


references. These indicate single and multidimensional subscripts.
• Parentheses(): These special symbols are used to indicate function
calls and function parameters.
• Braces{}: These opening and ending curly braces mark the start and
end of a block of code containing more than one executable
statement.
• Comma (, ): It is used to separate more than one statement like for
separating parameters in function calls.
• Colon(:): It is an operator that essentially invokes something called
an initialization list.
• Semicolon(;): It is known as a statement terminator. It indicates the
end of one logical entity. That’s why each individual statement must
be ended with a semicolon.
Constants

• The constants refer to the variables with fixed


values. They are like normal variables but with
the difference that their values can not be
modified in the program once they are defined.
Example
const int c_var = 20;
const int* const ptr = &c_var;
Constants are further divided into int, char, string ,
float type and can be used as per requirement of
the program
Operators

• Unary Operators: Those operators that require only a single


operand to act upon are known as unary operators. For Example
increment and decrement operators
• Binary Operators: Those operators that require two operands to act
upon are called binary operators. Binary operators can further are
classified into:
– Arithmetic operators
– Relational Operators
– Logical Operators
– Assignment Operators
– Bitwise Operator
• Ternary Operator: The operator that requires three operands to act
upon is called the ternary operator. Conditional Operator(?) is also
called the ternary operator.
Write about operator precedence and Associatively
#include<stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);//arithmetic operator
c=a/b;
printf("a/b = %d \n",c);
printf("++a = %d \n", a++);// increment operator
printf("++a = %d \n", ++a);//increment operator
printf("--b = %d \n", --b); //decrement operator

int d=6;
int e=5; //assignment operator
printf("d = %d\n", d);
d+=e; //assignment operator
printf("d = %d\n", d);
int f=5,g=5, h=10;
printf("%d == %d is %d \n", f, g, f == g);//relational operator
printf("%d < %d is %d \n", f, g, f < g); //less than
printf("%d != %d is %d \n", f, g, f != g);//not equal
int result;
result=(f==g)&&(f>h);//logical operator (logical and)
printf("(f == g) && (f > h) is %d \n", result);
result=(f==g)||(f>h); //logical OR
printf("(f == g) || (f > h) is %d \n", result);
result=(f==g)&&(f<h);
printf("(f == g) && (f < h) is %d \n", result);
result = !(f != g);
printf("!(f != g) is %d \n", result);
int i;
float j;
char k;
printf("size of int: %d\n",sizeof(i));//size of operator
printf("size of float: %d\n",sizeof(j)); //size of operator
printf("size of char: %d\n",sizeof(k));
return 0;
}
Escape Sequences
• \b Backspace
• \f Form feed
• \n Newline
• \r Return
• \t Horizontal tab
• \v Vertical tab
• \\ Backslash
• \' Single quotation mark
• \" Double quotation mark
• \? Question mark
• \0 Null character
Variables in C

A variable is a name of the memory location. It is used to store data. Its value
can be changed, and it can be reused many times.

type variable_list;
• The example of declaring the variable is given below:
• int a;
• float b;
• char c;
Here, a, b, c are variables. The int, float, char are the 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';
DATA TYPES
• The data type is a collection of data with values having
fixed values,
• Types
• Description
• Primitive Data Types Primitive data types are the most
basic data types that are used for representing simple
values such as integers, float, characters, etc.
• User Defined Data Types The user-defined data types
are defined by the user himself.
• Derived Types The data types that are derived from
the primitive or built-in data types are referred to as
Derived Data Types.
#include<stdio.h>
int main()
{
int a;
printf("enter first number");
scanf("%d",&a);
int b;
printf("enter second number");
scanf("%d",&b);
int c;
c=a*b;
printf("multiplication is%d",c);
return 0;
}

1200000
120000
Output- -2028888064
Decision Making in C / C++

• The conditional statements (also known as decision control structures)


such as if, if else, switch, etc. are used for decision-making purposes in
C/C++ programs.
• They are also known as Decision-Making Statements and are used to
evaluate one or more conditions and make the decision whether to
execute a set of statements or not. These decision-making statements in
programming languages decide the direction of the flow of program
execution.
• There come situations in real life when we need to make some
decisions and based on these decisions, we decide what should we do
next. Similar situations arise in programming also where we need to
make some decisions and based on these decisions we will execute the
next block of code
Diagram of Conditional Statement
#include <stdio.h>

int main()
{
int i = 10;

if (i > 15) {
printf("10 is greater than 15");
}

printf("I am Not in if");


}
if…..else ... Statement
• Its syntax is:-
• if (condition)
• {
• Statement1;
• Statement2;
• }
• else
• {
• Statement1;
• Statement2;
• }
Nesting of if …else
• When there are another if else statement in if-block or else-
block, then it is called nesting of if-else statement.

• if (condition)
{
If (condition)
Statement1;
else
statement2;
}
Statement3;
If….else LADDER
• In this type of nesting there is an if else statement in every else part
except the last part. If condition is false control pass to block where
condition is again checked with its if statement.
if (condition)
Statement1;
else if (condition)
statement2;
else if (condition)
statement3;
else
statement4;
Switch Statement in C

• Switch case statement evaluates a given expression and based on the evaluated
value(matching a certain condition), it executes the statements associated with it.
Basically, it is used to perform different actions based on different
conditions(cases).
Syntax of switch Statement in C
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
default: default_statement;
}
Loops in C
• Loop:-it is a block of statement that performs set of instructions. In loops.
Repeating particular portion of the program either a specified number of
time or until a particular no of condition is being satisfied.

• There are three types of loops in c


• 1.While loop
• 2.do while loop
• 3.for loop
While loop
while(condition)
{
Statement 1;
Statement 2;
}
Or while(test condition)
Statement;
do while loop
• This (do while loop) statement is also used for looping. The body of this
loop may contain single statement or block of statement. The syntax for
writing this statement is:
Do
{
Statement;
}
while(condition);
Difference b/w While and Do While
• Here firstly statement inside body is executed then condition is
checked. If the condition is true again body of loop is executed and
this process continue until the condition becomes false. Unlike
while loop semicolon is placed at the end of while.
• There is minor difference between while and do while loop, while
loop test the condition before executing any of the statement of
loop. Whereas do while loop test condition after having executed
the statement at least one within the loop. If initial condition is
false while loop would not executed it’s statement on other hand
do while loop executed it’s statement at least once even If
condition fails for first time. It means do while loop always executes
at least once. Notes: Do while loop used rarely when we want to
execute a loop at least once.
for loop
• In a program, for loop is generally used when number of iteration are
known in advance. The body of the loop can be single statement or
multiple statements. Its
• syntax for writing is:

for(exp1;exp2;exp3)
{
Statement;
}
for(initialized counter; test counter; update counter)
{
Statement;
}
Nesting of loop
• When a loop written inside the body of another loop then, it is known as
nesting of loop. Any type of loop can be nested in any type such as while,
do while, for. For
example nesting of for loop can be represented as :
void main()
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<5; j++)
printf(“%d %d”, i, j);
}
Output: i=0
j=0 1 2 3 4
i=1
j=0 1 2 3 4
Break statement(break)
• When break is encountered inside any loop, control automatically
passes to the first statement after the loop. This break statement is
usually associated with if statement.

void main()
{
int j=0;
for(;j<6;j++)
if(j==4)
break;
}
Output:
0123
Continue statement (key word
continue)
• Continue statement is used for continuing next iteration of loop after skipping some statement of loop.
When it encountered control automatically passes through the beginning of the loop. It is usually
associated with the if statement. It is useful when we want to continue the program without executing any
part of the program.
• The difference between break and continue is, when the break encountered loop is terminated and it
transfer to the next statement and when continue is encounter control come back to the beginning
position.
• Example:-
void main()
{
int n;
for(n=2; n<=9; n++)
{
if(n==4)
continue;
printf(“%d”, n);
}
}
Printf(“out of loop”);
}
Output: 2 3 5 6 7 8 9 out of loop
ARRAY
• Array is the collection of similar data types or collection of similar entity stored in contiguous memory location. Array of character is a
string. Each data item of an array is called an element. And each element is unique and located in separated memory location. Each of
elements of an array share a variable but each element having different index no. known as subscript. One dimensional array is known
as vector and two dimensional arrays are known as matrix. ADVANTAGES: array variable can store more than one value at a time
where other variable can store one value at a time
Example: int arr[100];
nt mark[100];
DECLARATION OF AN ARRAY :
Its syntax is :Data type array name [size];
int arr[100]; int mark[100]; int a[5]={10,20,30,100,5}

• Below are some real-time applications of arrays.


• Signal Processing: Arrays are used in signal processing to represent a set of samples that are collected over time. This can be used in
applications such as speech recognition, image processing, and radar systems.
• Multimedia Applications: Arrays are used in multimedia applications such as video and audio processing, where they are used to store
the pixel or audio samples. For example, an array can be used to store the RGB values of an image.
• Data Mining: Arrays are used in data mining applications to represent large datasets. This allows for efficient data access and
processing, which is important in real-time applications.
• Robotics: Arrays are used in robotics to represent the position and orientation of objects in 3D space. This can be used in applications
such as motion planning and object recognition.
• Real-time Monitoring and Control Systems: Arrays are used in real-time monitoring and control systems to store sensor data and
control signals. This allows for real-time processing and decision-making, which is important in applications such as industrial
automation and aerospace systems.
• Financial Analysis: Arrays are used in financial analysis to store historical stock prices and other financial data. This allows for efficient
data access and analysis, which is important in real-time trading systems.
• Scientific Computing: Arrays are used in scientific computing to represent numerical data, such as measurements from experiments
and simulations. This allows for efficient data processing and visualization, which is important in real-time scientific analysis and
experimentation.
One dimension and two dimension
array
Syntax
Data-type array name[row][column];
Total no. of elements in 2-D array is calculated as row*column
Example:- int a[2][3];
Total no of elements=row*column is 2*3 =6
20 2 7
8 3 15

Initialization of 2-d array


2-D array can be initialized in a way similar to that of 1-D array. for example:-
int mat[4][3]={11,12,13,14,15,16,17,18,19,20,21,22};
Or int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}}
C Functions

• A function in C is a set of statements that when called perform some specific task. It is the
basic building block of a C program that provides modularity and code reusability. The
programming statements of a function are enclosed within { } braces, having certain
meanings and performing certain operations
Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
• Function Declaration-Syntax
return_type name_of_the_function (parameter_1, parameter_2);
Function Declaration
• Function Definition
The function definition consists of actual statements which are executed when the
function is called (i.e. when the program control comes to the function).
Function Call

• A function call is a statement that instructs the compiler to execute the


function. We use the function name and parameters in the function call.
• We can pass arguments to the C function in
two ways:
Pass by Value:-Parameter passing in this method copies values from actual
parameters into formal function parameters. As a result, any changes
made inside the functions do not reflect in the caller’s parameters.
Pass by Reference:-The caller’s actual parameters and the function’s actual
parameters refer to the same locations, so any changes made inside the
function are reflected in the caller’s actual parameters.

You might also like