Cprogram Theory
Cprogram Theory
Structure is a user-defined data type that enables us to store the collection of different data
types. Each element of a structure is called a member.
The,struct keyword is used to define the structure.
Syntax:
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Array Structure
Array is a collection of homogeneous Structure is a collection of heterogenous
data. data.
Array data are access using index. Structure data are access using “.”
(dot)Operator.
Array allocates static memory. Structure allocates dynamic memory.
Individual entries in array are called Individual entries in structure are called
elements. member.
“struct” keyword is used. No keyword is used.
Array requires less time to access. Structure requires more time to access.
Individual member can be accessed at a time. Only one member can be accessed at a time.
Several member can be initialized at once. Only first member can be initialized at once
Function:
A function is a self contained block of statements that performs a particular task or job.
In the figure, we can see that main() calls a function named func1(). Therefore, main() is known as the
calling function and func1() is known as the called function.
The moment the compiler encounters a function call, the control jumps to the statements that are a
part of the called function. After the called function is executed, the control is returned to the calling
program.
Advantage of functions:
Dividing the program into separate well-defined functions facilitates each function to be
written and tested separately. This simplifies the process of getting the total program to work.
Understanding, coding, and testing multiple separate functions is easier than doing the same
for one big function.
If a big program has to be developed without using any function other than main(), then there
will be countless lines in the main() function and maintaining that program will be a difficult
task.
When a big program is broken into comparatively smaller functions, then different programmers
working on that project can divide the workload by writing different functions.
Types of functions:
Recursive Function:
A function that keeps on calling itself is called recursive function. One critical requirement of
recursive functions is termination point or base case. Every recursive program must have
base case to make sure that the function will terminate. Missing base case results in
unexpected behavior.
Modes of File handling:
File opening modes or access modes specify the allowed operations on the file to
be opened. They are passed as an argument to the fopen() function.
Read Mode ("r"): This mode allows reading data from the file. When a file
is opened in read mode, the file must exist, and the file pointer is positioned
at the beginning of the file. If the file does not exist or cannot be opened,
fopen() returns NULL.
FILE *file;
file = fopen("filename.txt", "r");
Write Mode ("w"): This mode allows writing data to the file. If the file
already exists, it will be truncated (i.e., its contents will be erased) before
writing. If the file does not exist, a new file is created. If fopen() fails to open
the file, it returns NULL
FILE *file;
file = fopen("filename.txt", "w");
Append Mode ("a"): This mode allows writing data to the end of the file. If
the file does not exist, it is created. If the file exists, the file pointer is
positioned at the end of the file, and new data is appended. If fopen() fails to
open the file, it returns NULL.
FILE *file;
file = fopen("filename.txt", "a");
File handling Functions:
fopen():
For opening a file in C, the fopen() function is used with the filename or file path
along with the required access modes.
FILE *fptr;
fptr = fopen("filename.txt", "w");
fscanf():
The file read operation in C can be performed using functions fscanf().
FILE * fptr;
fptr = fopen(“fileName.txt”, “r”);
fscanf(fptr, "%s %s", &name, &address);
fprintf():
The file write operations can be performed by the functions fprintf().
FILE *fptr;
fptr = fopen(“fileName.txt”, “w”);
fprintf(fptr, "%s",name);
fclose():
The fclose() function is used to close the file. After successful file operations, you
must always close a file to remove it from the memory.
fclose(fptr);
Pointer:
Pointer is a variable that stores the address of another variable. It is
declared using de-reference operator(*).
Example: int *a;
Advantages of pointer:
Pointers in C programming are helpful to access a memory location
Pointers are used for the allocation of dynamic memory and the distribution
Pointers are used to build complicated data structures like a linked list, graph, tree, etc