SlideShare a Scribd company logo
1 of 117
RTS tech. 1
Features of C
 It is simple and easy to use.
 It is Primitive Programming Language.
 It is structured programming language.
 It is used in many popular application such as : Linux,
Unix, MySQL etc…
 It is platform independent.
 It is case sensitive.
 It is Fast.
 It is Mid Level Programming Language.
RTS tech. 2
Learning Strategies
 Learning Natural Language
 Learning Computer Language:
RTS tech. 3
Alphabets Words Sentences Paragraph
Character Set Keywords Statements Program
What is Program?
 Program is a step by step solution to a problem.
 Program contains :
 Data(variables): To store the human data
 Function: to perform action on stored data
RTS tech. 4
Structure of C Program
RTS tech. 5
Library in c
 It is a collection of Reusable programs.
RTS tech. 6
Lets start coding.
 //header file
 # include <stdio.h>
 //main function
 void main(){
 // to print output
 printf(“Hello world!!!”);
 }
RTS tech. 7
Basic Building Block
 #include <stdio.h>: is a header file for input
and output functions.
 void : it is a keyword which means nothing.
 main(): it is a entry point of the program.
 printf(): it is a in-built function to print output
on the console screen.
 // : it is used to write comments. Comments not take
parts in execution.
RTS tech. 8
How to compile and run ?
 Save the file by the name Hello.c
 Open command Prompt and compile the file.
 C:>gcc Hello.c –o Hello
 This command will generate Hello.exe file
 Execute/Run
 c:>Hello.exe
RTS tech. 9
Hello.c Compile Hello.exe
Compilation process of C
 C:>gcc –Wall –save-temps test.c –o test
 Run the command to generate the
following files
 Expanded code file (.i) extension
 Assembly code file (.a) extension
 Object code file(.obj) extension
 Executable code file. (.exe) extension
RTS tech. 10
printf() function
 This function is sued to write the text on the console window.
 Format of printf():
 printf(“format string”, argument list);
 Format Strings are:
 “%d”: it is used to write int
 “%f”: ”: it is used to write float
 “%c”: ”: it is used to write character
 “%lf”: ”: it is used to write double
 “%u”: for unsigned int
 “%Lf”: for long double
RTS tech. 11
scanf() function
 This function is used to receive inputs.
 We can enter the data from the keyboard.
 ‘&’ symbol is must in the scanf function to read the data.
 & is an “Address of” operator.
 For ex.
 int a;
 Scanf(“%d”,&a);
 Above statement will read the integer value from the
keyboard and stores it into variable a.
RTS tech. 12
Use of scanf()
 #include <stdio.h>
 int main(){
 int a;
 printf("Enter a no n");
 scanf("%d",&a);
 printf("Squre of entered No is : %dn",a*a);
 return 0;
 }
RTS tech. 13
Variables in C
 Variables are the name of the
memory Locations.
 It is sued to store data.
 Variables has different sizes
according to the Data type.
 We can access the value by
variable name from the
computer memory.
 Declare a variable:
 data_type var_name=value
14
30
int x=30;
Memory
X
Data_type var_name value
RTS tech.
Basic data Types
 #include<stdio.h>
 void main(){
 int i=2;
 char ch='A';
 float f=2.3f;
 double d=4.567789;
 short int s=2;
 printf("%dn",i);
 printf("%fn",f);
 printf("%lfn",d);
 printf("%cn",ch);
 printf("%dn",s);
 }
RTS tech. 15
Data Types
 Data type is used to define the type of the data.
 Size can be vary compiler to compiler
RTS tech. 16
Types Data Type
Basic int,char, float, double
Derived array, structure, pointer,
union
Enumeration enum
void void
Data type Modifiers
 It is used to modify the size of the Data types.
 C provides the 4 Modifiers:
 short: short integer no
 short int a=1;
 long: large integer no.
 long int a=5678;
 Long double a=2.345667;
 Signed: contains positive(+) and negative(-) values.
 signed int a=-2;
 Unsigned: It accept only positive values.
 unsigned int =+5;
RTS tech. 17
Get The size of the Data Types
 #include<stdio.h>
 void main(){
 int i=2;
 char ch='A';
 float f=2.3f;
 double d=4.567789;
 short int s=2;
 printf("Size of int %dn",sizeof(i));
 printf("Size of float %dn",sizeof(f));
 printf("Size of double %dn",sizeof(d));
 printf("Size of char %dn",sizeof(ch));
 printf("Size of short int %dn",sizeof(s));
 }
RTS tech. 18
All data types
Data Type Range Bytes Format
signed char -128 to +127 1 %c
unsigned char 0 to 255 1 %c
short signed int -32768 to +32767 2 %d
short unsigned int 0 to 65535 2 %u
signed int -2147483648 to +2147483647 4 %d
unsigned int 0 to 4294967295 4 %u
long signed int -2147483648 to +2147483647 4 %ld
long unsigned int 0 to 4294967295 4 %lu
float -3.4e38 to +3.4e38 4 %f
double -1.7e308 to +1.7e308 8 %lf
long double -1.7e4932 to +1.7e4932 10 %Lf
RTS tech. 19
Keywords
 Keywords are reserved words.
 We can not use them to define user defined variables and
function.
RTS tech. 20
Identifiers
 It is a name of the
 Variables
 Function
 array
 Structure
 Union…
 We can access the variables by the name.
RTS tech. 21
Rules to define the Identifiers
 It can start with alphabets and _under score.
 It cannot start with a digit. int 123test;
 It does not contains special symbols excluding
_underscore. int @name;
 It can precede with a digit. For ex. int test123;.
 We can not use keywords as identifiers.
 It is case sensitive.
 int a=10;
 int A=20; both are different for c.
RTS tech. 22
Operators
 Operators are symbol which perform some operation
when applied.
 We can categorize them according to the performed
operation like:
 Arithmetic Operators,
 Logical Operators,
 Bitwise operators
 Relational operators.
 etc…
RTS tech. 23
Precedence of Operators
 3+4+6
 3-2*10
 A=2+3*4
RTS tech. 24
Types of Operators and precedence
RTS tech. 25
Unary Operators
 () !
 ~ +(unary)
 sizeof() -(unary)
 ++ --
RTS tech. 26
Binary Operator
It operates on two variables.
X (op) Y
RTS tech. 27
Operands
Operator
Arithmetic Operators
Addition
Subtraction
Division
Multiply
Reminder
RTS tech. 28
+
-
/
*
%
Relational operators
<
>
<=
>=
!=
==
RTS tech. 29
Less Than
Greater Than
Less than equal
Greater than equal
Not equal
Equal to
Compound Operators
+=
-=
/=
*=
%=
RTS tech. 30
Add Assign
Subtract Assign
Divide Assign
Multiply Assign
Reminder Assign
Logical Operators
Logical And
&&
Logical OR
||
RTS tech. 31
Bitwise Operators
RTS tech. 32
<<
>>
~
&
|
^
Left shift
Right shift
Bitwise complement
Bitwise AND
Bitwise OR
Bitwise XOR
Unary bitwise complement
1 1 1 1 0 1 0 1
~
1 Byte
RTS Tech. 33
0 0 0 0 1 0 1 0
byte a = 10;
byte b = ~a;
RTS tech.
Left Shift <<
1 1 1 0 0 1 0 1
<<
1 Byte
1 0 0 1 0 1 0 0
byte a = 10;
b = a<<2;
RTS Tech. 34
RTS tech.
Right Shift >>
1 1 1 0 0 1 0 1
>>
1 Byte
1 0 0 1 1 0 0 1
byte a = 10;
b = a>>2;
RTS Tech. 35
RTS tech.
And bitwise &
1 1 1 0 0 1 0 1
&
1 Byte
RTS Tech. 36
0 0 1 1 1 0 0 1
byte a = 10;
b = 20;
c = a & b;
0 0 1 0 0 0 0 1
RTS tech.
OR bitwise |
1 1 1 0 0 1 0 1
|
1 Byte
37
0 0 1 1 1 0 0 1
byte a = 10;
b = 20;
c = a | b;
1 1 1 1 1 1 0 1
RTS tech.
XOR bitwise ^
1 1 1 0 0 1 0 1
^
1 Byte
38
0 0 1 1 1 0 0 1
byte a = 10;
b = 20;
c = a ^ b;
1 1 0 1 1 1 0 0
RTS tech.
Conditional operator
 It is also known as Ternary Operator.
 It is like if then else
 If (a<b) then a else b
 Result=(a<b)?a:b
