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

CA - Unit-2 Fundamentals of Programming

The document discusses various fundamental programming concepts in C including decision making using conditional statements, loops, arrays, structures, unions, file handling, functions, and function calls. It provides details on the syntax and usage of these concepts.

Uploaded by

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

CA - Unit-2 Fundamentals of Programming

The document discusses various fundamental programming concepts in C including decision making using conditional statements, loops, arrays, structures, unions, file handling, functions, and function calls. It provides details on the syntax and usage of these concepts.

Uploaded by

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

Comprehensive Assessment

Prof. Vijya Tulsani, Assistant Professor


IT & Computer Science
CHAPTER-2

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.

• They are also known as Decision-Making Statements and are used to


evaluate one or more conditions and make the decision whether to execute
a set of statements or not.

• These decision-making statements in programming languages decide the


direction of the flow of program execution.
Need of Conditional Statements
• There come situations in real life when we need to make some decisions and
based on these decisions, we decide what should we do next.

• Similar situations arise in programming also where we need to make some


decisions and based on these decisions we will execute the next block of
code. For example, in C if x occurs then execute y else execute z.

• 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.

• C programming has three types of loops:

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).

• The syntax of do-while loop in c language is given below:

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.

• The syntax of for loop in c language is given below:

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.

• C array is beneficial if you have to store similar elements. For example, if we


want to store the marks of a student in 6 subjects, then we don't need to
define different variables for the marks in the different subject. Instead of
that, we can define an array which can store the marks in each subject at the
contiguous memory locations.
Properties of Array
• The array contains the following properties.
1. Each element of an array is of same data type and carries the same size,
i.e., int = 4 bytes.
2. Elements of the array are stored at contiguous memory locations where
the first element is stored at the smallest memory location.
3. Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the
size of the data element.
Advantage & Disadvantageof C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an
array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of
code only.
4) Random Access: We can access any element randomly using the array.

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

Now, let us see the example to declare the array.

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

Now, let us see the example to declare the array.

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.

•Creation of the new file


•Opening an existing file
•Reading from the file
•Writing to the file
•Deleting the file
Functions for file handling
No. Function Description
1 fopen() opens new or existing file

2 fprintf() write data into the file


3 fscanf() reads data from the file
4 fputc() writes a character into the file

5 fgetc() reads a character from file

6 fclose() closes the file


7 fseek() sets the file pointer to given
position
8 fputw() writes an integer to file
9 fgetw() reads an integer from file

10 ftell() returns current position

11 rewind() sets the file pointer to the


beginning of the file
Functions
•A function is a block of code which only runs when it is called.

•You can pass data, known as parameters, into a function.

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

•// Outputs "I just got executed!"


Call a Function
Function Declarations
•In a function declaration, we must provide the function name, its return type,
and the number and type of its parameters. A function declaration tells the
compiler that there is a function with the given name defined somewhere else
in the program.

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

int *ptr = &var;


3. Pointer Dereferencing
• Dereferencing a pointer is the process of accessing the value stored in the
memory address specified in the pointer.
• We use the same ( * ) dereferencing operator that we used in the pointer
declaration.
Example
// C program to illustrate Pointers // Driver program
#include <stdio.h> int main()
void geeks() {
{ geeks();
int var = 10; return 0;
// declare pointer variable }
int* ptr;
// note that data type of ptr and var
must be same Output
ptr = &var; Value at ptr = 0x7fff1038675c
// assign the address of a variable Value at var = 10
to a pointer Value at *ptr = 10
printf("Value at ptr = %p \n", ptr);
printf("Value at var = %d \n", var);
printf("Value at *ptr = %d \n", *ptr);
}
www.paruluniversity.ac.in

You might also like