0% found this document useful (0 votes)
3 views

Cprogram Theory

The document discusses different data structures in C including structures, unions, and arrays. It provides details on how each is declared and their key differences. Functions are also covered including user defined vs library functions and different types of functions. File handling in C and common file handling functions like fopen, fscanf, fprintf and fclose are explained. Pointers in C are briefly introduced.

Uploaded by

yohijust8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Cprogram Theory

The document discusses different data structures in C including structures, unions, and arrays. It provides details on how each is declared and their key differences. Functions are also covered including user defined vs library functions and different types of functions. File handling in C and common file handling functions like fopen, fscanf, fprintf and fclose are explained. Pointers in C are briefly introduced.

Uploaded by

yohijust8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Structure:

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.

Array is declared as: Structure is declared as:


int a[10]; struct student
{
int roll;
char name[40];
};
Structure Union
The keyword struct is used to define a The keyword union is used to define a union.
structure.
The size of structure is greater than or equal The size of union is equal to the size of the
to the sum of size of its member. greatest member.

Each member is assigned a unique storage. Memory is shared by individual members.

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:

1. User defined functions


2. Library(built-in or predefined) functions
Library function User defined function
Library function is a predefined function in a User defined function is not a predefined
header file or preprocessor directive. function, it is defined by the programmer
according to the need.
Programmer can simply use this function by Programmer has to declare, define and use
including respective header file. this function by themselves.
The program using library function will be The program using user defined function will
usually short as the programmer doesn’t be usually lengthy as the programmer has to
have to define the function. define the function.
Program development time will be faster. Program development time will be usually
slower.
The program using library function will be The program using user defined function will
usually simple. be usually complex.
This function requires header file to use it. This function doesn’t require header file.
Example: printf(), scanf(), strlen(). Example: sum(), factorial().

Types of user-defined function:


1. Function with return with arguments:
This type of function has two way communication. Here, an argument is passed from the
calling function to the called function, and there will also be return statement in the called
function.

2. Function with return without arguments:


This type of function has only one way communication. Here, an argument is passed from
the calling function to the called function, but there is no need of return statement in the
called function.
3. Function without return with arguments:
This type of function has one way communication. Here, an argument is not passed from the
calling function to the called function, but there is need of return statement in the called
function.
4. Function without return without arguments:
Here we simply call a function without passing arguments and the called function doesn’t
have to return any values to the calling function.

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 an effective way to access the array structure elements

 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

You might also like