Physiochemical properties of nanomaterials and its nanotoxicity.pptx
C programming
1. C PROGRAMMING
Rohan K. Gajre
Drop me a line if I can do anything else for
you.
Mob:- 8087 0297 27
rohangajre@gmail.com
2. HISTORY
History of C:
C is a programming language developed by Dennis Ritchie
at AT & T’s Bell laboratories, USA in 1972.
• The root of all model language is ALGOL introduced in the early
1960s.
• ALGOL was the first computer language to use a block structure.
• ALGOL gave the concept of structure programming to the computer
science communication.
• In 1967s Martin Richards developed a language called as BCPL(Basic
Computer Programming Language) primarily form writing system
software.
• In 1970s Ken Thomson created a language using many feature of
BCPL and called it simply ‘B’.
• B was used to create early versions of UNIX operating system at Bell
laboratories.
3. • C was evolved from ALGOL , BCPL and B by Dennis Ritche in 1972 at
AT & T’s Bell Laboratories.
• ‘C’ uses many concept of data types and other power full features
since it is developed along with UNIX operating system.
Language Years Developers
ALGOL 1960 International Language
BCPL 1967 Martin Ritchard
B Language 1970 Ken Thomson
C Language 1972 Dennis Ritche
K & R C language 1978 Kernighan Ritchie
ANSI C 1989 ANSI Committee
4. CHARACTERISTICS AND FEATURES
Features of C language
C-is the one of the most popular programming language
and have the following features that cause it more popular.
• Simplicity- C has the richest collection of inbuilt function, keyword
and data types. Also it follows a structured approach so it is learn
also.
• Clarity- The keyword and library function available in C resembles
common English words thus it helps to improve the clarity of
programs.
• Portability- By the term portable we mean the computer
independent. The program made C can be made to run on different
machines thus increasing the efficiency of the programs.
• Modularity- The program made in C can be easily divide into small
module with the use of function. This features helps to increase the
understandability of the program.
• Easy Availability- The C compiler are easily available and they
require very less disc space for a their storage.
C is the one most popular language used today and features in
operating system embedded system.
5. C is the robust programming with an impressive set of built-
in function and a variety of operator which you can use to
wright a complex programs.
C programs are fast and efficient. This is because C uses a
powerful set of data types and operators.
C combine the power and capability of assembly language
with the user friendly features of a high level language.
C is very flexible language. It is convenient and portable, like
a high level language and flexible like a low level language. It
can be interfaced with other programming language.
C is easy to debug. The C compiler detects syntax errors
quickly and easily displays the error along with the line number
of the code and the error message.
6. • Object- The object is the basic runtime entities is an object oriented system.
The programs can be divided into the no of entities known as object. They
may be represent a place, a bank account, a table of data or any items thet
the programs has to handle. They may also represent used defined data such
as a vector, time and lists. Object take up space in the memory and have and
associated address.
when a program is executed the object interact by sending message to one
another. For ex- If customer and account are two object in programs, then the
customer object may send a message to the account object requesting bank
balance.
Each object contain data, and code to manipulate the data. Object can
interact without having to known details of each others data or code.
7. PREPROCESSOR DIRECTIVE
C programming
The C-preprocessor are not part of
the compiler but it is separate step in the compilation
process. A C-preprocessor is just a text substitution tool
and it instruct the compiler to do required preprocessing
before the actual compilation. All preprocessor command
begins with a hash symbol(#). It must be the first nonblank
character and for readability, a preprocessor directive
should begins in the first column.
For e.g. #define- Substitutes a preprocessor macro
#include- Insert a particular header from another
file
#error- Prints error message on stander error
8. There are four types of preprocessor directives
• Macro
• File inclusion
• Conditional compilation
• Other directives
• Macro- Macro are the piece of code in a program which is given same name.
Whenever the name encountered by the compile the compiler replace the
name with actual piece of code. The ‘# define’ directives is used to define a
macro.
• File Inclusion- These type of preprocessor directives tells the compiler to
include file in the source code program. There are two types of files which can
be included by the program.
a. Header files or standard files
b. User defined files
• Conditional compilation- Conditional compilation directives are the types of
the directives which helps to compile a specific portion of the program or to
skip the compilation of some specific part of the program based on condition.
This can be done with the help of the two preprocessing commands ‘if def’ and
‘end if’.
9. • Other directives- A part from the above direatives there are two or more
directives which are not commonly used.
a. #undef Directive- The # undef directives is used to undefined an existing macro
b. #pragma Directive- This directives are special purpose directives and used to
turn on or off some features.
They are explained as follows:
#pragma startup and #pragma exit- These directives helps us to specify the
function that are needed to run before program startup( Before the control passes
to main()) and just before program exit( just before the control return from main()).
10. HEADER FILES
A header files is a file with extension .h which
contain C function declaration and macro declaration to be
shared between several files. There are two types of
header files- The files that the programmer writes and the
files that comes with your compiler.
The header files your program by including with the
preprocessor directives #include. A simple practice in C or
CPP programs is that we keep all the content, macros,
system wide global variables and function prototypes in
the header files and include that header files whenever it
is required.
• In C- language- In general the C language contain the
header files also the programmer writes and comes with
compiler.
11. Some are discussed as follows-
•#include <stdio.h>
-The # is preprocessor
-include used to for header files
-<stdio.h> stand for standard input/output header files
List of some inbuilt C function is stdio.h file
1. printf()
2. scanf()
3. getc()
4. gets()
5. getchar()
6. putchar()
7. F(open)
8. Fclose
9. remove()
10. Fflush()
12. CONSOLE I/O FUNCTION
In C language
Console I/O function are used to receive input from
the keyboard and write output to screen.
Console I/O function are classified in two types-
• Formated function
• Unformated function
• Formated function- The formatted function are of the
following
a. Scanf()- This function is used for in putting the values from
the keyboard in a specified format. These function is used to
input value of any type as int, float, char, string.
Syntax- scanf(“format specifier”, variable_name);
While in putting any type of value except string we have to
proceed address operator(&) before the variable name.
13. b. printf()- This function is used for displays(show) the value on the screen
specified format. This function show any value type message (int, float, char,
string)
Syntax- printf(“format specifier”, variable_name)
c. sprintf()- it similar to the printf but instead of printing the output on screen, it
stores it in the character array.
d. Sscanf()- The sscanf() is the counter part of sprint() function. It allows the
programmer to store the character of string in some other variable
14. PROGRAM STRUCTURE
• Structure of C program:
#include<pre-processor directives>
[macro declaration and definition]
[global variable declaration]
main()
{
Local variable declaration
……..
Declaration part
Executable part
}
[User_defined_function_definition([arguments])
{
statement block;
}
]
15. • #include<pre processor direcives>:
At this place we have to include all the header files,which are prototype
for the statement wchich are used in our program.
• [Macro declaration and definition]:
at this place we have to declare and definition of the macro that are used
in program.
It is optional.
• [Global variable declaration]:
If we require global variable in our program then such variables are
declared at this place.
It is optional.
• main():
This is the first statement of any C program.Every C program must start
with main().
The local variable declaration have to declare all the variable with the
proper datatype which are used in our program(in main()).
• [User_defined_funtion_defintion]:
If any user defined funtion is called in program then its definition are
given at this place.
It is optional.
16. CONTROL STATEMENT
In C language:
1.In C-language :
As we know that the structure oriented language and
does not use the concept of the object oriented language . In this
language the program structure is structure oriented
17. • Decision making with if statement:
The ‘if statement’ is powerful decision making statement
and is used to control the flow of execution of statement . It is basically to a way
decision statement and it used in conjunction with an expression . It takes the
following form.
It allows the
computer to execute the expression
first and then , depending on whether the value of the expression is ‘true’ (or
non zero) or false(zero) , it transfer the control to a particular statement . The if
statement may be implemented in different forms depending in the complexity
of condition to be tested.
The different forms are:
1.Simple if statement.
2.if _ _ _else statement.
3.Nestead if _ _ _ else statement.
4.else if ladder
if(test-expression){ };
18. .1.Simple if statement :
In the simple_if , the statement block may be a single
statement or a group of statement if the test expression is true then the
statement block is executed otherwise the statement-X block will be executed .
Syntax: Flowchart:
if(test_expression) Entry
{
statement block;
}
True
statement-X;
False
Test
expres
sion
statement_-X
statement_bl
ock
20. • 2.If_ _ _else statement:
The if_else statement is a extension of simple_if statement.
In the if_else statement if the test-expression is true then the true block is
executed otherwise the false block is executed.
Syntax: Flowchart:
Entry
if(test_expression)
{
statement1; //true block True
False
}
else
{
statement2; //false block
}
statement-X;
Test
expres
sion
statement1 statement_2
statement_-X
21. Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a , b;
clrscr();
printf(“nt Enter the two number:”);
scanf(“%d%d”,&a&b);
if(a+b=>20)
{
printf(“Addition is greater than 20”);
}
else
{
printf(“Addition is smaller than 20”);
}
getch();
}
o/p:
Enter the two number
10 20
Addition is greater
than 20
22. • Nested if statement:
If the one ‘if’ condition is present with the another ‘if’
condition and so on , then such type of control statement is called as nested
if .
We also say that condition instead of test expression .
Syntax: 1. if(condition-A) or 2. if(condition1
{ {
if(condition-B) if(condition-2)
{ {
statement-1; if(condition-3)
} {
} statement;
else }
{ }
statement-2; }
}
}
else
{
statement-3;
} -------- // N number of times
24. As the above syntax and flowchart if the test expression-A(condition) is true
then it jump to the test expression-B then if the condition B is true then
statement-1 will be executed otherwise statement2 will be executed . Otherwise
if the test expression-A is false then statement 3 will be executed otherwise
control goes to the statement-X block .
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a , b ,c;
clrscr();
printf(“ Type the value of a,b,c”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
25. {
printf(“%d is grater: ”,a);
}
else
{
printf(“%d is grater:“,c);
}
else
{
if(b>c)
{
printf(“%d is grater:”, b);
}
else
{
printf(“%d is grater:”,c);
}
}
getch();
}
o/p:
Type the value of
a b c
8 9 3
9 is grater
26. • Else If ladder:
When more than one ‘is’ condition are placed in a series (sequence)
, then such ‘if’ structure is called as else if ladder (multiple if) structure.
Syntax:
if(condition 1)
{ True
False statement1;
}
else
if(condtion 2)
{ True
False statement2;
}
else ----
if(conditio N)
{
statement n;
}
else
{
statement – X; }
28. As the above the syntax and flowchart if the condition is true then associated
statement executed and control goes to statement x otherwise condition is
false then goes to next condition and if all false then default condition will be
executed .
Programs:
#include<stdio.h>
#include<conio.h>
main()
{
int num1 , num2 , ch;
clrscr();
printf(“Enter the value of num1 & num2:”);
scanf(“%d%d”,&num1,&num2);
printf(“1.add 2.sub 3.mul 4.divn Enter choice:”);
scanf(“%d”,&ch);
if(ch==1)
{
printf(“n addition is=%d”,num1+num2);
31. SWITCH STATEMENT
The ‘switch case’ is a control statement which allows to make
decision from the number of choices. To make this control statement
use keyword are switch, case, break and default.
• Switch keyword :
The switch keyword is follows by a expression, which yield
and integer or character constant, each character in each case may be
different from all other. When we executed the program which contain
the switch statement first the expression following the keyword
‘switch’ is evaluated. When a match found the program executed the
statement following the case and all subsequent case.
• Break keyword :
The break-statement at the end of each block signal the end
of a particular case and causes an exist from the switch statement .
Transfering the control to the statement . Transfering the control to
the statement –x following the switch.
32. • Default keyword :
The default is optional case when present it will be
executed if the value of the expression does not match with any of the case
values. If not present, no action takes place when all matches failed.
NOTE : The difference between if and switch statement.
- if statement can evaluate float condition but switch can’t.
- if statement allows relational operator but switch can’t.
switch statement :
As the switch case have same syntax and flowchart in both languages, but
as we know that only the difference is C language use the concept of
structure oriented programming and CPP use the concept of partially object
oriented programming(with classes and object or without) but the java is
the totally object oriented language hence use the concept of object and
classes
33. Syntax of switch statement :
switch(expression)
{
Case 1:
Statement-1;
Break;
Case 2:
Statement-2;
Break;
Case 3:
Statement-3;
Break;
.
.
.
Default:
Statement N;
}
35. Example : Program to get two members input and perform
mathematical operation on it according to choice
#include <stdio.h>
#include<conio.h>
main()
{
Int num1, num2,ch;
Printf(“type value num1 and num2 : “);
Scanf(“%d %d “, num1 & num2);
Printf(“1.add, 2. sub, 3.mul, 4.div n type choice(1-4));
Scanf(“%d”,&ch);
Switch(ch)
{
case : 1
{
printf(“n addition = %d”, num1+num2);
break;
}
37. o/p :
Type value num1 & num2
Choice :
if num1= 10 & num2= 20
Choice-1
10+20=30
38. DECISION MAKING WITH THE LOOPING
STATEMENT
• Looping-
The process of repeatedly executing a block of statement is
known as Looping. Loop is used to perform a one or more series of
commands for a number of items. The number of times may be
fixed or depend on condition.
• Fixed loop- In this loop we known in advance how many times
the loop will execute the command.
e.g. For loop
• Conditional loop- in this loop we don’t know in advance how
many times the loop will execute the command loop will execute
the command till the condition is match.
e.g. while, do…..while
• Why use loop?
When you need to execute a block of code several number
of times then you need to use looping concept.
39. A looping process, in general ,would include the following four steps-
1. Setting and initialization of a counter.
2. Execution of the statement in the loop.
3. Test for a specified condition for execution of the loop.
4. Increment/Decrement the counter.
• Advantage of loops
1. Reduce length of code.
2. Take less memory space.
3. Time consuming process to execute the program is reduced.
• Comparison of looping in C, CPP & Java- The looping statement are of the
three types
1. While loop
2. Do While
3. For loop
In C, CPP & Java the syntax of looping statement is same, but only
the difference is the C is structure based language hence does not use the
concept of object and class, and CPP is the partially object oriented language it
use the concept of object & classes or may not.
40. While loop :
Syntax:
while(condition)
{
loop body ;
increment/decrement;
}
The syntax same for C,CPP and java
42. In while loop first check the condition if the condition Is true then control
goes to the inside loop body otherwise goes outside the body. while loop will
be repeats in clockwise direction.
C-Language
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
i=1;
while(I <= 5)
{
printf(“n %d “,i);
i++;
}
return(0);
getch();
}
O/P :
1
2
3
4
5
43. Do while loop :
when we need a repeat a statement at in do while loop post checking
process will accors i.e after the execution then condition checks and continue.
Syntax :
Syntax is same for C, CPP and java.
do
{
statement;
increment(i++)/decrement(i--);
}
while();
45. // program to demonstrate do-while loop in C
#include<stdio.h>
#include<conio.h>
Void main()
{
int i;
clrscr();
i=1;
do
{
printf(“n %d”,i);
i++;
}
while(i<5);
getch();
}
O/P :
1
2
3
4
5
46. For loop :
For loop is a statement while allow code to de repeatedly for loop
contain 3 parts Initilization, Condition & Increment/decrement.
Syntax :
For(initilization;condition;++/--)
{
statement(s);
}
STEPS :
1. First initialize the variable.
2. Check condition.
3. Control goes to the inside loop body and execute
4. ++/--
5. Repeat until condition true
48. C-Language
//program to Demonstrate for loop in C
#include<stdio.h>
#include <conio.h>
Void main()
{
int i;
clrscr();
For(i=1;i<=5;i++)
{
printf(“n %d “, i);
}
getch();
}
O/P :
1
2
3
4
5
49. ARRAY :
• Introduction :
Array is used to store more than one values of the same data
type under single variable name in a continuous memory location.
Array is the kind of the data structure that can store a fixed size
sequential collection of element of the same data .
An array is used to store a collection of data but if is often more useful to
think f an array as collection of variable of same data type .
• Why we use array :
Instead of declaring individual variable , such as num0 ,
num1 , _ _ _ numN , you declare one array variable such as number[N] to
represent individual variable . A specific element in an array is accessed
by an index .
An array consist of continuous memory location . The lowest address
corresponds to the first element and the highest address to the last
element .
First
Last
Number[0
]
Number[1
]
Number[3
]
_ _ _ _ _ _ Number[
N]
50. Array in C and CPP:
Both the language us the concept of the array and having the
same syntaxes , only difference is that C is the structure oriented language and
CPP is the partially object oriented language .
The following is the points of array in the c and cpp :
• Declaring array.
• Initializing array.
• Accessing array element.
• Types of array:
1. one dimensional array.
2. two dimensional array.
3. three dimensional array
• Concept of pointer:
-pointer to array.
-array of pointer.
• Array to the function.
Each points can be explained as follows :
51. Array:
array is used to store the more than one value of the same data type
under a single variable name in a continuous memory allocation .
• Declaration of Array:
To declare array in the C and CPP a programmer specifies the
type of the element and number of element required by an array as follows .
Syntax:
type array_name [array size];
Example:
int mark[5];
• Initialization of Array :
It is the possible to initialize an array in C and CPP during
declaration .
Syntax:
data type array name[row] [column] = {------- }
Example:
int mark[5]= {10 , 20 , 30 ,40 ,50};
52. • Accessing Array element :
You can access element of array by using indices as the above
example .
mark[0] mark[1] mark[2] mark[3] mark[4]
Note : Array have the zero ‘0’ as the first index not one ‘1’ .
• Types of Array :
1. One dimensional array
2. Two dimensional array
3. Three dimensional array
10 20 30 40 50
53. • 1.One Dimensional Array :
the simplest form of array is one dimensional array . The
one dimensional array consist of single row and multiple columns . While
accessing any element from this type of array , we have to first provide array
name followed by element number or index number of subscript number . If we
don’t specified element number then the first element value is accessed . It can
be represented in memory as follows:
Index number 0 1 2 3
Memory address 100 101 102 103
Declaration :
while declaring one dimensional array we have to specify data type
followed by array name followed by size of array in square bracket ( [] ).
Syntax:
data_type Array_name [size of array]
Example:
int num[4];
2 3 8 9
54. Initialization :
we can assign the value to array element at the time of declaration of
array . All value are enclosed within parenthesis and two values and separated
by comma ( , ).
Syntax:
data_type Array name [size of array ] {set of value}
Example:
int num[4]={20 , 30 , 40}
Program in c :
#include<stdio.h>
#include<conio.h>
main()
{
int ary[5] , p ;
clrscr()
printf(“type % number n”);
for(p=0;p<=4;p++)
{
56. 2.Two Dimensional Array:
The two dimensional array consist of rows and number of
columns . Each row consist of specified number of columns . To access a element
from this type of array , we have to provide array name followed by row number
and followed by columns number . If we skip the column number , then specified
row number and first column (zero index) and first column (index zero) value is
accessed . It is represent in memory as follows.
Index number a[0][0] a[0][1] a[0][2] a[0][3]
Memory address 100 101 102 103
Declaration:
Syntax:
data_type array name[row][column];
Example:
int num[2][2];
2 3 8 9
57. Initialization:
Syntax:
data_type array name[row][column]={{0th row
element} {1st row element}, _ _ _ }
Example:
int num[2][2]={{1,2}{4,5}};
Program in c:
#include<stdio.h>
#include<conio.h>
main()
{
int r , c ,mat[3][3]
clrscr();
for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
printf(“|n enter value for row=%d ,
column=%d:”,r+1,c+1);
58. scanf(“%d”,& mat[r][c]);
}
}
printf(“n matrix in inverse form:”):
for(r=0; r<=2;r++)
{
printf(“t %d”,mat[c][r])
}
}
getch();
}
o/p:
Enter value for
row=1,column=1: 10
column=2: 20
column=3: 30
row=2,
column=1: 40
column=2: 50
column=3: 60
row=3,
column=1: 70
column=2: 80
column=3:90
Matrix in inverse form:
59. • Three Dimensional Array:
It is also know known as multidimensional array . It is the
array of array in which each element represent two dimensional array . While
accessing a element from such type of array , we have to specify array name
followed by element number , row number & column number . The memory
representation of array a[3][3][2] as follows .
1-D array 2-D array 3-d
array
100 101 102
To understand the representation exactly study the following diagram .
1 2 3 4 5 6 7 8 9 1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
100 101 103
1 2
3 4
5 6
7 8
9 1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
60. Declaration:
while declaring three dimensional array we have to specify data type
followed by array name followed by element number (number 2D array)
followed by total number of column in each row . Element , row and column are
numbers are specified in separate square bracket .
Syntax:
data_type array name [element][row][column];
Example:
int num [2][3][3];
Initialization:
we can assign the value to array element at the time of declaration of
array .
Syntax:
data-type array name [element][row][column]=
{
{
{0th row element},
{1st row element},
------