SlideShare une entreprise Scribd logo
1  sur  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;
}

Contenu connexe

Tendances

Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
Manisha Keim
 
C PROGRAMMING LANGUAGE
C  PROGRAMMING  LANGUAGEC  PROGRAMMING  LANGUAGE
C PROGRAMMING LANGUAGE
PRASANYA K
 

Tendances (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++
 

Similaire à Lecture 3.mte 407

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
ComedyTechnology
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
Kumaran K
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
Ganesh Samarthyam
 

Similaire à Lecture 3.mte 407 (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
 

Dernier

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

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
 
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 ...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
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
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
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
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
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...
 

Lecture 3.mte 407

  • 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; }