SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Rumana Tasnim
Lecture 3
Data Structure
 They are used to store data in organized form in computer.
the term “array” is an example of Data Structure.
 Some other examples are- Stack, Queue, Linked List, Tree
etc.
Data Types:
They basically specify the type of data you are storing in
your system
 Any information we want to store is an information for our
system.
 Its type will explain many things to our system, one being
the space(number of bytes) its gonna take in the memory.
 Specifying the data type clears the category of the
 Data structure is a way of describing a certain way to
organize pieces of data so that operations and alogrithms
can be more easily applied.
For example: tree type data structures often allow for efficient
searching algorithms.
 A data type describes pieces of data that all share a common
property.
For example: an integer data type describes every integer that
the computer can handle. Often when referring to data types
in practice you will be referencing primitive data types which
are the most basic data types of a programming language for
example int, string or array. However in some languages like C
you can define your own data types for example a data type of
`person` could contain a string labeled `name` and an integer
called `age` which could be used to store the information for
 Structure is a collection of variables of different types
under a single name.
 For example: I want to store some information about a
person: his/her name, citizenship number and salary. I
can easily create different variables name, citNo, salary
to store these information separately.
 However, in the future, I would want to store
information about multiple persons. Now, I need to
create different variables for each information per
person: name1, citNo1, salary1, name2, citNo2, salary2
 You can easily visualize how big and messy the code
would look. Also, since no relation between the
variables (information) would exist, it's going to be a
daunting task.
 Keyword struct is used for creating a structure.
Syntax of structure
struct structure_name
{ data_type member1;
data_type member2;
data_type memeber; };
Note: Don't forget the semicolon }; in the ending line.
 We can create the structure for a person as
mentioned above as:
 struct person
{
char name[50];
int citNo;
float salary;
};
 Another way of creating a structure variable is:
struct person
{ char name[50];
Int citNo; float salary;
}
person1, person2, person3[20];
In this case, two variables person1, person2 and an
array person3 having 20 elements of type struct
person are created.
Example of structures
1. Write a C program to add two distances entered
by user. Measurement of distance should be in
inch and feet. (Note: 12 inches = 1 foot)
2. Write a C Program to Store Information of a
Student Using Structure and and Display it Using
Structure
 In this program, a structure, student is created.
 This structure has three
members: name (string), roll (integer) and marks (float).
 Then, a structure variable s is created to store information
 As we know, we have to declare the size of an
array before we use it. Hence, the array I
declared may be insufficient or more than
required to hold data. To solve this issue, we can
allocate memory dynamically.
 Dynamic memory management refers to manual
memory management. This allows us to obtain
more memory when required and release it when
not necessary.
 Although C inherently does not have any technique to
allocate memory dynamically, there are 4 library
functions defined under <stdlib.h> for dynamic
memory allocation.
 Pointer is a variable that points to the address
of another variable.
 For Example, to allocate a memory using
malloc(), we need to point the specific memory
using pointer and size of variable casting.
Malloc() dynamically allocates a memory.
 ptr means address
 *ptr-the value of pointer address (pointer er
address e j value ta ase take bojhay
 Free(ptr) – we can release memory using
free(ptr), otherwise it will continue collect
garbage. Free() will delete unnecessary
garbages.
1. Write a C program to find sum of n elements entered
by user using C malloc() and free()
2. Write a C program to Store Information Using
Structures with Dynamically Memory Allocation
This program asks user to store the value of no of Records and allocates the
memory for the no of records structure variable dynamically using malloc()
function.
Example:
 Preprocessors are a way of making text processing with C program
before they are actually compiled. Before the actual compilation
of every C program it is passed through a Preprocessor.
 Preprocessors are programs which process the input data and
produces an output which is taken as input for another program.
In simpler words, it is another program that processes our
program and sends it to the compiler.
 The Preprocessor looks through the program trying to find out
specific instructions called Preprocessor directives that it can
understand.
 All Preprocessor directives begin with the # (hash) symbol. C++
compilers use the same C preprocessor.
 The Preprocessor is a part of the compiler which performs
preliminary operations (conditionally compiling code, including files
etc...) to your code before the compiler sees it. These transformations
are lexical, meaning that the output of the preprocessor is still text.
 The first stage of converting a C code into program is the pre-
processing stage (code is not program).
 It is used to include header files, line control, macro expansions and
conditional compilation. In C programming it is mostly used by the
compiler at the beginning of the code to include certain functionalities
that come with the header files
 In many C implementations, it is a separate program invoked by
the compiler as the first part of translation.
 The language of preprocessor directives is only weakly related to the
grammar of C, and so is sometimes used to process other kinds of text
files.
The C preprocessor is a macro preprocessor (allows
you to define macros) that transforms your program
before it is compiled.
These transformations can be inclusion of header file,
macro expansions etc.
All preprocessing directives begins with a # symbol.
For example,
#define PI 3.14
 Some of the common uses of C preprocessor are:
Include header files
Macros
Conditional Compilation
Diagnostics
Line Control
Pragmas
Other Directives
Preprocessor Output
 Macros in C are string replacements commonly used to
define constants.
 For example:-
 #define MAX 1000
 It sets the value of identifier MAX to 1000 and whenever
you want to use 1000 in your program you can use MAX
instead of writing 1000.
 Similarly ,
 #define PI 3.1415926
 Whenever you have to use value of pi in your code you can
simply write PI instead of writing lengthy value of pi again
and again.
 Basically macros are one of the Preprocessor directives
available in C. Macro substitution is a process where an
identifier in a program is replaced by a predefined string.
Example 1: Using #define preprocessor
to find area of a circle.
#include <stdio.h>
#define PI 3.1415
int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%d", &radius);
// Notice, the use of PI
area = PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}
 You can also define macros that works like a
