C programming, RTS Tech. Indore
This is a presentation to take your skills to next level. Hope you will like our work to make programming easier for you.
Feel free to contact for the online/offline batches.
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
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
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
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
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
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
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];
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
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
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
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
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.