c Programming Assignment
c Programming Assignment
F.Y. – Sem-I
Sub: CS-02: Problem solving methodology &
Programming in C
Types Description
Primitive data types are the most basic data types that are
Primitive Data
Types used for representing simple values such as integers, float,
characters, etc.
User Defined
Data Types The user-defined data types are defined by the user himself.
Derived Types
The data types that are derived from the primitive or built-
in data types are referred to as Derived Data Types.
printf("%d",i);
}
3. FLOAT
Floating point numbers are used to store decimal numbers. The range of
values it can store also varies from compiler to compiler.
For 32-bit and 64-bit compilers, it is the same as 4 bytes. That is 2^(4*8)
length of value, which is 4,29,49,67,296 i.e. 0 to 4,29,49,67,296 numbers can
be represented using float.
#include <stdio.h>
void main() {
float f;
f = 1.20 ;
printf("%f",f);
}
4. VOID
It is a special type known as empty data type that is used to state that a given
variable does not have any type. This is mainly used in defining functions
where we do not want to return any value.
Secondary data types:
Secondary data types are formed by combining two or more primary data
types in C.
They are mainly of two types:
1. USER-DEFINED DATA TYPES
2. DERIVED DATA TYPE
These data types are defined by the user as per their convenience. If a user
feels a need of having a data type which is not predefined in C library, then
they make their own.
STRUCTURE
Derived data types are data types which are formed by combining one or more
primitive data types or basic data types in C. For example, there might be
some cases where primitive data types are not sufficient for us. Like if we
want to store a phone number we can use an integer but what if we want to
store phone numbers of all the students in a college. Making variables for each
of them is not the optimal way.
To deal with such situations optimally, C has some derived data types, which
we can use as per our convenience.
1. ARRAY
2. POINTER
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1. Arithmetic Operations in C
These operators are used to perform arithmetic/mathematical
operations on operands. Examples: (+, -, *, /, %,++,–). Arithmetic
operators are of two types:
a) Unary Operators:
Operators that operate or work with a single operand are unary
operators. For example: Increment(++) and Decrement(–) Operators
int val = 5;
cout<<++val; // 6
b) Binary Operators:
Operators that operate or work with two operands are binary
operators. For example: Addition(+), Subtraction(-), multiplication(*),
Division(/) operators
int a = 7;
int b = 2;
cout<<a+b; // 9
2. Relational Operators in C
These are used for the comparison of the values of two operands. For
example, checking if one operand is equal to the other operand or not,
whether an operand is greater than the other operand or not, etc.
Some of the relational operators are (==, >= , <= )
int a = 3;
int b = 5;
cout<<(a < b);
// operator to check if a is smaller than b
3. Logical Operator in C
Logical Operators are used to combining two or more
conditions/constraints or to complement the evaluation of the
original condition in consideration. The result of the operation of a
logical operator is a Boolean value either true or false.
For example, the logical AND represented as the ‘&&’ operator in
C returns true when both the conditions under consideration are
satisfied. Otherwise, it returns false. Therefore, a && b returns true
when both a and b are true (i.e. non-zero)
4. Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then the
calculation is performed on the operands. Mathematical operations
such as addition, subtraction, multiplication, etc. can be performed at
the bit level for faster processing. For example, the bitwise
AND operator represented as ‘&’ in C takes two numbers as operands
and does AND on every bit of two numbers. The result of AND is 1
only if both bits are 1(True).
int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
cout << (a ^ b); // 00001100
cout <<(~a); // 11111010
5. Assignment Operators in C
Assignment operators are used to assign value to a variable. The left
side operand of the assignment operator is a variable and the right
side operand of the assignment operator is a value. The value on the
right side must be of the same data type as the variable on the left side
otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
a) “=”
This is the simplest assignment operator. This operator is used to
assign the value on the right to the variable on the left.
Example:
a = 10;
b = 20;
ch = 'y';
b) “+=”
This operator is the combination of the ‘+’ and ‘=’ operators. This
operator first adds the current value of the variable on left to the value
on the right and then assigns the result to the variable on the left.
Example:
(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11.
c) “-=”
This operator is a combination of ‘-‘ and ‘=’ operators. This operator
first subtracts the value on the right from the current value of the
variable on left and then assigns the result to the variable on the left.
Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) = 2.
d) “*=”
This operator is a combination of the ‘*’ and ‘=’ operators. This
operator first multiplies the current value of the variable on left to the
value on the right and then assigns the result to the variable on the
left.
Example:
(a *= b) can be written as (a = a * b)
If initially, the value stored in a is 5. Then (a *= 6) = 30.
e) “/=”
This operator is a combination of the ‘/’ and ‘=’ operators. This
operator first divides the current value of the variable on left by the
value on the right and then assigns the result to the variable on the
left.
Example:
(a /= b) can be written as (a = a / b)
If initially, the value stored in a is 6. Then (a /= 2) = 3.
6. Other Operators
Apart from the above operators, there are some other operators
available in C used to perform some specific tasks. Some of them are
discussed here:
i. sizeof operator
sizeof is much used in the C programming language.
denoted by size_t.
Basically, the sizeof the operator is used to compute the size of the
variable.
ii. Comma Operator
The comma operator (represented by the token) is a binary operator
that evaluates its first operand and discards the result, it then
evaluates the second operand and returns this value (and type).
The comma operator has the lowest precedence of any C operator.
Expression3
Here, Expression1 is the condition to be evaluated. If the
operators.
iv. dot (.) and arrow (->) Operators
Member operators are used to referencing individual members of
into another.
The most general cast supported by most of the C compilers is as
1. if Statement
2. if-else Statement
3. Nested if Statement
4. if-else-if Ladder
5. switch Statement
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
#include <stdio.h>
int main()
{
int i = 10;
if (i < 15) {
printf("10 is less than 15 \n");
}
printf("I am Not in if");
}
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
Syntax of switch:
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Example:
#include <stdio.h>
int main()
{
int var = 2;
switch (var) {
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
printf("Default Case is executed");
break;
}
return 0;
}
Q – 4: Explain Looping control structure of C.
Ans: Loops in programming are used to repeat a block of code until the
specified condition is met. A loop statement allows programmers to
execute a statement or group of statements multiple times without
repetition of code.
There are mainly two types of loops in C Programming:
1. Entry Controlled loops: In Entry controlled loops the test condition
is checked before entering the main body of the loop. For Loop and
While Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops the test condition is
evaluated at the end of the loop body. The loop body will execute at
least once, irrespective of whether the condition is true or false. do-
while Loop is Exit Controlled loop.
for Loop
The for loop in C Language provides a functionality/feature to repeat a set
of statements a defined number of times. The for loop is in itself a form of
an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the
initialization, condition, and updating statements as part of its syntax. It is
mainly used to traverse arrays, vectors, and other data structures.
Syntax of for Loop
for(initialization; check/test expression; updation)
{
// body consisting of
multiple statements
}
#include <stdio.h>
int main()
{
int i = 0;
for (i=0; i < 5 ; i++) {
printf("Hello Creative\n");
}
return 0;
}
} while (condition);
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf("Hello Creative\n");
}while(i<5);
return 0;
}
In C, we have to declare the array like any other variable before using it. We can
declare an array by specifying its name, the type of its elements, and the size of its
dimensions. When we declare an array in C, the compiler allocates the memory
block of the specified size to the array name.
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
C Array Initialization
Initialization in C is the process to assign some initial value to the variable. When
the array is declared or allocated memory, the elements of the array contain some
garbage value. So, we need to initialize the array to some meaningful value. There
are multiple ways in which we can initialize an array in C.
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They are as
follows:
1. One Dimensional Arrays (1D Array)
2. Multidimensional Arrays
1. One Dimensional Array in C
The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have only
one dimension.
Syntax of 1D Array in C
array_name [size];
Example:
#include <stdio.h>
int main()
{
int arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = i * i - 2 * i + 1;
}
printf("Elements of Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2. Multidimensional Array in C
Multi-dimensional Arrays in C are those arrays that have more than one
dimension. Some of the popular multidimensional arrays are 2D arrays and 3D
arrays. We can declare arrays with more dimensions than 3d arrays but they
are avoided as they get very complex and occupy a large amount of space.
A. Two-Dimensional Array in C
A Two-Dimensional array or 2D array in C is an array that has exactly two
dimensions. They can be visualized in the form of rows and columns
organized in a two-dimensional plane.
Syntax of 2D Array in C
array_name[size1] [size2];
Here,
size1: Size of the first dimension.
size2: Size of the second dimension.
#include <stdio.h>
int main()
{
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
int main()
{
int arr[2][2][2] = { 10, 20, 30, 40, 50, 60 };
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n \n");
}
return 0;
}
Q – 6: Explain UDF with all its types.
A function is self-contained block of code that performs a particular
task functions can be classified into two categories namely library
function and user defined function. The main difference between two
these categories is that library function are need not to be written by us
when user defined functions has to be developed by users.
Types of Functions
Advantage of functions in C :
The main advantages of using UDF is that it can be called & used
whenever and wherever
required in the program.
It can be used t to implement or facilitates top-down modular
programming approach, by
which the logic of program can be divided into functions.
By using functions, we can avoid rewriting same logic/code again
and again in a program.
We can call C functions any number of times in a program and from
any place in a program.
We can track a large C program easily when it is divided into
multiple functions.
Reusability is the main achievement of C functions.
The length of source program can be reduce which save both
time and space. Function are made for reusability. In
case of large program with thousands of code lines,
debugging and editing become easier if you use function.
Example :
void starline( ); int sum(int,int);
float interest(float,float,float);
When function doesn’t have any argument and doesn’t have any return value
at that time it is called as Function with no Argument and no Return Value.
Means the called function will not receive any argument form the calling
function and it will not return any value to calling function.
We can use any UDF at any number of times.
Ex :
void get()
{
Printf(“Hello this is testing function”);
}
Void main()
{
get();
}
When function have some argument to send called function and have
not any return value at that time it is called as Function with
Argument and No Return Value.
Means the called function will receive some argument form the
calling function but it will no return value to calling function.
Ex :
void get(int a)
{
Printf(“A=%d”,a);
}
void main()
{
get(10);
}