SlideShare a Scribd company logo
1 of 25
Programming in C
Avishek Bhattacharjee
(B-TECH Student at HETC, 3+ Years of Programming Experience as of 2022)
What is Programming?
• Whenever we try to do some operations or give some problems, or think
some logics and steps to solve a problem is called Programming.
• Simply just do or run or think a program through sequence of steps is called
Programing. And this sequence of steps is called Algorithm.
Avishek Bhattacharjee
What is Programming Language?
• In Simple language like English, Bengali or Hindi, there must have some
vocabulary what you must know to learn this language, simply in
programming, there is a specified language what compiler or interpreter can
only understand, this specified code of language is called Programming
Language.
• In simple way which language is used to do a program is called Programming
Language.
Avishek Bhattacharjee
Types of Programing Language
Avishek Bhattacharjee
What is Compiler?
• Operating System of a system software make a layer which helps to convert
user input codes to machine language codes (0-1) and shows the output by
converting it to the same way to user known language. It’s close to machine.
Hardware
System Software
Compiler
Application Software
Avishek Bhattacharjee
History of C
 On 1970/1971 C was developed by Dennis Ritchie at AT&T Bell laboratories.
(It was popular after the invention of compiler on 1980.)
Why it is called C?
Ans: Before C(CPL) was born there was B(BCPL). So it was chosen c while working on UNIX operating
system project.
Avishek Bhattacharjee
Basic Syntax Rules for C
Programming
• C is a case sensitive language so all C
instructions must be written in lower case
letter.
• All C statement must be end with a
semicolon.
• Whitespace is used in C to describe
blanks and tabs.
• Whitespace is required between
keywords and identifiers.
Avishek Bhattacharjee
Avishek Bhattacharjee
Avishek Bhattacharjee
Some Logical Reasoning
• What if there is once more main function?
The Compiler will get confused where to start
program and give an error.
• What does the & mean in C
Programming?
& is called ampersand, it is often used in scanf
function inside the parentheses after a , (comma)
to show the address of a declaring variable. E.g.
scanf(“%d”, &declaring_variable);
• What is called string?
String is usually a collection of letters, numbers,
symbols under double and triple quotes(“string”).
• Why is it called printf rather than print?
f stands for format here, f means formatting
parameters inside parentheses and read input that
conforms with a format string declared in stdio.h.
Avishek Bhattacharjee
Avishek Bhattacharjee
KEYWORDS
(A type of predefined reserve words used in Programming)
• Character Set: Set of letters,
alphabets and some special
characters that are valid in c.
• Datatype: Type of a Data.
• Identifiers: Refers to the name what
can help to identify of an certain
entity
• Constant: Const. can be used to
declare constant variables whose
values remain unchanged when
initialized.
• Operator: Operator is a symbol
that operate values or variables.
Avishek Bhattacharjee
32 Keywords in C
auto break char case do continue default const
else double enum extern for goto if float
short int long signed unsigned volatile while register
return typedef union switch void sizeof static struct
Avishek Bhattacharjee
auto break character case do continue default constant else double enum extern for goto if float, short int long
signed, unsigned volatile while register return typedef union switch void sizeof static struct.
Simple Way to remind through this sentence.
Avishek Bhattacharjee
Explanations of Variable Types
• Local Variable: Those variable is
declared inside the function or
block called Local Variable.
• Global Variable: Declared outside
the function/block.
• Static Variable: Retains value in
multiple functions. Situates local
and other variables.
• Automatic Variable: All variables in
c that are declared inside the block
are called Automatic variable.
• External Variable: External variable
can be shared in multiple c files
declared using extern keyword.
Avishek Bhattacharjee
CONSTANTS
Primary Constants Secondary Constants
Avishek Bhattacharjee
Integer Constants(Num)
Real Constants(float)
Character Constants(a,b,c…)
Array: Contiguous memory locations of same data type.
Pointer: Containing a variable memory address.
Structure: Collection of any user defined data type
Union: A data type that allows to store different variables of
different data types in the same memory location.
Operators
Arithmetic Increment & Decrement Assignment Logical Bitwise Relational
Avishek Bhattacharjee
+ (Unary Plus)
- (Unary Minus)
*(Multiplication)
/ (Division)
% (Remainder)
++ (Increment)
-- (Decrement)
=
+=
-=
*=
/=
%=
==
>=
<=
&&
(Logical
AND)
||
(Logical
OR)
! (Logical
NOT)
If the relation
is true, it
returns one; if
the relation is
false, it returns
zero.
& (Bitwise
AND)
| (Bitwise OR)
^ (Bitwise
exclusive OR)
~ (Bitwise
Complement)
<< (Shift Left)
>> (Shift Right)
Basic Concept of Memory Allocations
• Within computer memory, every stored data item occupies one or more
contiguous memory cell.
• Whenever we declare a variable, the system(compiler) allocates memory
locations to hold the value of variable and variable must have some address
while it gets allocated to memory locations.
• Variable address and assigned value are not same. Let’s see now pointer
concept on next slide to understand properly.
Avishek Bhattacharjee
Pointer Concept
What is memory?
• Memory is called as racks or place of
locations. Each and every variable,
compiler allocates a memory of some
space. Memory Address
variable1
variable2
variable3
What is pointer?
• Pointer is what contains the
address of a variable allocated to a
memory location. It can be
changed anytime. See the previous
figure that pointer 1 is containing
2600 after it contains 2700, but
mind that a pointer can’t contain
both variable address at a time.
Pointer 1 2600
Pointer 1 2700
Pointer 3 2800
Avishek Bhattacharjee
Pointer Expression
• An integer can add or subtract from a pointer.
• Subtract one pointer to another but addition of two pointer is not allowed in
c.
a. Explanations: Suppose p1 and p2 are two pointers, the following
statements are valid;
b. (p1-p2); *p1(address of p1)+2; *p2-3
Avishek Bhattacharjee
Storing memory layout for c
1. Text Segment: Code Segment or some text segment containing
instructions situated under heap and stack shown on the pic,
read-only.
2. Initialized Data Segment: Simple data segment containing of
global and static variable of the virtual address place.
3. Uninitialized Data Segment: BSS[Block started by symbol]
another name, start at the end of text segment and all previously
declared variables initialized to zero without any clear
initialization in source.
4. Stack: adjoined the heap area and grew in the opposite direction
and exhaust free memory.
5. Heap: the segment where dynamic memory allocation usually
takes place. The heap area begins at the end of the BSS segment
and grows to larger addresses from there.
Avishek Bhattacharjee
Dynamic Memory Locations
Theory with logics
• Every data in nature is dynamic, so we have already known that when we
declare a variable, compiler must allocate some place to memory.
• On the array definition, we know that array means contiguous memory
locations of same data types, if we consider array as variable which value is
integer or float or double then it will take some place and the value what is
assigned will be the storage space for a compiler time of entire memory. So
if variables are changing or static in nature then its memory locations are
dynamic that means it’s changing as per time of compilation.
Avishek Bhattacharjee
Memory Allocations Functions
• malloc : - (Allocates requested number of bytes and returns pointer to the first byte
of the allocated space.)
• calloc : - (Allocates space for an array of elements, and initialize them to zero and
returns a pointer to the memory.)
• free : - (Frees previously allocated space.)
• realloc : - (Modifies the previously allocated space.)
A block of memory of specified size can be allocated or reserved using malloc and
returns a pointer type of void.
Pointer type= (type*)malloc(byte_size)); ex: (int*)malloc(100*size of(int));
Avishek Bhattacharjee
80001 80005 80009
In c programming, we are often used dynamic memory locations
techniques to safe wastage of memory space.
X2
x[3]
x[1] x[2]
X1 X3
x[0]
80005
80009
80013
80017
80021
Avishek Bhattacharjee
Variable A
Variable B
Variable C
Variable D
A Block of memory is reserved
here through malloc function and
you can see (p) the pointer type of
void returns here and it’s pointed
through it.
p
Here calloc function is
used and all elements
are initialized to zero
and a pointer returns
to the memory.
Array
• Array is a collection of same data types located in contiguous memory locations.
Suppose there is arr[10] that means n(number of indices or elements)=10 so if we
start from 0 so we will get upto 9(0,1,2…,9) here.
Start Address+ index*number of bytes
If the above element is int, so size is 4. we can say a2= a1+0*4
Avishek Bhattacharjee
a1 a2 a3
Index :-0 Index :-1 Index :-2

