CA - Unit-2 Fundamentals of Programming
CA - Unit-2 Fundamentals of Programming
Fundamentals of Programming:
Decision Making in C (if , if..else, Nested if, if-else-if )
• The conditional statements (also known as decision control structures) such
as if, if else, switch, etc. are used for decision-making purposes in C
programs.
• There can also be multiple conditions like in C if x occurs then execute p, else
if condition y occurs execute q, else execute r. This condition of C else-if is
one of the many ways of importing multiple conditions.
Types of Conditional Statements in C
Loops
• In programming, a loop is used to repeat a block of code until the specified
condition is met.
1. for loop
2. while loop
3. do...while loop
Advantage of loops in C
s1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or
linked lists).
do-while loop in C
• The do-while loop continues until a given condition satisfies. It is also called
post tested loop. It is used when it is necessary to execute the loop at least
once (mostly menu driven programs).
do{
//code to be executed
}while(condition);
while loop in C
• The while loop in c is to be used in the scenario where we don't know the
number of iterations in advance.
• The block of statements is executed in the while loop until the condition
specified in the while loop is satisfied. It is also called a pre-tested loop.
• The syntax of while loop in c language is given below:
while(condition){
//code to be executed
}
for loop in C
• The for loop is used in the case where we need to execute some part of the
code until the given condition is satisfied. The for loop is also called as a per-
tested loop. It is better to use for loop if the number of iteration is known in
advance.
for(initialization;condition;incr/decr){
//code to be executed
}
Array
• An array is defined as the collection of similar type of data items stored at
contiguous memory locations.
• The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
• Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array,
we can't exceed the limit. So, it doesn't grow the size dynamically like
LinkedList which we will learn later.
Declaration and Example of C Array
data_type array_name[array_size];
int marks[5];
Initialization of C Array
The simplest way to initialize an array is by using the index of each element. We
can initialize each element of the array by using the index. Consider the
following example.
1.marks[0]=80;//initialization of array
2.marks[1]=60;
3.marks[2]=70;
4.marks[3]=85;
5.marks[4]=75;
Declaration and Example of C Array
data_type array_name[array_size];
int marks[5];
What is a structure (struct)?
• Structure (struct) is a user-defined data type in a programming language
that stores different data types' values together. The struct keyword is used
to define a structure data type in a program.
• The struct data type stores one or more than one data element of different
kinds in a variable.
• Suppose that you want to store the data of employee in your C/C++ project,
where you have to store the following different parameters:
What is a structure (struct)?
•Syntax of struct
struct [structure_name]
{
type member_1;
type member_2;
...
type member_n;
};
What is a structure (struct)?
•Example
struct employee
{
int id;
char name[50];
string department;
string email;
};
What is a Union?
•In "c," programming union is a user-defined data type that is used to store the
different data type's values.
•However, in the union, one member will occupy the memory at once. In other
words, we can say that the size of the union is equal to the size of its largest
data member size.
•Union offers an effective way to use the same memory location several times
by each data member. The union keyword is used to define and create a union
data type.
What is a Union?
•Syntax of Union
union [union name]
{
type member_1;
type member_2;
...
type member_n;
};
What is a Union?
•Example
union employee
{
string name;
string department;
int phone;
string email;
};
What is a Union?
•Example
union employee
{
string name;
string department;
int phone;
string email;
};
Basics of File Handling in C
•File handling in C enables us to create, update, read, and delete the files
stored on the local file system through our C program.
•The following operations can be performed on a file.
•Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
Create a Function
•To create (often referred to as declare) your own function, specify the name of
the function, followed by parentheses () and curly brackets {}:
•Syntax
•void myFunction() {
• // code to be executed
•}
Example Explained
• myFunction() is the name of the function
• void means that the function does not have a return value.
• Inside the function (the body), add code that defines what the function should
do
Call a Function
•Declared functions are not executed immediately. They are "saved for later
use", and will be executed when they are called.
•To call a function, write the function's name followed by two parentheses ()
and a semicolon ;
•In the following example, myFunction() is used to print a text (the action),
when it is called:
Call a Function
•Inside main, call myFunction():
// Create a function
void myFunction() {
printf("I just got executed!");
}
int main() {
myFunction(); // call the function
return 0;
}
Function Definition
•The function definition consists of actual statements which are executed when
the function is called (i.e. when the program control comes to the function).
Function Call
•A function call is a statement that instructs the compiler to execute the
function. We use the function name and parameters in the function call.
Functions call
C Pointers
• Pointers are one of the core components of the C programming language.
• A pointer can be used to store the memory address of other variables,
functions, or even other pointers.
• The use of pointers allows low-level memory access, dynamic memory
allocation, and many other functionality in C.
• A pointer is defined as a derived data type that can store the address
of other C variables or a memory location. We can access and
manipulate the data stored in that memory location using pointers.
C Pointers
Syntax of C Pointers
• The syntax of pointers is similar to the variable declaration in C, but we use
the ( * ) dereferencing operator in the pointer declaration.
datatype * ptr;
where
• ptr is the name of the pointer.
• datatype is the type of data it is pointing to.
C Pointers
How to Use Pointers?
The use of pointers in C can be divided into three steps:
• Pointer Declaration
• Pointer Initialization
• Pointer Dereferencing
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To
declare a pointer, we use the ( * ) dereference operator before its name.
Example
int *ptr;
The pointer declared here will point to some random memory address as it is
not initialized. Such pointers are called wild pointers.
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the
pointer variable. We generally use the ( & ) addressof operator to get the
memory address of a variable and then store it in the pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;
OR