RTS tech. 39
Type casting
 Conversion from one data type to another data
types.
 It can be done in two ways:
 Implicit: by compiler
 Explicit: by programmer
RTS tech. 40
Implicit type conversion
 It is automatically done by the compile.
 int x=5;
 double d=x;
 short int s=3;
 int i=s;
RTS tech. 41
Explicit type conversion
 It is done when some data loss is considerable.
 It is done by the programmer.
 double d=5.8;
 int i=(int)d;
RTS tech. 42
Constant in c
 Constant has fixed value, we can not change in the
program.
 We can create different kind of constant like int, float,
double, char, string etc.
 There is 2 ways to define constant in c
 Using const keyword
 const int AVG_AGE=60;
 Using #define preprocessor.
 #define PI 3.14
RTS tech. 43
Literals?
 Literal are the fixed value which is assigned to a
constant variable.
 Types of Literals:
 Integer
 int x=5;
 Float
 float f=5.6;
 Double
 double d=6.789;
 Character
 Char ch=‘A’;
RTS tech. 44
Control statements
 If-else
 while
 do-while
 for
 switch
 break
 continue
 goto.
RTS tech. 45
If-else
 #include<stdio.h>
 int main(){
 int age =0;
 printf("Enter age:n");
 scanf("%d",&age);
 If(age>=18){
 printf(“you can voten”);
 }else{
 printf(“You can not voten”);
 return 0;
 }
RTS tech. 46
While Loop
 It is a conditional loop.
 It executes till the condition is true.
RTS tech. 47
While loop (cont.)
 #include <stdio.h>
 #include <stdbool.h>
 int main(){
 int check;
 bool light_is_on=true;
 while(light_is_on){
 printf("Read a bookn");
 printf("Enter 0 if light has gonen");
 scanf("%d",&check);
 if(check==0){
 light_is_on=false;
 }
 }//end of while
 printf("Light has gone you can not readn");
 return 0;
 }
 RTS tech. 48
For loop
 It is a counter loop.
RTS tech. 49
How
Many
lives
to
play
For loop (cont.)
 #include <stdio.h>
 int main(){
 For(int lives=1; lives<=5; lives++){
 printf("Play game =%d timesn",lives);
 }
 printf("No more lives to play");
 }
RTS tech. 50
do-while loop
 It is also a conditional loop
 #include <stdio.h>
 int main(){
 int i=11;
 do{
 printf("%dn",i);
 i++;
 }while(i<=10);
 }
RTS tech. 51
Break & continue
 for(int =1; i<=5; i
++){
 if(i==3){
 continue;
 }
 printf(“%dn",i);
 }
 for(int =1; i<=5; i
++){
 if(i==3){
 break;
 }
 printf(“%dn",i);
 }
RTS tech. 52
goto statement
 It is a jump statement.
 Which is used to transfer the code to the specified
level.
 Not preferable because it makes program less
readable.
 it must be use in to break the multiple loop in some
condition.
RTS tech. 53
goto (cont.)
 #include <stdio.h>
 int main(){
 for(int i=1; i<=5; i++){
 for(int j=1; j<=5; j++){
 if( i==3){
 goto outer;
 }
 printf("%d",i);
 }printf("n");
 }
 outer: printf("outside the loop");
 }
RTS tech. 54
Decision using switch
 #include <stdio.h>
 int main(){
 int i=2,j=3,ch;
 printf("enter 1 for additionn");
 printf("enter 2 for subtractionn");
 scanf("%d",&ch);
 switch(ch){
 case 1:
 printf("Addition of %d and %d is=%dn",i,j,i+j);
 break;
 case 2:
 printf("Subtraction of %d and %d is=%dn",i,j,i-j);
 break;
 default:
 printf("Not a valid operationn");
 }return 0;
 }
RTS tech. 55
Character is also allowed
 #include<stdio.h>
 int main(){
 char ch;
 scanf("%c",&ch);
 printf("%c",ch);
 switch(ch){
 case 'A':
 printf("case An");
 break;
 case 'B':
 printf("Case Bn");
 default:
 printf("Default casen");}
 return 0;
 }
RTS tech. 56
String
 A string is a 1-dimensional character array ending with null
character(0).
 Each character occupies 1 byte memory.
 String constant is enclosed by double quotes.
 Char[] ch=“RTS Tech.”
 %s format specification is used to print and to get input as
string.
 scanf() function is not capable of receiving multiword
string.
 To receive multi word string we have gets() function.
 Counterpart of gets is puts which is used to print a single
string at a time
RTS tech. 57
String as character array
 char ch[]={'R','T','S','T','e','c','h',
'0'};
RTS tech. 58
R T S T e c h 0
0 1 2 3 4 5 6 7
String implementation as array
 #include<stdio.h>
 int main(){
 char ch[]={'R','T','S‘,'0'};
 printf("%sn",ch);
 return 0;
 }
RTS tech. 59
gets() & puts()
 #include<stdio.h>
 int main(){
 char name[30];
 printf("Enter Your namen");
 gets(name);
 puts(name);
 return 0;
 }
RTS tech. 60
String Functions
 #include<stdio.h>
 #include<string.h>
 int main(){
 char ch[30];
 printf("Enter Your Namen");
 gets(ch);
 printf("You Entered :%sn",ch);
 printf("%dn",strlen(ch));
 printf("%d",strcmp("O","N"));
 printf("%sn",strlwr(ch));
 printf("%sn",strrev(ch));
 printf("%sn",strupr(ch));
 printf("%sn",strcat(ch,"Hello"));
 return 0;
 }
RTS tech. 61
String Pointer
 #include<stdio.h>
 #include<string.h>
 int main(){
 char s[20]="RTS Tech";
 int *p=s;
 printf("%sn",p);
 printf("%sn",s);
 }
RTS tech. 62
Math
 #include<stdio.h>
 #include<math.h>
 int main(){
 printf("%fn",pow(2,2));
 printf("%fn",sqrt(4));
 printf("%fn",cbrt(27));
 printf("%dn",abs(-9.0));
 printf("%fn",exp(2));
 printf("%fn",fabs(-4.8));
 return 0;
 }
RTS tech. 63
Random Numbers
 #include<stdio.h>
 #include<stdlib.h>
 #include<time.h>
 int main(){
 //to get new random numbers every time
 srand(time(0));
 int random=rand();
 printf("Random number %d",random);
 return 0;
 }
RTS tech. 64
Random No in Range
 #include<stdio.h>
 #include<stdlib.h>
 #include<time.h>
 int main(){
 //to get new random numbers every time
 srand(time(0));
 int max=10, min=4;
 int random=(rand()%(max-min))+min;
 printf("Random number %d",random);
 return 0;
 }
RTS tech. 65
Pointers
 ‘*’ symbol is used as pointer operator.
 It is called value at address operator.
 It gives the value stored at particular address.
 It is also known as indirection operator
RTS tech. 66
Pointer Notation:
 Int j=10;
RTS tech. 67
j
10
10101
Memory Location name
value
Location address
Pointers(cont.)
 #include<stdio.h>
 int main(){
 int j=10;
 int *b;
 b=&j;
 printf("j=%dn",j);
 printf("b=%dn",b);
 printf("*b=%dn",*b);
 printf("&j=%dn",&j);
 return 0;
 }
RTS tech. 68
j
10
65524
b
65524
60500
Double Pointer
 #include<stdio.h>
 int main(){
 int i=10,*a,**b;
 a=&I,b=&a;
 printf(“i=%dn",i);
 printf("a=%dn",a);
 printf("b=%dn",b);
 printf("&i=%un",&i);
 printf("&a=%un",&a);
 printf("&b=%un",&b);
 printf("*a=%dn",*a);
 printf("**b=%dn",**b);
 return 0;
 }
RTS tech. 69
i
10
65524
a
65524
60500
b
60500
60504
Function
 Function is a block of statements to perform specific
task.
RTS tech. 70
Types of Functions
Library Functions
User Defined
Functions
printf()
scanf()
sum()
display()
Function With return type
 #include<stdio.h>
 int sum(int , int);
 int main(){
 int i=2, j=4;
 int result=sum(i,j);
 printf("%dn",result);
 return 0;
 }
 int sum(int a, int b){
 return a+b;
 }
RTS tech. 71
Function prototype
Function call
Function’s Definition
Function without return type
 #include<stdio.h>
 void sum(int , int);
 int main(){
 int i=2, j=4;
 sum(i,j);
 return 0;
 }
 void sum(int a, int b){
 printf("%dn",a+b);
 return;
 }
RTS tech. 72
Call by value
 #include<stdio.h>
 void swap(int, int);
 int main(){
 int x=12, y=10;
 swap(x,y);
 printf("x=%d, y=%dn",x,y);
 return 0;
 }
 void swap(int a, int b){
 int t=0;
 t=a;
 a=b;
 b=t;
 printf("a=%d, b=%dn",a,b);
 }
RTS tech. 73
Call by reference
 #include<stdio.h>
 void swap(int *, int *);
 int main(){
 int x=12, y=10;
 swap(&x,&y);
 printf("x=%d, y=%dn",x,y);
 return 0;
 }
 void swap(int *a, int *b){
 int t=0;
 t=*a;
 *a=*b;
 *b=t;
 }
RTS tech. 74
Recursion
 A function calls itself is known as recursive function.
 We have to define the termination condition for this
type of functions.
RTS tech. 75
Recursive Function
 #include<stdio.h>
 int fact(int);
 int fact(int n){
 if(n==1 || n==0){
 return 1;
 }else{
 return n*fact(n-1);
 }
 }
 int main(){
 printf("%dn",fact(5));
 return 0;
 }
RTS tech. 76
Array
 Array is used to store multiple elements of similar
type.
 Array stores all the elements in a continuous memory
locations.
 We can access elements of an array using index.
 Array index start with 0.
RTS tech. 77
One Dimensional Array
 int i[]={1,2,3,4,5};
 OR
 Array declaration
 int i[5];
 Assign value to an Array
 i[0]=2;
 Access Element from an array
 int Val=i[0];
RTS tech. 78
1
2
3
4
5
0
1
2
3
4
Multidimensional Array
RTS tech. 79
int a[][]={{1,0,1},{3,4,1}}
OR
int a[][3]={{1,0,1},{3,4,1}}
OR
int a[2][3]={{1,0,1},{3,4,1}}
1D Array 2D Array 3D Array
int a[]={3,2}
OR
int a[2];
int a[2][3][3];
Two Dimensional Array
RTS tech. 80
Two Dimensional Array
 #include<stdio.h>
 int main(){
 int arr[][3]={{1,2,3},{3,4,5},{6,7,8}}
;
 for(int i=0; i<3;i++){
 for(int j=0; j<3;j++){
 printf("%d",arr[i][j]);
 }
 printf("n");
 }
 return 0;
 }
RTS tech. 81
Three Dimensional Array
 #include <stdio.h>
 int main()
 { int a[2][5][5];
 for (int i = 0; i < 2; i++){
 for (int j = 0; j < 5; j++){
 for (int k = 0; k < 5; k++){
 a[i][j][k]=i+j+k;
 printf("%d", a[i][j][k]);
 }
 printf("n");
 }
 printf("n");
 }return 0;
 }
RTS tech. 82
Pointer to an array
RTS tech. 83
Pointer to array(cont.)
 #include<stdio.h>
 int main(){
 int a[]={1,2,3,4,5,6,7};
 int *p;
 p=a;
 printf("%un",a);
 printf("%un",p);
 for(int i=0; i<7;i++){
 printf("%dn",*(p+i));
 }//OR
 for(int i=0; i<7;i++){
 printf("%dn",a[i]);
 }
 return 0;
 }
RTS tech. 84
Structure
 It is a derived Data type in c.
 Structure is used to store different types of values under a
single name.
 struct keyword is used to create a structure in c.
 Syntax:
 struct structure_name{
 data_type var1;
 data_type var2;
 ...
 };
RTS tech. 85
Define structure variables
 We can define structure variable in two manner:
 Using struct keyword in main method.
 int main(){
 struct structure_name var1,var2;
 Define the variables at the time of defining structure.
 struct structure_name{
 data_type var1;
 data_type var2;
 }var1,var2;
RTS tech. 86
Structure example
 #include <stdio.h>
 #include <string.h>
 struct Person{
 char name[50];
 int age;
 char address[50];
 }p1;
 int main()
 { //struct Person p1;
 //Person's Info
 strcpy(p1.name, "Ram");
 strcpy(p1.address, "Indore");
 p1.age = 20;

//print details of the person
 printf("Name: %sn", p1.name);
 printf("Age: %dn", p1.age);
 printf("Address: %sn", p1.address);
 return 0;
 }
RTS tech. 87
Nested Structure
 #include <stdio.h>
 struct Person{
 char name[50];
 int age;};
 struct Student{
 int rollno;
 struct Person p;};
 int main(){
 struct Student std;
 printf("Enter Name , Age and RollNo of the studentn");
 scanf("%s %d %d", &std.p.name,&std.p.age, &std.rollno);
 printf("Name =%s , age=%d, rollno=%dn", std.p.name, st
d.p.age, std.rollno);
 return 0;
 }
RTS tech. 88
typedef
 It is a keyword in c to define the aliases to existing
names.
 We can give a name to existing data types and
structures etc.
 Syntax:
 typedef existing_name new_name;
RTS tech. 89
Typedef example
 #include <stdio.h>
 int main()
 {
 typedef unsigned int data;
 data a, b;
 printf("Enter the value of a and bn")
;
 scanf("%d %d", &a, &b);
 printf("a=%d, b=%dn", a, b);
 return 0;
 }
RTS tech. 90
Union
 It is a user defined data type.
 union keyword is used to define the union in c.
 Unlike structure union shares the same memory location.
 Union allocates memory of the data variables one at a
time.
 The size of the union will be the largest element in the
union.
 It is used to group the items.
RTS tech. 91
Union
 #include <stdio.h>
 union Person{
 char name[20];
 int age;
 char address[50];
 } p1;
 int main(){
 printf("Enter the namen");
 scanf("%s", &p1.name);
 printf("Name=%sn", p1.name);
 printf("Enter the addressn");
 scanf("%s", &p1.address);
 printf("Name=%sn", p1.address);
 printf("Enter the agen");
 scanf("%d", &p1.age);
 printf("age=%dn", p1.age);
 printf("%dn", sizeof(p1));
 return 0;}
RTS tech. 92
Preprocessor
 It pre-processed our program before compilation.
 It is also known as micro preprocessor.
 All preprocessor directives starts with ‘#’.
 Preprocessor directives can be placed anywhere in the program.
 Preprocessor’s List:
RTS tech. 93
#include #define #undef
#if #endif #else
#elif #ifdef #ifndef
#error #pragma
Define Macros
 Macros is a code segment which is replaced by its value.
 It makes program readable and Faster.
 Types:
 Object-Based (without argument)
 Function Based(with argument)
 Macro has 2 parts macro template and macro expansion.
 #define PI 3.14
RTS tech. 94
Macro template Macro expansion
Object Based
 #include <stdio.h>
 #define PI 3.14
 int main()
 {
 printf("EnterRadius of the Circlen");
 float radius;
 scanf("%f", &radius);
 float area = pi * radius * radius;
 printf("Area: %fn", area);
 return 0;
 }
RTS tech. 95
Function Based
 #include <stdio.h>
 #define PI 3.14
 #define AREA(r) (PI*r*r)
 int main()
 {
 printf("Enter Radius of the Circlen");
 float radius;
 scanf("%f", &radius);
 float area = AREA(radius);
 printf("Area: %fn", area);
 return 0;
 }
RTS tech. 96
Predefined Macros
 #include<stdio.h>
 int main(){
 printf("File :%sn", __FILE__ );
 printf("Date :%sn", __DATE__ );
 printf("Time :%sn", __TIME__ );
 printf("Line :%dn", __LINE__ );
 printf("STDC :%dn", __STDC__ );
 return 0;
 }
RTS tech. 97
Preprocessor Directives
Directives Uses
#include To include file either predefine or user define
#define To define Macros
#undef To undefine macros which is define by #define
#ifdef It checks and execute the macro which is define by
#define if not then #else is execute.
#if It checks the condition and execute the if block
#elif It It checks the condition and execute the elif block
#endif It is end of the #if
#ifndef It checks and execute the macro if not define by #define
otherwise #else is execute.
#error It indicates error
#pragma It provides additional information to the compiler.
RTS tech. 98
Command Line Arguments
 #include<stdio.h>
 int main(int argc, char *argv[]){
 printf("Total argument %dn",a);
 printf("Fle name:%sn",args[0]);
 printf("Hello %sn",args[1]);
 printf("%sn",args[2]);
 return 0;
 }
RTS tech. 99
File IO
 We can perform operation on file in C.
 WE can perform following operation on File:
 Creating a new file
 Opening a File
 Deleting a file
 Update the file
 Write to a file
 Read data from file
RTS tech. 100
Types of Memory
.NO Volatile Memory Non-Volatile Memory
1.
Data is lost as it is powered-
off.
Data remains stored even if it is
powered-off.
2. Contents is stored temporarily. Contents stored permanently.
3. It is faster. It is slower.
4. For ex. RAM. For ex. ROM
RTS tech. 101
File Modes
 r: to read existing file
 w: to write into a file if
exist otherwise creates
new
 a: to update a existing file
otherwise creates new
 rb: to read a binary file
 wb: to write a binary file
 ab: to append the data to a
binary.
 r+: for read write both
 w+: for read write both
 a+: for read write both, but
writing starts with end of
file.
 r+b(rb+): read write binary
file
 w+b(wb+): read write binary
file
 a+b(ab+): read write binary
file
RTS tech. 102
Writing to a Text File
 #include<stdio.h>
 int main(){
 FILE *file;
 file = fopen("test.txt","w");
 for (int i = 0; i <10; i++)
 {
 fprintf(file,"%s %dn","Line No",i);
 }
 printf("Data saved to file");
 fclose(file);
 return 0;
 }
RTS tech. 103
Reading from a Text File
 #include <stdio.h>
 int main(){
 FILE *fp;
 char buff[255];//creating char array to
store data of file
 fp = fopen("test.txt", "r");
 while(fscanf(fp, "%s", buff)!=EOF){
 printf("%s ", buff );
 }
 fclose(fp);
 return 0;
 }
RTS tech. 104
Write char by char
 #include <stdio.h>
 int main(){
 FILE *file;
 file = fopen("Test.txt", "w");
 for(int i=65; i<90; i++){
 fputc((char)i, file);
 }
 fclose(file);
 printf("Data saved");
 return 0;
 }
RTS tech. 105
Read Char by char
 #include <stdio.h>
 int main(){
 FILE *file;
 file = fopen("Test.txt", "r");
 char c=fgetc(file);
 while(c!=EOF){
 printf("%c",c);
 c=fgetc(file);
 }
 fclose(file);
 return 0;
 }
RTS tech. 106
Copy Binary File
 #include<stdio.h>
 #include<stdlib.h>
 int main(){
 FILE *from, *to;
 from = fopen(“Image.jpg","rb");
 to=fopen(“Copy.jpg","wb");
 int ch;
 ch=fgetc(from);
 while(ch != -1) {
 fputc(ch,to);
 ch=fgetc(from);
 }
 fclose(from);
 fclose(to);
return 0;
 }
RTS tech. 107
File Operation
RTS tech. 108
Operation Description
fopen() to open a new or existing file
fclose() to close a file()
fprintf() To write data
fscanf() To read data
fputc() To write single character
fgetc() To read single character
fputw() To write Integer
fgetw() To read Integer
fputs() To write data
fgets() To read data
rewind To set file pointer to the beginning.
seek() To set file pointer to the given position
ftell() Returns file pointer location
Enum
 It is User define data type.
 Enum keyword is used to create enum type.
 We can create constants by enum.
 It consists of integer values, and it provides meaningful
names to these values.
 We can define a enum type without a name
RTS tech. 109
Enum type
 enum Weekday{SUNDAY,MONDAY,TUESDAY,WEDN
ESDAY,THURSDAY,FRIDAY,SATURDAY};
 #include<stdio.h>
 int main(){
 enum Weekday day;
 day=SATURDAY;
 printf("Weekday %d",day);
 return 0;
 }
RTS tech. 110
Dynamic memory allocation
 We can assign memory at runtime.
 We need <stdlib.h> header file which contains 4
dynamic memory allocation function.
 malloc(): allocate single block of requested memory.
 calloc(): allocate multiple block of requested memory.
 Free(): frees dynamically allocated memory
 realloc(): reallocate memory occupied by malloc()
and calloc().
RTS tech. 111
malloc
 Defined in <stdlib.h> header file.
 Allocates requested memory.
 If allocation succeed returns pointer to the 1st byte else
return NULL or Void
 To avoid memory leak use free() to deallocate pointer.
 Syntax:
 void* malloc(size_t size);
RTS tech. 112
Implementation of malloc()
 #include <stdio.h>
 #include <stdlib.h>
 int main(){
 int *p1;
 p1 = malloc(sizeof(int[4]));
 if (p1){
 for (int i = 0; i < 4; i++){
 p1[i] = 2 * i + 1;
 }
 }for (int i = 0; i < 4; i++){
 printf("p1[%d]==%dn", i, p1[i]);
 }
 free(p1);
 return 0;
 }
RTS tech. 113
calloc()
 Defined in <stdlib.h> header file.
 Allocates memory according to number of objects.
 Initializes all bytes in the allocated storage to zero.
 If succeed returns pointer on failure return null pointer.
 Syntax:
 void* calloc( size_t num, size_t size
);
RTS tech. 114
Implementation of calloc()
 #include <stdio.h>
 #include <stdlib.h>
 int main(int argc, char *argv[]){
 // int *p1 = calloc(4, sizeof(int)); // allocate and zero out an
array of 4 int
 int *p1 = calloc(1, sizeof(int[4])); // same, naming the array typ
e directly
 // int *p3 = calloc(4, sizeof *p3); // same, without repeating
the type name
 if (p1) {
 for (int i = 0; i < 4; i++) {
 p1[i] = 2 * i + 1;
 }
 }for (int i = 0; i < 4; i++){
 printf("p1[%d]==%dn", i, p1[i]);
 }free(p1);
 return 0;
 }
RTS tech. 115
Disclaimer
 This is a educational Presentation to make
programming easier.
 We have used images of different URLs to make
presentation better.
 We respect the work of the owners of the URLs.
116
RTS tech.
Thank You!
RTS tech. 117
RTS Tech
:rtstech30@gmail.com
:+91 8818887087

More Related Content

What's hot

Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2Abdul Haseeb
 
Python Part 1
Python Part 1Python Part 1
Python Part 1Sunil OS
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classesIntro C# Book
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3Naveen Kumar
 
Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2Naveen Kumar
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1Abdul Haseeb
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul Haseeb
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#Giorgio Zoppi
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm ComplexityIntro C# Book
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsSvetlin Nakov
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio Zoppi
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstractionIntro C# Book
 
Java level 1 Quizzes
Java level 1 QuizzesJava level 1 Quizzes
Java level 1 QuizzesSteven Luo
 

What's hot (20)

Computer Programming- Lecture 3
Computer Programming- Lecture 3Computer Programming- Lecture 3
Computer Programming- Lecture 3
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
 
Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, Loops
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
 
09. Methods
09. Methods09. Methods
09. Methods
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
Java level 1 Quizzes
Java level 1 QuizzesJava level 1 Quizzes
Java level 1 Quizzes
 

Similar to C Programming Language

presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxKrishanPalSingh39
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerGaurav Verma
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptxvijayapraba1
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing TutorialMahira Banu
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)bolovv
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxKrishanPalSingh39
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 FocJAYA
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans incnayakq
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 

Similar to C Programming Language (20)

Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
dinoC_ppt.pptx
dinoC_ppt.pptxdinoC_ppt.pptx
dinoC_ppt.pptx
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
Advanced+pointers
Advanced+pointersAdvanced+pointers
Advanced+pointers
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptx
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
C tutorial
C tutorialC tutorial
C tutorial
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
 
Vcs14
Vcs14Vcs14
Vcs14
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 

Recently uploaded

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 

Recently uploaded (20)

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 

C Programming Language

  • 2. Features of C  It is simple and easy to use.  It is Primitive Programming Language.  It is structured programming language.  It is used in many popular application such as : Linux, Unix, MySQL etc…  It is platform independent.  It is case sensitive.  It is Fast.  It is Mid Level Programming Language. RTS tech. 2
  • 3. Learning Strategies  Learning Natural Language  Learning Computer Language: RTS tech. 3 Alphabets Words Sentences Paragraph Character Set Keywords Statements Program
  • 4. What is Program?  Program is a step by step solution to a problem.  Program contains :  Data(variables): To store the human data  Function: to perform action on stored data RTS tech. 4
  • 5. Structure of C Program RTS tech. 5
  • 6. Library in c  It is a collection of Reusable programs. RTS tech. 6
  • 7. Lets start coding.  //header file  # include <stdio.h>  //main function  void main(){  // to print output  printf(“Hello world!!!”);  } RTS tech. 7
  • 8. Basic Building Block  #include <stdio.h>: is a header file for input and output functions.  void : it is a keyword which means nothing.  main(): it is a entry point of the program.  printf(): it is a in-built function to print output on the console screen.  // : it is used to write comments. Comments not take parts in execution. RTS tech. 8
  • 9. How to compile and run ?  Save the file by the name Hello.c  Open command Prompt and compile the file.  C:>gcc Hello.c –o Hello  This command will generate Hello.exe file  Execute/Run  c:>Hello.exe RTS tech. 9 Hello.c Compile Hello.exe
  • 10. Compilation process of C  C:>gcc –Wall –save-temps test.c –o test  Run the command to generate the following files  Expanded code file (.i) extension  Assembly code file (.a) extension  Object code file(.obj) extension  Executable code file. (.exe) extension RTS tech. 10
  • 11. printf() function  This function is sued to write the text on the console window.  Format of printf():  printf(“format string”, argument list);  Format Strings are:  “%d”: it is used to write int  “%f”: ”: it is used to write float  “%c”: ”: it is used to write character  “%lf”: ”: it is used to write double  “%u”: for unsigned int  “%Lf”: for long double RTS tech. 11
  • 12. scanf() function  This function is used to receive inputs.  We can enter the data from the keyboard.  ‘&’ symbol is must in the scanf function to read the data.  & is an “Address of” operator.  For ex.  int a;  Scanf(“%d”,&a);  Above statement will read the integer value from the keyboard and stores it into variable a. RTS tech. 12
  • 13. Use of scanf()  #include <stdio.h>  int main(){  int a;  printf("Enter a no n");  scanf("%d",&a);  printf("Squre of entered No is : %dn",a*a);  return 0;  } RTS tech. 13
  • 14. Variables in C  Variables are the name of the memory Locations.  It is sued to store data.  Variables has different sizes according to the Data type.  We can access the value by variable name from the computer memory.  Declare a variable:  data_type var_name=value 14 30 int x=30; Memory X Data_type var_name value RTS tech.
  • 15. Basic data Types  #include<stdio.h>  void main(){  int i=2;  char ch='A';  float f=2.3f;  double d=4.567789;  short int s=2;  printf("%dn",i);  printf("%fn",f);  printf("%lfn",d);  printf("%cn",ch);  printf("%dn",s);  } RTS tech. 15
  • 16. Data Types  Data type is used to define the type of the data.  Size can be vary compiler to compiler RTS tech. 16 Types Data Type Basic int,char, float, double Derived array, structure, pointer, union Enumeration enum void void
  • 17. Data type Modifiers  It is used to modify the size of the Data types.  C provides the 4 Modifiers:  short: short integer no  short int a=1;  long: large integer no.  long int a=5678;  Long double a=2.345667;  Signed: contains positive(+) and negative(-) values.  signed int a=-2;  Unsigned: It accept only positive values.  unsigned int =+5; RTS tech. 17
  • 18. Get The size of the Data Types  #include<stdio.h>  void main(){  int i=2;  char ch='A';  float f=2.3f;  double d=4.567789;  short int s=2;  printf("Size of int %dn",sizeof(i));  printf("Size of float %dn",sizeof(f));  printf("Size of double %dn",sizeof(d));  printf("Size of char %dn",sizeof(ch));  printf("Size of short int %dn",sizeof(s));  } RTS tech. 18
  • 19. All data types Data Type Range Bytes Format signed char -128 to +127 1 %c unsigned char 0 to 255 1 %c short signed int -32768 to +32767 2 %d short unsigned int 0 to 65535 2 %u signed int -2147483648 to +2147483647 4 %d unsigned int 0 to 4294967295 4 %u long signed int -2147483648 to +2147483647 4 %ld long unsigned int 0 to 4294967295 4 %lu float -3.4e38 to +3.4e38 4 %f double -1.7e308 to +1.7e308 8 %lf long double -1.7e4932 to +1.7e4932 10 %Lf RTS tech. 19
  • 20. Keywords  Keywords are reserved words.  We can not use them to define user defined variables and function. RTS tech. 20
  • 21. Identifiers  It is a name of the  Variables  Function  array  Structure  Union…  We can access the variables by the name. RTS tech. 21
  • 22. Rules to define the Identifiers  It can start with alphabets and _under score.  It cannot start with a digit. int 123test;  It does not contains special symbols excluding _underscore. int @name;  It can precede with a digit. For ex. int test123;.  We can not use keywords as identifiers.  It is case sensitive.  int a=10;  int A=20; both are different for c. RTS tech. 22
  • 23. Operators  Operators are symbol which perform some operation when applied.  We can categorize them according to the performed operation like:  Arithmetic Operators,  Logical Operators,  Bitwise operators  Relational operators.  etc… RTS tech. 23
  • 24. Precedence of Operators  3+4+6  3-2*10  A=2+3*4 RTS tech. 24
  • 25. Types of Operators and precedence RTS tech. 25
  • 26. Unary Operators  () !  ~ +(unary)  sizeof() -(unary)  ++ -- RTS tech. 26
  • 27. Binary Operator It operates on two variables. X (op) Y RTS tech. 27 Operands Operator
  • 29. Relational operators < > <= >= != == RTS tech. 29 Less Than Greater Than Less than equal Greater than equal Not equal Equal to
  • 30. Compound Operators += -= /= *= %= RTS tech. 30 Add Assign Subtract Assign Divide Assign Multiply Assign Reminder Assign
  • 32. Bitwise Operators RTS tech. 32 << >> ~ & | ^ Left shift Right shift Bitwise complement Bitwise AND Bitwise OR Bitwise XOR
  • 33. Unary bitwise complement 1 1 1 1 0 1 0 1 ~ 1 Byte RTS Tech. 33 0 0 0 0 1 0 1 0 byte a = 10; byte b = ~a; RTS tech.
  • 34. Left Shift << 1 1 1 0 0 1 0 1 << 1 Byte 1 0 0 1 0 1 0 0 byte a = 10; b = a<<2; RTS Tech. 34 RTS tech.
  • 35. Right Shift >> 1 1 1 0 0 1 0 1 >> 1 Byte 1 0 0 1 1 0 0 1 byte a = 10; b = a>>2; RTS Tech. 35 RTS tech.
  • 36. And bitwise & 1 1 1 0 0 1 0 1 & 1 Byte RTS Tech. 36 0 0 1 1 1 0 0 1 byte a = 10; b = 20; c = a & b; 0 0 1 0 0 0 0 1 RTS tech.
  • 37. OR bitwise | 1 1 1 0 0 1 0 1 | 1 Byte 37 0 0 1 1 1 0 0 1 byte a = 10; b = 20; c = a | b; 1 1 1 1 1 1 0 1 RTS tech.
  • 38. XOR bitwise ^ 1 1 1 0 0 1 0 1 ^ 1 Byte 38 0 0 1 1 1 0 0 1 byte a = 10; b = 20; c = a ^ b; 1 1 0 1 1 1 0 0 RTS tech.
  • 39. Conditional operator  It is also known as Ternary Operator.  It is like if then else  If (a<b) then a else b  Result=(a<b)?a:b RTS tech. 39
  • 40. Type casting  Conversion from one data type to another data types.  It can be done in two ways:  Implicit: by compiler  Explicit: by programmer RTS tech. 40
  • 41. Implicit type conversion  It is automatically done by the compile.  int x=5;  double d=x;  short int s=3;  int i=s; RTS tech. 41
  • 42. Explicit type conversion  It is done when some data loss is considerable.  It is done by the programmer.  double d=5.8;  int i=(int)d; RTS tech. 42
  • 43. Constant in c  Constant has fixed value, we can not change in the program.  We can create different kind of constant like int, float, double, char, string etc.  There is 2 ways to define constant in c  Using const keyword  const int AVG_AGE=60;  Using #define preprocessor.  #define PI 3.14 RTS tech. 43
  • 44. Literals?  Literal are the fixed value which is assigned to a constant variable.  Types of Literals:  Integer  int x=5;  Float  float f=5.6;  Double  double d=6.789;  Character  Char ch=‘A’; RTS tech. 44
  • 45. Control statements  If-else  while  do-while  for  switch  break  continue  goto. RTS tech. 45
  • 46. If-else  #include<stdio.h>  int main(){  int age =0;  printf("Enter age:n");  scanf("%d",&age);  If(age>=18){  printf(“you can voten”);  }else{  printf(“You can not voten”);  return 0;  } RTS tech. 46
  • 47. While Loop  It is a conditional loop.  It executes till the condition is true. RTS tech. 47
  • 48. While loop (cont.)  #include <stdio.h>  #include <stdbool.h>  int main(){  int check;  bool light_is_on=true;  while(light_is_on){  printf("Read a bookn");  printf("Enter 0 if light has gonen");  scanf("%d",&check);  if(check==0){  light_is_on=false;  }  }//end of while  printf("Light has gone you can not readn");  return 0;  }  RTS tech. 48
  • 49. For loop  It is a counter loop. RTS tech. 49 How Many lives to play
  • 50. For loop (cont.)  #include <stdio.h>  int main(){  For(int lives=1; lives<=5; lives++){  printf("Play game =%d timesn",lives);  }  printf("No more lives to play");  } RTS tech. 50
  • 51. do-while loop  It is also a conditional loop  #include <stdio.h>  int main(){  int i=11;  do{  printf("%dn",i);  i++;  }while(i<=10);  } RTS tech. 51
  • 52. Break & continue  for(int =1; i<=5; i ++){  if(i==3){  continue;  }  printf(“%dn",i);  }  for(int =1; i<=5; i ++){  if(i==3){  break;  }  printf(“%dn",i);  } RTS tech. 52
  • 53. goto statement  It is a jump statement.  Which is used to transfer the code to the specified level.  Not preferable because it makes program less readable.  it must be use in to break the multiple loop in some condition. RTS tech. 53
  • 54. goto (cont.)  #include <stdio.h>  int main(){  for(int i=1; i<=5; i++){  for(int j=1; j<=5; j++){  if( i==3){  goto outer;  }  printf("%d",i);  }printf("n");  }  outer: printf("outside the loop");  } RTS tech. 54
  • 55. Decision using switch  #include <stdio.h>  int main(){  int i=2,j=3,ch;  printf("enter 1 for additionn");  printf("enter 2 for subtractionn");  scanf("%d",&ch);  switch(ch){  case 1:  printf("Addition of %d and %d is=%dn",i,j,i+j);  break;  case 2:  printf("Subtraction of %d and %d is=%dn",i,j,i-j);  break;  default:  printf("Not a valid operationn");  }return 0;  } RTS tech. 55
  • 56. Character is also allowed  #include<stdio.h>  int main(){  char ch;  scanf("%c",&ch);  printf("%c",ch);  switch(ch){  case 'A':  printf("case An");  break;  case 'B':  printf("Case Bn");  default:  printf("Default casen");}  return 0;  } RTS tech. 56
  • 57. String  A string is a 1-dimensional character array ending with null character(0).  Each character occupies 1 byte memory.  String constant is enclosed by double quotes.  Char[] ch=“RTS Tech.”  %s format specification is used to print and to get input as string.  scanf() function is not capable of receiving multiword string.  To receive multi word string we have gets() function.  Counterpart of gets is puts which is used to print a single string at a time RTS tech. 57
  • 58. String as character array  char ch[]={'R','T','S','T','e','c','h', '0'}; RTS tech. 58 R T S T e c h 0 0 1 2 3 4 5 6 7
  • 59. String implementation as array  #include<stdio.h>  int main(){  char ch[]={'R','T','S‘,'0'};  printf("%sn",ch);  return 0;  } RTS tech. 59
  • 60. gets() & puts()  #include<stdio.h>  int main(){  char name[30];  printf("Enter Your namen");  gets(name);  puts(name);  return 0;  } RTS tech. 60
  • 61. String Functions  #include<stdio.h>  #include<string.h>  int main(){  char ch[30];  printf("Enter Your Namen");  gets(ch);  printf("You Entered :%sn",ch);  printf("%dn",strlen(ch));  printf("%d",strcmp("O","N"));  printf("%sn",strlwr(ch));  printf("%sn",strrev(ch));  printf("%sn",strupr(ch));  printf("%sn",strcat(ch,"Hello"));  return 0;  } RTS tech. 61
  • 62. String Pointer  #include<stdio.h>  #include<string.h>  int main(){  char s[20]="RTS Tech";  int *p=s;  printf("%sn",p);  printf("%sn",s);  } RTS tech. 62
  • 63. Math  #include<stdio.h>  #include<math.h>  int main(){  printf("%fn",pow(2,2));  printf("%fn",sqrt(4));  printf("%fn",cbrt(27));  printf("%dn",abs(-9.0));  printf("%fn",exp(2));  printf("%fn",fabs(-4.8));  return 0;  } RTS tech. 63
  • 64. Random Numbers  #include<stdio.h>  #include<stdlib.h>  #include<time.h>  int main(){  //to get new random numbers every time  srand(time(0));  int random=rand();  printf("Random number %d",random);  return 0;  } RTS tech. 64
  • 65. Random No in Range  #include<stdio.h>  #include<stdlib.h>  #include<time.h>  int main(){  //to get new random numbers every time  srand(time(0));  int max=10, min=4;  int random=(rand()%(max-min))+min;  printf("Random number %d",random);  return 0;  } RTS tech. 65
  • 66. Pointers  ‘*’ symbol is used as pointer operator.  It is called value at address operator.  It gives the value stored at particular address.  It is also known as indirection operator RTS tech. 66
  • 67. Pointer Notation:  Int j=10; RTS tech. 67 j 10 10101 Memory Location name value Location address
  • 68. Pointers(cont.)  #include<stdio.h>  int main(){  int j=10;  int *b;  b=&j;  printf("j=%dn",j);  printf("b=%dn",b);  printf("*b=%dn",*b);  printf("&j=%dn",&j);  return 0;  } RTS tech. 68 j 10 65524 b 65524 60500
  • 69. Double Pointer  #include<stdio.h>  int main(){  int i=10,*a,**b;  a=&I,b=&a;  printf(“i=%dn",i);  printf("a=%dn",a);  printf("b=%dn",b);  printf("&i=%un",&i);  printf("&a=%un",&a);  printf("&b=%un",&b);  printf("*a=%dn",*a);  printf("**b=%dn",**b);  return 0;  } RTS tech. 69 i 10 65524 a 65524 60500 b 60500 60504
  • 70. Function  Function is a block of statements to perform specific task. RTS tech. 70 Types of Functions Library Functions User Defined Functions printf() scanf() sum() display()
  • 71. Function With return type  #include<stdio.h>  int sum(int , int);  int main(){  int i=2, j=4;  int result=sum(i,j);  printf("%dn",result);  return 0;  }  int sum(int a, int b){  return a+b;  } RTS tech. 71 Function prototype Function call Function’s Definition
  • 72. Function without return type  #include<stdio.h>  void sum(int , int);  int main(){  int i=2, j=4;  sum(i,j);  return 0;  }  void sum(int a, int b){  printf("%dn",a+b);  return;  } RTS tech. 72
  • 73. Call by value  #include<stdio.h>  void swap(int, int);  int main(){  int x=12, y=10;  swap(x,y);  printf("x=%d, y=%dn",x,y);  return 0;  }  void swap(int a, int b){  int t=0;  t=a;  a=b;  b=t;  printf("a=%d, b=%dn",a,b);  } RTS tech. 73
  • 74. Call by reference  #include<stdio.h>  void swap(int *, int *);  int main(){  int x=12, y=10;  swap(&x,&y);  printf("x=%d, y=%dn",x,y);  return 0;  }  void swap(int *a, int *b){  int t=0;  t=*a;  *a=*b;  *b=t;  } RTS tech. 74
  • 75. Recursion  A function calls itself is known as recursive function.  We have to define the termination condition for this type of functions. RTS tech. 75
  • 76. Recursive Function  #include<stdio.h>  int fact(int);  int fact(int n){  if(n==1 || n==0){  return 1;  }else{  return n*fact(n-1);  }  }  int main(){  printf("%dn",fact(5));  return 0;  } RTS tech. 76
  • 77. Array  Array is used to store multiple elements of similar type.  Array stores all the elements in a continuous memory locations.  We can access elements of an array using index.  Array index start with 0. RTS tech. 77
  • 78. One Dimensional Array  int i[]={1,2,3,4,5};  OR  Array declaration  int i[5];  Assign value to an Array  i[0]=2;  Access Element from an array  int Val=i[0]; RTS tech. 78 1 2 3 4 5 0 1 2 3 4
  • 79. Multidimensional Array RTS tech. 79 int a[][]={{1,0,1},{3,4,1}} OR int a[][3]={{1,0,1},{3,4,1}} OR int a[2][3]={{1,0,1},{3,4,1}} 1D Array 2D Array 3D Array int a[]={3,2} OR int a[2]; int a[2][3][3];
  • 81. Two Dimensional Array  #include<stdio.h>  int main(){  int arr[][3]={{1,2,3},{3,4,5},{6,7,8}} ;  for(int i=0; i<3;i++){  for(int j=0; j<3;j++){  printf("%d",arr[i][j]);  }  printf("n");  }  return 0;  } RTS tech. 81
  • 82. Three Dimensional Array  #include <stdio.h>  int main()  { int a[2][5][5];  for (int i = 0; i < 2; i++){  for (int j = 0; j < 5; j++){  for (int k = 0; k < 5; k++){  a[i][j][k]=i+j+k;  printf("%d", a[i][j][k]);  }  printf("n");  }  printf("n");  }return 0;  } RTS tech. 82
  • 83. Pointer to an array RTS tech. 83
  • 84. Pointer to array(cont.)  #include<stdio.h>  int main(){  int a[]={1,2,3,4,5,6,7};  int *p;  p=a;  printf("%un",a);  printf("%un",p);  for(int i=0; i<7;i++){  printf("%dn",*(p+i));  }//OR  for(int i=0; i<7;i++){  printf("%dn",a[i]);  }  return 0;  } RTS tech. 84
  • 85. Structure  It is a derived Data type in c.  Structure is used to store different types of values under a single name.  struct keyword is used to create a structure in c.  Syntax:  struct structure_name{  data_type var1;  data_type var2;  ...  }; RTS tech. 85
  • 86. Define structure variables  We can define structure variable in two manner:  Using struct keyword in main method.  int main(){  struct structure_name var1,var2;  Define the variables at the time of defining structure.  struct structure_name{  data_type var1;  data_type var2;  }var1,var2; RTS tech. 86
  • 87. Structure example  #include <stdio.h>  #include <string.h>  struct Person{  char name[50];  int age;  char address[50];  }p1;  int main()  { //struct Person p1;  //Person's Info  strcpy(p1.name, "Ram");  strcpy(p1.address, "Indore");  p1.age = 20;  //print details of the person  printf("Name: %sn", p1.name);  printf("Age: %dn", p1.age);  printf("Address: %sn", p1.address);  return 0;  } RTS tech. 87
  • 88. Nested Structure  #include <stdio.h>  struct Person{  char name[50];  int age;};  struct Student{  int rollno;  struct Person p;};  int main(){  struct Student std;  printf("Enter Name , Age and RollNo of the studentn");  scanf("%s %d %d", &std.p.name,&std.p.age, &std.rollno);  printf("Name =%s , age=%d, rollno=%dn", std.p.name, st d.p.age, std.rollno);  return 0;  } RTS tech. 88
  • 89. typedef  It is a keyword in c to define the aliases to existing names.  We can give a name to existing data types and structures etc.  Syntax:  typedef existing_name new_name; RTS tech. 89
  • 90. Typedef example  #include <stdio.h>  int main()  {  typedef unsigned int data;  data a, b;  printf("Enter the value of a and bn") ;  scanf("%d %d", &a, &b);  printf("a=%d, b=%dn", a, b);  return 0;  } RTS tech. 90
  • 91. Union  It is a user defined data type.  union keyword is used to define the union in c.  Unlike structure union shares the same memory location.  Union allocates memory of the data variables one at a time.  The size of the union will be the largest element in the union.  It is used to group the items. RTS tech. 91
  • 92. Union  #include <stdio.h>  union Person{  char name[20];  int age;  char address[50];  } p1;  int main(){  printf("Enter the namen");  scanf("%s", &p1.name);  printf("Name=%sn", p1.name);  printf("Enter the addressn");  scanf("%s", &p1.address);  printf("Name=%sn", p1.address);  printf("Enter the agen");  scanf("%d", &p1.age);  printf("age=%dn", p1.age);  printf("%dn", sizeof(p1));  return 0;} RTS tech. 92
  • 93. Preprocessor  It pre-processed our program before compilation.  It is also known as micro preprocessor.  All preprocessor directives starts with ‘#’.  Preprocessor directives can be placed anywhere in the program.  Preprocessor’s List: RTS tech. 93 #include #define #undef #if #endif #else #elif #ifdef #ifndef #error #pragma
  • 94. Define Macros  Macros is a code segment which is replaced by its value.  It makes program readable and Faster.  Types:  Object-Based (without argument)  Function Based(with argument)  Macro has 2 parts macro template and macro expansion.  #define PI 3.14 RTS tech. 94 Macro template Macro expansion
  • 95. Object Based  #include <stdio.h>  #define PI 3.14  int main()  {  printf("EnterRadius of the Circlen");  float radius;  scanf("%f", &radius);  float area = pi * radius * radius;  printf("Area: %fn", area);  return 0;  } RTS tech. 95
  • 96. Function Based  #include <stdio.h>  #define PI 3.14  #define AREA(r) (PI*r*r)  int main()  {  printf("Enter Radius of the Circlen");  float radius;  scanf("%f", &radius);  float area = AREA(radius);  printf("Area: %fn", area);  return 0;  } RTS tech. 96
  • 97. Predefined Macros  #include<stdio.h>  int main(){  printf("File :%sn", __FILE__ );  printf("Date :%sn", __DATE__ );  printf("Time :%sn", __TIME__ );  printf("Line :%dn", __LINE__ );  printf("STDC :%dn", __STDC__ );  return 0;  } RTS tech. 97
  • 98. Preprocessor Directives Directives Uses #include To include file either predefine or user define #define To define Macros #undef To undefine macros which is define by #define #ifdef It checks and execute the macro which is define by #define if not then #else is execute. #if It checks the condition and execute the if block #elif It It checks the condition and execute the elif block #endif It is end of the #if #ifndef It checks and execute the macro if not define by #define otherwise #else is execute. #error It indicates error #pragma It provides additional information to the compiler. RTS tech. 98
  • 99. Command Line Arguments  #include<stdio.h>  int main(int argc, char *argv[]){  printf("Total argument %dn",a);  printf("Fle name:%sn",args[0]);  printf("Hello %sn",args[1]);  printf("%sn",args[2]);  return 0;  } RTS tech. 99
  • 100. File IO  We can perform operation on file in C.  WE can perform following operation on File:  Creating a new file  Opening a File  Deleting a file  Update the file  Write to a file  Read data from file RTS tech. 100
  • 101. Types of Memory .NO Volatile Memory Non-Volatile Memory 1. Data is lost as it is powered- off. Data remains stored even if it is powered-off. 2. Contents is stored temporarily. Contents stored permanently. 3. It is faster. It is slower. 4. For ex. RAM. For ex. ROM RTS tech. 101
  • 102. File Modes  r: to read existing file  w: to write into a file if exist otherwise creates new  a: to update a existing file otherwise creates new  rb: to read a binary file  wb: to write a binary file  ab: to append the data to a binary.  r+: for read write both  w+: for read write both  a+: for read write both, but writing starts with end of file.  r+b(rb+): read write binary file  w+b(wb+): read write binary file  a+b(ab+): read write binary file RTS tech. 102
  • 103. Writing to a Text File  #include<stdio.h>  int main(){  FILE *file;  file = fopen("test.txt","w");  for (int i = 0; i <10; i++)  {  fprintf(file,"%s %dn","Line No",i);  }  printf("Data saved to file");  fclose(file);  return 0;  } RTS tech. 103
  • 104. Reading from a Text File  #include <stdio.h>  int main(){  FILE *fp;  char buff[255];//creating char array to store data of file  fp = fopen("test.txt", "r");  while(fscanf(fp, "%s", buff)!=EOF){  printf("%s ", buff );  }  fclose(fp);  return 0;  } RTS tech. 104
  • 105. Write char by char  #include <stdio.h>  int main(){  FILE *file;  file = fopen("Test.txt", "w");  for(int i=65; i<90; i++){  fputc((char)i, file);  }  fclose(file);  printf("Data saved");  return 0;  } RTS tech. 105
  • 106. Read Char by char  #include <stdio.h>  int main(){  FILE *file;  file = fopen("Test.txt", "r");  char c=fgetc(file);  while(c!=EOF){  printf("%c",c);  c=fgetc(file);  }  fclose(file);  return 0;  } RTS tech. 106
  • 107. Copy Binary File  #include<stdio.h>  #include<stdlib.h>  int main(){  FILE *from, *to;  from = fopen(“Image.jpg","rb");  to=fopen(“Copy.jpg","wb");  int ch;  ch=fgetc(from);  while(ch != -1) {  fputc(ch,to);  ch=fgetc(from);  }  fclose(from);  fclose(to); return 0;  } RTS tech. 107
  • 108. File Operation RTS tech. 108 Operation Description fopen() to open a new or existing file fclose() to close a file() fprintf() To write data fscanf() To read data fputc() To write single character fgetc() To read single character fputw() To write Integer fgetw() To read Integer fputs() To write data fgets() To read data rewind To set file pointer to the beginning. seek() To set file pointer to the given position ftell() Returns file pointer location
  • 109. Enum  It is User define data type.  Enum keyword is used to create enum type.  We can create constants by enum.  It consists of integer values, and it provides meaningful names to these values.  We can define a enum type without a name RTS tech. 109
  • 110. Enum type  enum Weekday{SUNDAY,MONDAY,TUESDAY,WEDN ESDAY,THURSDAY,FRIDAY,SATURDAY};  #include<stdio.h>  int main(){  enum Weekday day;  day=SATURDAY;  printf("Weekday %d",day);  return 0;  } RTS tech. 110
  • 111. Dynamic memory allocation  We can assign memory at runtime.  We need <stdlib.h> header file which contains 4 dynamic memory allocation function.  malloc(): allocate single block of requested memory.  calloc(): allocate multiple block of requested memory.  Free(): frees dynamically allocated memory  realloc(): reallocate memory occupied by malloc() and calloc(). RTS tech. 111
  • 112. malloc  Defined in <stdlib.h> header file.  Allocates requested memory.  If allocation succeed returns pointer to the 1st byte else return NULL or Void  To avoid memory leak use free() to deallocate pointer.  Syntax:  void* malloc(size_t size); RTS tech. 112
  • 113. Implementation of malloc()  #include <stdio.h>  #include <stdlib.h>  int main(){  int *p1;  p1 = malloc(sizeof(int[4]));  if (p1){  for (int i = 0; i < 4; i++){  p1[i] = 2 * i + 1;  }  }for (int i = 0; i < 4; i++){  printf("p1[%d]==%dn", i, p1[i]);  }  free(p1);  return 0;  } RTS tech. 113
  • 114. calloc()  Defined in <stdlib.h> header file.  Allocates memory according to number of objects.  Initializes all bytes in the allocated storage to zero.  If succeed returns pointer on failure return null pointer.  Syntax:  void* calloc( size_t num, size_t size ); RTS tech. 114
  • 115. Implementation of calloc()  #include <stdio.h>  #include <stdlib.h>  int main(int argc, char *argv[]){  // int *p1 = calloc(4, sizeof(int)); // allocate and zero out an array of 4 int  int *p1 = calloc(1, sizeof(int[4])); // same, naming the array typ e directly  // int *p3 = calloc(4, sizeof *p3); // same, without repeating the type name  if (p1) {  for (int i = 0; i < 4; i++) {  p1[i] = 2 * i + 1;  }  }for (int i = 0; i < 4; i++){  printf("p1[%d]==%dn", i, p1[i]);  }free(p1);  return 0;  } RTS tech. 115
  • 116. Disclaimer  This is a educational Presentation to make programming easier.  We have used images of different URLs to make presentation better.  We respect the work of the owners of the URLs. 116 RTS tech.
  • 117. Thank You! RTS tech. 117 RTS Tech :rtstech30@gmail.com :+91 8818887087

Editor's Notes

  1. www.sunilos.com