More Related Content

Similar to Programming in C.pptx

Similar to Programming in C.pptx (20)

C language
C languageC language
C language
 
7986-lect 7.pdf
7986-lect 7.pdf7986-lect 7.pdf
7986-lect 7.pdf
 
C language
C languageC language
C language
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
 
C intro
C introC intro
C intro
 
C for Engineers
C for EngineersC for Engineers
C for Engineers
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
c-programming
c-programmingc-programming
c-programming
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
 
Programming Fundamental Slide lecture no.2 (Section E)
Programming Fundamental Slide lecture no.2 (Section E)Programming Fundamental Slide lecture no.2 (Section E)
Programming Fundamental Slide lecture no.2 (Section E)
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
C-PPT.pdf
C-PPT.pdfC-PPT.pdf
C-PPT.pdf
 
Pc module1
Pc module1Pc module1
Pc module1
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
C interview questions
C interview questionsC interview questions
C interview questions
 

Recently uploaded

PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
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
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 

Recently uploaded (20)

PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
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
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 

Programming in C.pptx

  • 1. Programming in C Avishek Bhattacharjee (B-TECH Student at HETC, 3+ Years of Programming Experience as of 2022)
  • 2. What is Programming? • Whenever we try to do some operations or give some problems, or think some logics and steps to solve a problem is called Programming. • Simply just do or run or think a program through sequence of steps is called Programing. And this sequence of steps is called Algorithm. Avishek Bhattacharjee
  • 3. What is Programming Language? • In Simple language like English, Bengali or Hindi, there must have some vocabulary what you must know to learn this language, simply in programming, there is a specified language what compiler or interpreter can only understand, this specified code of language is called Programming Language. • In simple way which language is used to do a program is called Programming Language. Avishek Bhattacharjee
  • 4. Types of Programing Language Avishek Bhattacharjee
  • 5. What is Compiler? • Operating System of a system software make a layer which helps to convert user input codes to machine language codes (0-1) and shows the output by converting it to the same way to user known language. It’s close to machine. Hardware System Software Compiler Application Software Avishek Bhattacharjee
  • 6. History of C  On 1970/1971 C was developed by Dennis Ritchie at AT&T Bell laboratories. (It was popular after the invention of compiler on 1980.) Why it is called C? Ans: Before C(CPL) was born there was B(BCPL). So it was chosen c while working on UNIX operating system project. Avishek Bhattacharjee
  • 7. Basic Syntax Rules for C Programming • C is a case sensitive language so all C instructions must be written in lower case letter. • All C statement must be end with a semicolon. • Whitespace is used in C to describe blanks and tabs. • Whitespace is required between keywords and identifiers. Avishek Bhattacharjee
  • 10. Some Logical Reasoning • What if there is once more main function? The Compiler will get confused where to start program and give an error. • What does the & mean in C Programming? & is called ampersand, it is often used in scanf function inside the parentheses after a , (comma) to show the address of a declaring variable. E.g. scanf(“%d”, &declaring_variable); • What is called string? String is usually a collection of letters, numbers, symbols under double and triple quotes(“string”). • Why is it called printf rather than print? f stands for format here, f means formatting parameters inside parentheses and read input that conforms with a format string declared in stdio.h. Avishek Bhattacharjee
  • 12. KEYWORDS (A type of predefined reserve words used in Programming) • Character Set: Set of letters, alphabets and some special characters that are valid in c. • Datatype: Type of a Data. • Identifiers: Refers to the name what can help to identify of an certain entity • Constant: Const. can be used to declare constant variables whose values remain unchanged when initialized. • Operator: Operator is a symbol that operate values or variables. Avishek Bhattacharjee
  • 13. 32 Keywords in C auto break char case do continue default const else double enum extern for goto if float short int long signed unsigned volatile while register return typedef union switch void sizeof static struct Avishek Bhattacharjee auto break character case do continue default constant else double enum extern for goto if float, short int long signed, unsigned volatile while register return typedef union switch void sizeof static struct. Simple Way to remind through this sentence.
  • 15. Explanations of Variable Types • Local Variable: Those variable is declared inside the function or block called Local Variable. • Global Variable: Declared outside the function/block. • Static Variable: Retains value in multiple functions. Situates local and other variables. • Automatic Variable: All variables in c that are declared inside the block are called Automatic variable. • External Variable: External variable can be shared in multiple c files declared using extern keyword. Avishek Bhattacharjee
  • 16. CONSTANTS Primary Constants Secondary Constants Avishek Bhattacharjee Integer Constants(Num) Real Constants(float) Character Constants(a,b,c…) Array: Contiguous memory locations of same data type. Pointer: Containing a variable memory address. Structure: Collection of any user defined data type Union: A data type that allows to store different variables of different data types in the same memory location.
  • 17. Operators Arithmetic Increment & Decrement Assignment Logical Bitwise Relational Avishek Bhattacharjee + (Unary Plus) - (Unary Minus) *(Multiplication) / (Division) % (Remainder) ++ (Increment) -- (Decrement) = += -= *= /= %= == >= <= && (Logical AND) || (Logical OR) ! (Logical NOT) If the relation is true, it returns one; if the relation is false, it returns zero. & (Bitwise AND) | (Bitwise OR) ^ (Bitwise exclusive OR) ~ (Bitwise Complement) << (Shift Left) >> (Shift Right)
  • 18. Basic Concept of Memory Allocations • Within computer memory, every stored data item occupies one or more contiguous memory cell. • Whenever we declare a variable, the system(compiler) allocates memory locations to hold the value of variable and variable must have some address while it gets allocated to memory locations. • Variable address and assigned value are not same. Let’s see now pointer concept on next slide to understand properly. Avishek Bhattacharjee
  • 19. Pointer Concept What is memory? • Memory is called as racks or place of locations. Each and every variable, compiler allocates a memory of some space. Memory Address variable1 variable2 variable3 What is pointer? • Pointer is what contains the address of a variable allocated to a memory location. It can be changed anytime. See the previous figure that pointer 1 is containing 2600 after it contains 2700, but mind that a pointer can’t contain both variable address at a time. Pointer 1 2600 Pointer 1 2700 Pointer 3 2800 Avishek Bhattacharjee
  • 20. Pointer Expression • An integer can add or subtract from a pointer. • Subtract one pointer to another but addition of two pointer is not allowed in c. a. Explanations: Suppose p1 and p2 are two pointers, the following statements are valid; b. (p1-p2); *p1(address of p1)+2; *p2-3 Avishek Bhattacharjee
  • 21. Storing memory layout for c 1. Text Segment: Code Segment or some text segment containing instructions situated under heap and stack shown on the pic, read-only. 2. Initialized Data Segment: Simple data segment containing of global and static variable of the virtual address place. 3. Uninitialized Data Segment: BSS[Block started by symbol] another name, start at the end of text segment and all previously declared variables initialized to zero without any clear initialization in source. 4. Stack: adjoined the heap area and grew in the opposite direction and exhaust free memory. 5. Heap: the segment where dynamic memory allocation usually takes place. The heap area begins at the end of the BSS segment and grows to larger addresses from there. Avishek Bhattacharjee
  • 22. Dynamic Memory Locations Theory with logics • Every data in nature is dynamic, so we have already known that when we declare a variable, compiler must allocate some place to memory. • On the array definition, we know that array means contiguous memory locations of same data types, if we consider array as variable which value is integer or float or double then it will take some place and the value what is assigned will be the storage space for a compiler time of entire memory. So if variables are changing or static in nature then its memory locations are dynamic that means it’s changing as per time of compilation. Avishek Bhattacharjee
  • 23. Memory Allocations Functions • malloc : - (Allocates requested number of bytes and returns pointer to the first byte of the allocated space.) • calloc : - (Allocates space for an array of elements, and initialize them to zero and returns a pointer to the memory.) • free : - (Frees previously allocated space.) • realloc : - (Modifies the previously allocated space.) A block of memory of specified size can be allocated or reserved using malloc and returns a pointer type of void. Pointer type= (type*)malloc(byte_size)); ex: (int*)malloc(100*size of(int)); Avishek Bhattacharjee
  • 24. 80001 80005 80009 In c programming, we are often used dynamic memory locations techniques to safe wastage of memory space. X2 x[3] x[1] x[2] X1 X3 x[0] 80005 80009 80013 80017 80021 Avishek Bhattacharjee Variable A Variable B Variable C Variable D A Block of memory is reserved here through malloc function and you can see (p) the pointer type of void returns here and it’s pointed through it. p Here calloc function is used and all elements are initialized to zero and a pointer returns to the memory.
  • 25. Array • Array is a collection of same data types located in contiguous memory locations. Suppose there is arr[10] that means n(number of indices or elements)=10 so if we start from 0 so we will get upto 9(0,1,2…,9) here. Start Address+ index*number of bytes If the above element is int, so size is 4. we can say a2= a1+0*4 Avishek Bhattacharjee a1 a2 a3 Index :-0 Index :-1 Index :-2