function call, known as function-like macros.
For example,
 define circleArea(r) (3.1415*r*r)

#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)
int main()
{
int radius;
float area;
printf("Enter the radius: ");
scanf("%d", &radius);
area = circleArea(radius);
printf("Area = %.2f", area);
return 0;
}

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Data Types
Data TypesData Types
Data Types
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
Lect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer AbbasLect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer Abbas
 
cs8251 unit 1 ppt
cs8251 unit 1 pptcs8251 unit 1 ppt
cs8251 unit 1 ppt
 
Typedef
TypedefTypedef
Typedef
 
C PROGRAMMING LANGUAGE
C  PROGRAMMING  LANGUAGEC  PROGRAMMING  LANGUAGE
C PROGRAMMING LANGUAGE
 
Data types
Data typesData types
Data types
 
Data Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn HubData Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn Hub
 
Structure in c
Structure in cStructure in c
Structure in c
 
Programming Fundamentals lecture 6
Programming Fundamentals lecture 6Programming Fundamentals lecture 6
Programming Fundamentals lecture 6
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-c
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
Data types
Data typesData types
Data types
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Oo ps exam answer2
Oo ps exam answer2Oo ps exam answer2
Oo ps exam answer2
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
Notes on c++
Notes on c++Notes on c++
Notes on c++
 

Ähnlich wie Data Structures and Preprocessors Explained in C

C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...Rowank2
 
Ch-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxCh-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxChereLemma2
 
C programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfC programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfComedyTechnology
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&aKumaran K
 
Interview Questions For C Language .pptx
Interview Questions For C Language .pptxInterview Questions For C Language .pptx
Interview Questions For C Language .pptxRowank2
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokePranoti Doke
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++TemplatesGanesh Samarthyam
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Typesk v
 
Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language Rowank2
 

Ähnlich wie Data Structures and Preprocessors Explained in C (20)

Data types
Data typesData types
Data types
 
Structures
StructuresStructures
Structures
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
 
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
 
Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)
 
Ch-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxCh-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptx
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
 
C programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfC programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdf
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
 
interview questions.docx
interview questions.docxinterview questions.docx
interview questions.docx
 
Interview Questions For C Language .pptx
Interview Questions For C Language .pptxInterview Questions For C Language .pptx
Interview Questions For C Language .pptx
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
C tutorials
C tutorialsC tutorials
C tutorials
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
 
Data structures
Data structuresData structures
Data structures
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language
 
What is c language
What is c languageWhat is c language
What is c language
 

Kürzlich hochgeladen

CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESkarthi keyan
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmDeepika Walanjkar
 
Robotics Group 10 (Control Schemes) cse.pdf
Robotics Group 10  (Control Schemes) cse.pdfRobotics Group 10  (Control Schemes) cse.pdf
Robotics Group 10 (Control Schemes) cse.pdfsahilsajad201
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfManish Kumar
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfisabel213075
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxStephen Sitton
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.elesangwon
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming languageSmritiSharma901052
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfalene1
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewsandhya757531
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodManicka Mamallan Andavar
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfBalamuruganV28
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptJohnWilliam111370
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 

Kürzlich hochgeladen (20)

CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
 
Robotics Group 10 (Control Schemes) cse.pdf
Robotics Group 10  (Control Schemes) cse.pdfRobotics Group 10  (Control Schemes) cse.pdf
Robotics Group 10 (Control Schemes) cse.pdf
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdf
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptx
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overview
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument method
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 

