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