C programing ppt
C programing ppt
APPLICATIONS
• C programming by e balagurusamy.
Steps in development of a program
Problem Definition:-Identity the problem, what is requirement, what
should be the output.
Testing:- Here we test the code developed in previous steps and check
whether we are getting the desired result.
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
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.
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)
return 0;
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
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++
int main()
{
int i = 10;
if (i > 15) {
printf("10 is greater than 15");
}
• 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.
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}
• 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