![Page 1: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/1.jpg)
![Page 2: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/2.jpg)
Fundamentals and History of C
C is developed by Dennis RitchieC is a structured programming languageC supports functions that enables easy
maintainability of code, by breaking large file into smaller modules
Comments in C provides easy readabilityC is a powerful language
![Page 3: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/3.jpg)
PROGRAM STRCTURE IN C
#include<stdio.h>#include<conio.h>void main(){
--other statements}
![Page 4: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/4.jpg)
HEADER FILESThe files that are specified in the include
section is called as header fileThese are precompiled files that has some
functions defined in themWe can call those functions in our program
by supplying parametersHeader file is given an extension .hC Source file is given an extension .c
![Page 5: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/5.jpg)
MAIN FUNCTIONThis is the entry point of a programWhen a file is executed, the start point is the
main functionFrom main function the flow goes as per the
programmers choice.There may or may not be other functions
written by user in a programMain function is compulsory for any c
program
![Page 6: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/6.jpg)
Sample program#include<stdio.h>#include<conio.h>int main(){
printf(“Hello”);return 0;
} This program prints Hello on the screen
when we execute it
![Page 7: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/7.jpg)
HOW TO RUN C PROGRAM Type a programSave itCompile the program – This will generate an
exe file (executable)Run the program (Actually the exe created out
of compilation will run and not the .c file)In different compiler we have different option
for compiling and running. We give only the concepts.
![Page 8: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/8.jpg)
Points to rememberCase Sensitive • Case matters in C. A is not a• Add plenty of comments (/* */ or //)• Good layout, not: main() {printf("Hello World\n");}• Use meaningful variable names• Initialize your variables• Use parentheses to avoid confusion:a=(10.0 + 2.0) * (5.0 - 6.0) / 2.0
![Page 9: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/9.jpg)
DATA TYPESPrimitive data types
int, float, double, charAggregate data types
Arrays come under this categoryArrays can contain collection of int or float or
char or double dataUser defined data types
Structures and enum fall under this category.
![Page 10: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/10.jpg)
VARIABLESVariables are data that will keep on changingDeclaration
<<Data type>> <<variable name>>;int a;
Definition<<varname>>=<<value>>;a=10;
Usage<<varname>>a=a+1; //increments the value of a by 1
![Page 11: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/11.jpg)
RULES FOR VARIBLE NAMEShould not be a reserved word like int etc..Should start with a letter or an underscore(_)Can contain letters, numbers or underscore. No other special characters are allowed
including spaceVariable names are case sensitive
A and a are different.
![Page 12: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/12.jpg)
INPUT & OUTPUTInput
scanf(“%d”,&a);Gets an integer value from the user and stores
it under the name “a”Output
printf(“%d”,a)Prints the value present in variable a on the
screen
![Page 13: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/13.jpg)
OPERATORSArithmetic (+,-,*,/,%)Relational (<,>,<=,>=,==,!=)Logical (&&,||,!)Bitwise (&,|)Assignment (=)Compound assignment(+=,*=,-=,/=,%=,&=,|
=)Shift (right shift >>, left shift <<)
![Page 14: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/14.jpg)
OPERATORS(Contd.)
Increment and Decrement Operators ++ Increment operator
-- Decrement Operator k++ or k-- (Post-increment/decrement) k = 5;x = k++; // sets x to 5, then increments k to 6
![Page 15: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/15.jpg)
(contd.)
++k or --k (Pre-increment/decrement)k = 5;x = ++k; // increments k to 6 and then sets to
the resulting value, i.e., to 6
![Page 16: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/16.jpg)
CONTROL CONSTRUCTS
![Page 17: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/17.jpg)
Conditional Statementsif (condition) {
stmt 1; //Executes if Condition is true}else{
stmt 2; //Executes if condition is false}
![Page 18: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/18.jpg)
SWITCH & BREAKswitch(var){case 1: //if var=1 this case executes
stmt;break;
case 2: //if var=2 this case executesstmt;break;
default: //if var is something else this will executestmt;
}
![Page 19: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/19.jpg)
LOOPS
![Page 20: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/20.jpg)
FOR LOOPThe syntax of for loop is
for(initialisation;condition checking;increment){
set of statements}Eg: Program to print Hello 10 timesfor(I=0;I<10;I++){
printf(“Hello”);}
![Page 21: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/21.jpg)
WHILE LOOPThe syntax for while loop
while(condn){
statements;}
Eg:a=10;while(a != 0) Output: 10987654321{
printf(“%d”,a);a- -;
}
![Page 22: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/22.jpg)
DO WHILE LOOPThe syntax of do while loop
do{
set of statements}while(condn);
Eg:i=10; Output:do 10987654321{
printf(“%d”,i);i--;
}while(i!=0)
![Page 23: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/23.jpg)
ARRAYS
![Page 24: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/24.jpg)
ARRAYS
Arrays are collection of data that belong to similar data type
Arrays are collection of homogeneous dataArray elements can be accessed by its
position in the array called as index
![Page 25: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/25.jpg)
Contd.Array index starts with zeroThe last index in an array is num – 1 where num
is the no of elements in a arrayint a[5] is an array that stores 5 integersa[0] is the first element where as a[4] is the fifth
elementWe can also have arrays with more than one
dimensionfloat a[5][5] is a two dimensional array. It can
store 5x5 = 25 floating point numbersThe bounds are a[0][0] to a[4][4]
![Page 26: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/26.jpg)
String functionsstrlen(str) – To find length of string strstrrev(str) – Reverses the string str as rtsstrcat(str1,str2) – Appends str2 to str1 and
returns str1strcpy(st1,st2) – copies the content of st2 to
st1strcmp(s1,s2) – Compares the two string s1
and s2strcmpi(s1,s2) – Case insensitive comparison
of strings
![Page 27: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/27.jpg)
Function Syntax of functionDeclaration section<<Returntype>> funname(parameter list);Definition section<<Returntype>> funname(parameter list){
body of the function}Function CallFunname(parameter);
![Page 28: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/28.jpg)
Example#include<stdio.h>void fun(int a); //declarationint main(){
fun(10); //Call}void fun(int x) //definition{
printf(“%d”,x);}
![Page 29: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/29.jpg)
ACTUAL & FORMAL PARAMETERS
Actual parameters are those that are used during a function call
Formal parameters are those that are used in function definition and function declaration
![Page 30: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/30.jpg)
Call by valueCalling a function with parameters passed as
values
int a=10; void fun(int a)fun(a); {
defn;}
Here fun(a) is a call by value.Any modification done with in the function is local
to it and will not be effected outside the function
![Page 31: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/31.jpg)
Call By ReferenceCalling a function by passing pointers as
parameters (address of variables is passed instead of variables)
int a=1; void fun(int *x)fun(&a); {
defn;}
Any modification done to variable a will effect outside the function also
![Page 32: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/32.jpg)
Explanation
a and x are referring to same location. So value will be over written.
![Page 33: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/33.jpg)
Explanation
![Page 34: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/34.jpg)
ConclusionCall by value => copying value of variable in
another variable. So any change made in the copy will not affect the original location.
Call by reference => Creating link for the parameter to the original location. Since the address is same, changes to the parameter will refer to original location and the value will be over written.
![Page 35: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/35.jpg)
StructuresStructures are user defined data typesIt is a collection of heterogeneous dataIt can have integer, float, double or character
data in itWe can also have array of structuresstruct <<structname>>{
members;}element;We can access element.members;
![Page 36: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/36.jpg)
Examplestruct Person{int id;char name[5];}P1;P1.id = 1;P1.name = “vasu”;
![Page 37: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/37.jpg)
typedef statementUser Defined Data Types The C language provides a facility called typedef
for creating synonyms for previously defined data type names. For example, the declaration:
typedef int Length; makes the name Length a synonym (or alias) for
the data type int.
![Page 38: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/38.jpg)
(contd.)The data “type” name Length can now be
used in declarations in exactly the same way that the data type int can be used:
Length a, b, len ; Length numbers[10] ;
![Page 39: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/39.jpg)
UNION
![Page 40: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/40.jpg)
UNIONUnion has members of different data types,
but can hold data of only one member at a time.
The different members share the same memory location.
The total memory allocated to the union is equal to the maximum size of the member.
![Page 41: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/41.jpg)
EXAMPLE#include <stdio.h>
union marks{ float percent; char grade;};int main ( ){ union marks student1; student1.percent = 98.5; printf( "Marks are %f address is %16lu\n", student1.percent, &student1.percent); student1.grade = 'A'; printf( "Grade is %c address is %16lu\n", student1.grade, &student1.grade);}
![Page 42: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/42.jpg)
ENUM
![Page 43: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/43.jpg)
(ENUMERATED DATA TYPE) Enumeration is a user-defined data type.
It is defined using the keyword enum and the syntax is:
enum tag_name {name_0, …, name_n} ;
The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n.
![Page 44: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/44.jpg)
(contd.) As an example, the statement: enum colors { red, yellow, green } ; creates three constants. red is assigned the value 0, yellow is assigned 1 and green is assigned 2.
![Page 45: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/45.jpg)
POINTERPointer is a special variable that stores
address of another variableAddresses are integers. Hence pointer stores
integer dataSize of pointer = size of intPointer that stores address of integer
variable is called as integer pointer and is declared as int *ip;
![Page 46: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/46.jpg)
Pointers(Contd.)Pointers that store address of a double, char
and float are called as double pointer, character pointer and float pointer respectively.
char *cpfloat *fpdouble *dp;Assigning value to a pointer
int *ip = &a; //a is an int already declared
![Page 47: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/47.jpg)
Exampleint a;a=10; //a stores 10int *ip;ip = &a; //ip stores address of a (say 1000)
ip : fetches 1000*ip : fetches 10* Is called as dereferencing operator
![Page 48: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/48.jpg)
Dynamic Memory Allocation
The process of allocating memory at run time is known as dynamic memory allocation. Although c does not inherently have this facility there are four library routines which allow this functions, which can be used to allocate and free memory during the program execution.
![Page 49: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/49.jpg)
![Page 50: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/50.jpg)
malloc()A block mf memory may be allocated using
the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form:
![Page 51: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/51.jpg)
ptr=(cast-type*)malloc(byte-size);
ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory with size byte-size.
Example: x=(int*)malloc(100*sizeof(int));
![Page 52: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/52.jpg)
Contd…..
On successful execution of this statement a memory equivalent to 100 times the area of int bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x of type int
![Page 53: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/53.jpg)
CallocCalloc is another memory allocation function
that is normally used to request multiple blocks of storage each of the same size and then sets all bytes to zero. The general form of calloc is:
ptr=(cast-type*) calloc(n,elem-size);
![Page 54: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/54.jpg)
Contd……The above statement allocates contiguous space for n blocks each size of elements size bytes. All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space a null pointer is returned.
![Page 55: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/55.jpg)
free()Compile time storage of a variable is allocated and released by the system in accordance with its storage class. With the dynamic runtime allocation, it is our responsibility to release the space when it is not required.free(ptr); ptr is a pointer that has been created by using malloc or calloc
![Page 56: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/56.jpg)
realloc The memory allocated by using calloc or
malloc might be insufficient or excess sometimes in both the situations we can change the memory size already allocated with the help of the function realloc. This process is called reallocation of memory. The general statement of reallocation of memory is :
ptr=realloc(ptr,newsize);
![Page 57: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/57.jpg)
FILE HANDLING
![Page 58: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/58.jpg)
IntroductionFiles are places where data can be stored
permanently.Some programs expect the same set of data
to be fed as input every time it is run.Cumbersome.Better if the data are kept in a file, and the
program reads from the file.Programs generating large volumes of
output.Difficult to view on the screen.Better to store them in a file for later viewing/
processing
![Page 59: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/59.jpg)
Basic File OperationsOpening a fileReading data from a fileWriting data to a fileClosing a file
![Page 60: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/60.jpg)
Opening a FileA file must be “opened” before it can be used.
FILE *fp; : fp = fopen (filename, mode);fp is declared as a pointer to the data type FILE.filename is a string - specifies the name of the
file.fopen returns a pointer to the file which is used
in all subsequent file operations. mode is a string which specifies the purpose of
opening the file:“r” :: open the file for reading only “w” :: open the file for writing only“a” :: open the file for appending data to it
![Page 61: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/61.jpg)
Closing a FileAfter all operations on a file have been
completed, it must be closed.Ensures that all file data stored in memory
buffers are properly written to the file.General format: fclose (file_pointer) ;
FILE *xyz ; xyz = fopen (“test”, “w”) ; ……. fclose (xyz) ;
![Page 62: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/62.jpg)
Read/Write Operations on Files
The simplest file input-output (I/O) function are getc and putc.
getc is used to read a character from a file and return it.
char ch; FILE *fp;…..ch = getc (fp) ;
getc will return an end-of-file marker EOF, when the end of the file has been reached.
putc is used to write a character to a file.char ch; FILE *fp;……putc (c, fp) ;
![Page 63: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/63.jpg)
main() { FILE *in, *out ; char c ;
in = fopen (“infile.dat”, “r”) ; out = fopen (“outfile.dat”, “w”) ; while ((c = getc (in)) != EOF) putc (toupper (c), out); fclose (in) ; fclose (out) ;}
![Page 64: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/64.jpg)
Contd.We can also use the file versions of scanf and
printf, called fscanf and fprintf.General format:
fscanf (file_pointer, control_string, list) ; fprintf (file_pointer, control_string, list) ;
Examples:fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ;fprintf (out, “\nThe result is: %d”, xyz) ;
![Page 65: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/65.jpg)
Command line argumentCommand line arguments are parameters supplied
to a program, when the program is invoked.
How do these parameters get into the program?Every C program has a main function.main can take two arguments conventionally called argc
and argv. Information regarding command line arguments are
passed to the program through argc and argv.
![Page 66: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/66.jpg)
INTRODUCTION TO C PREPROCESSOR
![Page 67: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/67.jpg)
C PreprocessorOverviewPreprocessor DirectivesConditional Compilation
![Page 68: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/68.jpg)
Overview Six phases to execute C:
1. Edit2. Preprocess3. Compile4. Link5. Load6. Execute
![Page 69: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/69.jpg)
C PreprocessorAll preprocessor directives begin with #Possible actions
Inclusion of other filesDefinition of symbolic constants & macrosConditional compilation of program codeConditional compilation of preprocessor
directives
![Page 70: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/70.jpg)
Preprocessor Directives #define for symbolic constants
#define identifier text Creates symbolic constants The “identifier” is replaced by “text” in the
programExample#define PI 3.14area = PI * radius * radius; Replaced by “area = 3.14 * radius * radius” by
preprocessor before compilation
![Page 71: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/71.jpg)
Conditional Compilation Controls the execution of preprocessor
directives & compilation of codeDefine NULL, if it hasn’t been defined yet#if !defined(NULL)#define NULL 0
#endifUse to comment out code (for comments)#if 0code prevented from compiling
#endif
![Page 72: Fundamentals and History of C C is developed by Dennis Ritchie C is a structured programming language C supports functions that enables easy maintainability](https://reader035.vdocuments.us/reader035/viewer/2022062503/5a4d1afd7f8b9ab059984d5d/html5/thumbnails/72.jpg)
THANKS