Data Structures and Preprocessors Explained in C

  • 2. Data Structure  They are used to store data in organized form in computer. the term “array” is an example of Data Structure.  Some other examples are- Stack, Queue, Linked List, Tree etc. Data Types: They basically specify the type of data you are storing in your system  Any information we want to store is an information for our system.  Its type will explain many things to our system, one being the space(number of bytes) its gonna take in the memory.  Specifying the data type clears the category of the
  • 3.  Data structure is a way of describing a certain way to organize pieces of data so that operations and alogrithms can be more easily applied. For example: tree type data structures often allow for efficient searching algorithms.  A data type describes pieces of data that all share a common property. For example: an integer data type describes every integer that the computer can handle. Often when referring to data types in practice you will be referencing primitive data types which are the most basic data types of a programming language for example int, string or array. However in some languages like C you can define your own data types for example a data type of `person` could contain a string labeled `name` and an integer called `age` which could be used to store the information for
  • 4.
  • 5.
  • 6.
  • 7.  Structure is a collection of variables of different types under a single name.  For example: I want to store some information about a person: his/her name, citizenship number and salary. I can easily create different variables name, citNo, salary to store these information separately.  However, in the future, I would want to store information about multiple persons. Now, I need to create different variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2  You can easily visualize how big and messy the code would look. Also, since no relation between the variables (information) would exist, it's going to be a daunting task.
  • 8.  Keyword struct is used for creating a structure. Syntax of structure struct structure_name { data_type member1; data_type member2; data_type memeber; }; Note: Don't forget the semicolon }; in the ending line.
  • 9.  We can create the structure for a person as mentioned above as:  struct person { char name[50]; int citNo; float salary; };
  • 10.  Another way of creating a structure variable is: struct person { char name[50]; Int citNo; float salary; } person1, person2, person3[20]; In this case, two variables person1, person2 and an array person3 having 20 elements of type struct person are created.
  • 11. Example of structures 1. Write a C program to add two distances entered by user. Measurement of distance should be in inch and feet. (Note: 12 inches = 1 foot) 2. Write a C Program to Store Information of a Student Using Structure and and Display it Using Structure  In this program, a structure, student is created.  This structure has three members: name (string), roll (integer) and marks (float).  Then, a structure variable s is created to store information
  • 12.
  • 13.  As we know, we have to declare the size of an array before we use it. Hence, the array I declared may be insufficient or more than required to hold data. To solve this issue, we can allocate memory dynamically.  Dynamic memory management refers to manual memory management. This allows us to obtain more memory when required and release it when not necessary.  Although C inherently does not have any technique to allocate memory dynamically, there are 4 library functions defined under <stdlib.h> for dynamic memory allocation.
  • 14.  Pointer is a variable that points to the address of another variable.  For Example, to allocate a memory using malloc(), we need to point the specific memory using pointer and size of variable casting. Malloc() dynamically allocates a memory.  ptr means address  *ptr-the value of pointer address (pointer er address e j value ta ase take bojhay  Free(ptr) – we can release memory using free(ptr), otherwise it will continue collect garbage. Free() will delete unnecessary garbages.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19. 1. Write a C program to find sum of n elements entered by user using C malloc() and free() 2. Write a C program to Store Information Using Structures with Dynamically Memory Allocation This program asks user to store the value of no of Records and allocates the memory for the no of records structure variable dynamically using malloc() function. Example:
  • 20.
  • 21.  Preprocessors are a way of making text processing with C program before they are actually compiled. Before the actual compilation of every C program it is passed through a Preprocessor.  Preprocessors are programs which process the input data and produces an output which is taken as input for another program. In simpler words, it is another program that processes our program and sends it to the compiler.  The Preprocessor looks through the program trying to find out specific instructions called Preprocessor directives that it can understand.  All Preprocessor directives begin with the # (hash) symbol. C++ compilers use the same C preprocessor.
  • 22.  The Preprocessor is a part of the compiler which performs preliminary operations (conditionally compiling code, including files etc...) to your code before the compiler sees it. These transformations are lexical, meaning that the output of the preprocessor is still text.  The first stage of converting a C code into program is the pre- processing stage (code is not program).  It is used to include header files, line control, macro expansions and conditional compilation. In C programming it is mostly used by the compiler at the beginning of the code to include certain functionalities that come with the header files  In many C implementations, it is a separate program invoked by the compiler as the first part of translation.  The language of preprocessor directives is only weakly related to the grammar of C, and so is sometimes used to process other kinds of text files.
  • 23. The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your program before it is compiled. These transformations can be inclusion of header file, macro expansions etc. All preprocessing directives begins with a # symbol. For example, #define PI 3.14
  • 24.  Some of the common uses of C preprocessor are: Include header files Macros Conditional Compilation Diagnostics Line Control Pragmas Other Directives Preprocessor Output
  • 25.  Macros in C are string replacements commonly used to define constants.  For example:-  #define MAX 1000  It sets the value of identifier MAX to 1000 and whenever you want to use 1000 in your program you can use MAX instead of writing 1000.  Similarly ,  #define PI 3.1415926  Whenever you have to use value of pi in your code you can simply write PI instead of writing lengthy value of pi again and again.  Basically macros are one of the Preprocessor directives available in C. Macro substitution is a process where an identifier in a program is replaced by a predefined string.
  • 26.
  • 27. Example 1: Using #define preprocessor to find area of a circle. #include <stdio.h> #define PI 3.1415 int main() { float radius, area; printf("Enter the radius: "); scanf("%d", &radius); // Notice, the use of PI area = PI*radius*radius; printf("Area=%.2f",area); return 0; }
  • 28.  You can also define macros that works like a function call, known as function-like macros. For example,  define circleArea(r) (3.1415*r*r) 
  • 29. #include <stdio.h> #define PI 3.1415 #define circleArea(r) (PI*r*r) int main() { int radius; float area; printf("Enter the radius: "); scanf("%d", &radius); area = circleArea(radius); printf("Area = %.2f", area); return 0; }