0% found this document useful (0 votes)
9 views11 pages

Assignment 1

Uploaded by

revee2095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Assignment 1

Uploaded by

revee2095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Amity University Haryana

ASET 2023-26

Branch Year: 2023-26


Session: 2023-24
CPUCL Assignment 1

Submitted by
Submitted to:
Taarini Singh Dr. Anil
K. Mishra
Enroll no. A50504823003
BCA – 1st Year, Section B

1/A50504823003
2/A50504823003
Assignment 1
Answer the questions given below with suitable examples.
Q1. Describe the various features (characteristic) of C language. Discuss
the general structure of C language Program with suitable examples.
Q2. Explain all the types of operators of “C” Programming language with
proper examples.
Q3. Explain various types of decision control statements supported by C
program with suitable examples.
Q4. Explain various types of loops supported by “C” program language
with example.
Q5. How functions are declared in C programming language. Explain the
‘call by value’ and ‘call by reference’ mechanism with suitable examples.
Q6. Difference between Static scope and Dynamic scope, Compile and
Execution time with suitable examples.
Q7. Declare the structure and union data type in C.

Answers
A1.

1. Procedural Language

3/A50504823003
In C step by step, predefined instructions are carried out. C program
may contain more than one function to perform a particular task. New
people to programming will think that this is the only way a particular
programming language works. There are other programming paradigms
as well in the programming world. Most of the commonly used paradigm
is an object-oriented programming language.
2. Fast and Efficient
Newer languages like Java, python offer more features than c
programming language but due to additional processing in these
languages, their performance rate gets down effectively. C programming
language as the middle-level language provides programmers access to
direct manipulation with the computer hardware but higher-level
languages do not allow this. It’s fast because statically typed languages
are faster than dynamically typed languages.
3. Modularity
The concept of storing C programming language code in the form of
libraries for further future uses is known as modularity. This
programming language can do very little on its own most of its power is
held by its libraries. C language has its own library to solve common
problems.
4. Statically Type
C programming language is a statically typed language.i.e. the type of
variable is checked at the time of compilation but not at run time. Hence,
each time a programmer types a program they have to mention the type
of variables used.
5. General-Purpose Language
From system programming to photo editing software, the C
programming language is used in various applications. Some of the
common applications where it’s used are such as in Operating systems:
Windows, Linux, iOS, Android, OXS and Databases like PostgreSQL,
Oracle, MySQL, MS SQL Server, etc.
Syntax for the general structure of a program is as follows:
#include <stdio.h>
int main()
{
int i = 1;
printf("%d %d %d\n", i++, i++, i);
return 0;

4/A50504823003
}
Example:
#include <stdio.h>
int main()
{

// prints hello world


printf("Hello World");

return 0;
}
A2. Operators are certain symbols in C used for performing certain
mathematical, relational, bitwise, conditional, or logical operations for
the user.
There are 7 types of operators in C are:
 Unary operator - Operators that operate or work with a single
operand are unary operators. For example: Increment (++) and
Decrement (–) Operators.
 Arithmetic operator - These operators are used to perform
arithmetic/mathematical operations on operands. Examples: (+, -, *,
/, %, ++,–).
 Relational operator – these are used for the comparison of two
values to understand the type of relationship a pair of number
shares. For example, less than, greater than, equal to, etc. Let’s see
them one by one
 Logical operator - They are used to combine two or more
conditions/constraints or to complement the evaluation of the
original condition under consideration. For example, AND, OR, NOT
 Bitwise operator – these 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. For example,
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).
 Assignment operator - these 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. For example, “=”, “+=”, “-=”, etc.
5/A50504823003
 Conditional operator – these are used to make decisions based on
the evaluation of a condition. The most commonly used conditional
operator is the ternary conditional operator (? : ), which is also
known as the conditional operator or the ternary operator.
A3. The conditional statements (also known as decision control
structures) such as if, if else, switch, etc. are used for decision-making
purposes in C/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.
Following are the decision-making statements available in C or C++:
1. if Statement - The if statement is the simplest decision-making
statement. It is used to decide whether a certain statement or block
of statements will be executed or not i.e. if a certain condition is
true then a block of statements is executed otherwise not.
Syntax –
if(condition)
{
// Statements to execute if
// condition is true
}

2. if-else Statement - The if-else statement consists of two blocks,


one for false expression and one for true expression. if statement
alone tells us that if a condition is true, it will execute a block of
statements and if the condition is false, it won’t execute.
Syntax –
6/A50504823003
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

3. Nested if Statement – It is an if statement that is the target of


another if statement. Nested if statements mean an if statement
inside another if statement.
Syntax –
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}

4. if-else-if Ladder – They are used when the user has to decide
among multiple options. The C if statements are executed from the
top down. As soon as one of the conditions controlling the if is true,
the statement associated with that if is executed, and the rest of the
C else-if ladder is bypassed. If none of the conditions is true, then
the final else statement will be executed. if-else-if ladder is similar
to the switch statement.
Syntax –
if (condition)
statement;
else if (condition)
statement;
.
.

else
statement;

7/A50504823003
5. Switch Statement - Switch case statement evaluates a given
expression and based on the evaluated value (matching a certain
condition), it executes the statements associated with it. Basically,
it is used to perform different actions based on different
conditions(cases).
Syntax –
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
.
.
.
case value_n: statement_n;
break;

default: default_statement;
}

6. Conditional Operator - It is used to add conditional code in our


program. It is similar to the if-else statement. It is also known as
the ternary operator as it works on three operands.
Syntax - (condition) ? [true_statements] : [flase_statements];

A4. Loop – they 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.
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 – It is a repetition control structure that allows
programmers to write a loop that will be executed a specific

8/A50504823003
number of times. for loop enables programmers to perform n
number of steps together in a single line.
Syntax –
for (initialize expression; test expression; update expression)
{
//
// body of for loop
//
}
 While Loop - the execution is terminated on the basis of the test
condition. If the test condition will become false then it will break
from the while loop else body will be executed.
Syntax –
while (test_expression)
{
// body of the while loop

update_expression;
}
 Do – While Loop - the loop body will execute at least
once irrespective of the test condition. test condition which is
tested at the end of the body.
Syntax –
do
{
// body of do-while loop

update_expression;

} while (test_expression);

A5. A function declaration usually contains the function name, return


type, and the parameter types. Syntax - return_type
function_name(parameter_list);

9/A50504823003
 Call By Value: It is a method of parameter passing, the values of
actual parameters are copied to the function’s formal parameters.
There are two copies of parameters stored in different memory
locations. One is the original copy and the other is the function
copy. Any changes made inside functions are not reflected in the
actual parameters of the caller.
 Call By Reference: It is a method of parameter passing, the address
of the actual parameters is passed to the function as the formal
parameters. Both the actual and formal parameters refer to the
same locations. Any changes made inside the function are actually
reflected in the actual parameters of the caller.
Syntax –
#include <stdio.h>

main(int *a, int *b) {


// Get temporary variable to hold the value of a
int temp = *a;
// Assign the value of b to a
*a = *b;
// Assign the temporary value (originally in a) to b
*b = temp;
}

int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y); // Pass the addresses of x and y
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
A6. Static scoping: is also called lexical scoping. In this scoping, a
variable always refers to its top-level environment. This is a property of
the program text and is unrelated to the run-time call stack. It also
10/A50504823003
makes it much easier to make a modular code as a programmer can
figure out the scope easily through code.
Dynamic Scoping: It is a global identifier refers to the identifier
associated with the most recent environment and is uncommon in
modern languages. The compiler first searches the current block and
then successively all the calling functions.
A7.
 Structure type: It is a user-defined data type available in C that
allows to combining of data items of different kinds. Structures are
used to represent a record. To define a structure, you must use
the struct statement. The struct statement defines a new data type,
with more than or equal to one member.
Syntax - struct [structure name]
{
member definition;
member definition;
...
member definition;
};

 Union Type: It is a special data type available in C that allows


storing different data types in the same memory location. You can
define a union with many members, but only one member can
contain a value at any given time. Unions provide an efficient way
of using the same memory location for multiple purposes. To define
a union, you must use the union statement in the same way as you
did while defining a structure. The union statement defines a new
data type with more than one member for your program.
Syntax - union [union name]
{
member definition;
member definition;
...
member definition;
};

11/A50504823003

You might also like