KEMBAR78
Programing Part 2 | PDF | Pointer (Computer Programming) | Parameter (Computer Programming)
0% found this document useful (0 votes)
9 views10 pages

Programing Part 2

The document provides an overview of programming concepts in C, including algorithms, flowcharts, iteration, loops, and functions. It explains various programming constructs such as break and continue statements, switch statements, pointers, and recursion, along with their advantages and disadvantages. Additionally, it covers the differences between call by value and call by reference, as well as global and local variables.

Uploaded by

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

Programing Part 2

The document provides an overview of programming concepts in C, including algorithms, flowcharts, iteration, loops, and functions. It explains various programming constructs such as break and continue statements, switch statements, pointers, and recursion, along with their advantages and disadvantages. Additionally, it covers the differences between call by value and call by reference, as well as global and local variables.

Uploaded by

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

Programming in C

Part-II
1. What is Algorithm & Flowchart? Which symbols are used in flowchart Design?

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-
solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how
a work is to be executed in order to get the expected results.

A flowchart is a graphical representation of an algorithm. Programmers often use it as a program-


planning tool to solve a problem. It makes use of symbols that are connected among them to indicate the
flow of information and processing.

Flowchart Symbols

Here is a chart for some of the common symbols used in drawing flowcharts.

Symbol Symbol Name Purpose

Used at the beginning and end of the algorithm to show


Start/Stop
start and end of the program.

Process Indicates processes like mathematical operations.

Input/ Output Used for denoting program inputs and outputs.

Stands for decision statements in a program, where answer is


Decision
usually Yes or No.

Arrow Shows relationships between different shapes.

Connects two or more parts of a flowchart, which are


On-page Connector
on the same page.

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
Connects two parts of a flowchart which are spread over
Off-page Connector
different pages.

2. What is Iteration in C?

Iteration is a fundamental concept in programming that involves repeating a specific set of


instructions multiple times until a certain condition is met. In C programming language, there are
three types of iteration statements: for, while, and do-while.

3. Write down the syntax of for , while, do-while Loop?

for (initialization; condition; increment/decrement) {


// code to be executed
}
…………………………………………………..
while (condition) {
// code to be executed
}
……………………………………………………..
do {
// code to be executed
} while (condition);
4. What is infinite Loop?
An infinite loop is a looping construct that does not terminate the loop and executes the
loop forever. It is also called an indefinite loop or an endless loop. It either produces a
continuous output or no output.
for(; ;)
{
// body of the for loop.
}
5. Difference between while loop and do while loop?

while do-while

Condition is checked first then statement(s) is Statement(s) is executed atleast once, thereafter
executed. condition is checked.

It might occur statement(s) is executed zero times, At least once the statement(s) is executed.

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
while do-while

If condition is false.

No semicolon at the end of while. Semicolon at the end of while.


while(condition) while(condition);

If there is a single statement, brackets are not


Brackets are always required.
required.

Variable in condition is initialized before the variable may be initialized before or within the
execution of loop. loop.

while loop is entry controlled loop. do-while loop is exit controlled loop.

while(condition) do { statement(s); }
{ statement(s); } while(condition);

6. Difference between break and continue statement?

Break Statement Continue Statement

The Break statement is used to exit from the The continue statement is not used to exit
loop constructs. from the loop constructs.

The break statement is usually used with the The continue statement is not used with the
switch statement, and it can also use it within switch statement, but it can be used within
the while loop, do-while loop, or the for-loop. the while loop, do-while loop, or for-loop.

When a break statement is encountered then When the continue statement is encountered
the control is exited from the loop construct then the control automatically passed from
immediately. the beginning of the loop statement.

Syntax: Syntax:
break; continue;

Break statements uses switch and label


It does not use switch and label statements.
statements.

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
Break Statement Continue Statement

Leftover iterations are not executed after the Leftover iterations can be executed even if
break statement. the continue keyword appears in a loop.

7. What is Ternary Operator?

The conditional operator in C is kind of similar to the if-else statement as it follows the same
algorithm as of if-else statement but the conditional operator takes less space and helps to write
the if-else statements in the shortest way possible. It is also known as the ternary operator in
C

Syntax: condition ? <expression if true> : <expression if false>

8. What is Jump Statement? Give with example.

Jump statements are used to jump from one part of the code to another altering the normal flow
of the program. They are used to transfer the program control to somewhere else in the
program.

There are 4 types of jump statements in C:


1. break
2. continue
3. goto
4. return
9. What is switch Statement? Write down the advantage of switch statement?

The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute
multiple operations for the different possibles values of a single variable called switch variable.
Here, We can define various statements in the multiple cases for the different values of a single
variable.

Advantages of the switch statement:

There are several advantages of the switch statement in C. Some main advantages of the switch
statement are as follows:

o Readability and clarity: The switch statement provides a concise and straightforward
way to express multiway branching in the code. Dealing with multiple cases can make
the code more organized and easier to read than multiple nested if-else statements.

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
o Efficiency: The switch statement is generally more efficient than a series of if-else
statements when dealing with multiple conditions. It works as a direct jump table, which
makes it faster and more optimized in terms of execution time.
o Case-based logic: The switch statement naturally fits scenarios where the program needs
to make decisions based on specific values of a single variable. It is an intuitive and
straightforward way to implement case-based logic.

10. Why break statement is important for switch statement?

The break statement to end processing of a particular labeled statement within the switch
statement. It branches to the end of the switch statement. Without break, the program continues
to the next labeled statement, executing the statements until a break or the end of the statement is
reached.

11. What is return statement?


C return statement ends the execution of a function and returns the control to the
function from where it was called. The return statement may or may not return a value
depending upon the return type of the function. For example, int returns an integer value, void
returns nothing, etc.
12. What is Array? What are different types of array?

An array in C is a fixed-size collection of similar data items stored in contiguous memory


locations. It can be used to store the collection of primitive data types.
There are majorly three types of arrays:
1. One-dimensional array (1-D arrays)
2. Two-dimensional (2D) array
3. Three-dimensional array
13. How to declare an array?

data_type array_name [size];


or
data_type array_name [size1] [size2]...[sizeN];

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
14. What is String?
A String in C programming is a sequence of characters terminated with a null character
‘\0’. The C String is stored as an array of characters. The difference between a character array
and a C string is that the string in C is terminated with a unique character ‘\0’.

15. What is pointer?

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.

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.

16. Why Pointer is important in C?


Using pointer in C programming has following advantages:
1. Pointers provide direct access to memory.
2. Pointers provide a way to return multiple values from a user defined function without using return
keyword.
3. Pointers reduce the storage space and complexity of programs.
4. Pointers reduce the execution time of programs.

17.What is Null Pointer & void pointer?

A null pointer is a pointer which points nothing.


Some uses of the null pointer are:
a) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.
b) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.

void Pointer
A void pointer is a pointer that has no associated data type with it. A void pointer can hold an address
of any type and can be type casted to any type.
Syntax of void pointer
void *pointer name;
18. Write down the drawback of pointer?

Using pointer in C programming has following disadvantages:


1. If pointers are referenced with incorrect values, then it affects the whole program.

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
2. Memory leak occurs if dynamically allocated memory is not freed.
3. Segmentation fault can occur due to uninitialized pointer.

19. What is function in C? What are different types of function?

A function in C is a set of statements that when called perform some specific task. It is the
basic building block of a C program that provides modularity and code reusability. The
programming statements of a function are enclosed within { } braces, having certain
meanings and performing certain operations. They are also called subroutines or procedures
in other languages.

There are two types of functions in C programming:

1. Library Functions: are the functions which are declared in the C header files such as
scanf(), printf(), gets(), puts(), ceil(), floor() etc.
2. User-defined functions: are the functions which are created by the C programmer, so
that he/she can use it many times. It reduces the complexity of a big program and
optimizes the code.

20. Write down the advantage of function in C?

There are the following advantages of C functions.

o By using functions, we can avoid rewriting same logic/code again and again in a program.
o We can call C functions any number of times in a program and from any place in a program.
o We can track a large C program easily when it is divided into multiple functions.
o Reusability is the main achievement of C functions.
o However, Function calling is always a overhead in a C program.

21. What is Call by Value & Call by Reference Function?

Call by value
This method copies the actual value of an argument into the formal parameter of the function. In this case,
changes made to the parameter inside the function have no effect on the argument.

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
Call by reference
This method copies the address of an argument into the formal parameter. Inside the function, the
address is used to access the actual argument used in the call. This means that changes made to the
parameter affect the argument.
22. Difference between the Call by Value and Call by Reference?

Call By Value Call By Reference

While calling a function, instead of passing the


While calling a function, we pass the
values of variables, we pass the address of
values of variables to it. Such functions are
variables(location of variables) to the function
known as “Call By Values”.
known as “Call By References.

In this method, the value of each variable


In this method, the address of actual variables in
in the calling function is copied into
the calling function is copied into the dummy
corresponding dummy variables of the
variables of the called function.
called function.

With this method, the changes made to the


With this method, using addresses we would
dummy variables in the called function
have access to the actual variables and hence we
have no effect on the values of actual
would be able to manipulate them.
variables in the calling function.

In call-by-values, we cannot alter the


In call by reference, we can alter the values of
values of actual variables through function
variables through function calls.
calls.

Values of variables are passed by the Pointer variables are necessary to define to store
Simple technique. the address values of variables.

This method is preferred when we have to


This method is preferred when we have to pass a
pass some small values that should not
large amount of data to the function.
change.

Call by value is considered safer as Call by reference is risky as it allows direct


original data is preserved modification in original data

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
23. What is actual parameter and formal parameter in C?

Actual Parameters in C:
Actual parameters are the values that are passed to a function when it is called. They are also
known as arguments.
In C, actual parameters are enclosed in parentheses and separated by commas.
For example, consider the following function call:
int result = add(2, 3);
In this function call, 2 and 3 are the actual parameters, and they are passed to the function add,
which takes two formal parameters.
Formal Parameters in C:
Formal parameters are the variables declared in the function header that are used to receive the
values of the actual parameters passed during function calls. They are also known as function
parameters.
In C, formal parameters are declared in the function declaration or definition.
For example, consider the following function declaration:
int add(int a, int b);
In this function declaration, a and b are the formal parameters. They are used to receive the
values of the actual parameters passed during function calls.
24. Comparison between Global Variable and Local Variable?
Global Variable Local Variable

Global variables are declared outside all the function Local Variables are declared within a function
blocks. block.

The scope remains throughout the program. The scope is limited and remains within the
function only in which they are declared.

Any change in global variable affects the whole program, Any change in the local variable does not
wherever it is being used. affect other functions of the program.

A global variable exists in the program for the entire time A local variable is created when the function
the program is executed. is executed, and once the execution is
finished, the variable is destroyed.

It can be accessed throughout the program by all the It can only be accessed by the function
functions present in the program. statements in which it is declared and not by
the other functions.

If the global variable is not initialized, it takes zero by If the local variable is not initialized, it takes
default. the garbage value by default.

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558
25. What is Recursive Function? Write down the advantage and disadvantage of recursive function?

In C, a function that calls itself is called Recursive Function. The recursive functions contain a
call to themselves somewhere in the function body.

Advantages and Disadvantages of Recursion:

Recursion makes program elegant. However, if performance is vital, use loops instead as recursion
is usually much slower.

That being said, recursion is an important concept. It is frequently used in data structure and
algorithms. For example, it is common to use recursion in problems such as tree traversal.
26. What is Function Prototype?

The C function prototype is a statement that tells the compiler about the function’s name, its return
type, numbers and data types of its parameters. By using this information, the compiler cross-
checks function parameters and their data type with function definition and function call.

Syntax : return_type function_name (parameter_list) ;

where,

 return_type: It is the data type of the value that the function returns. It can be any data type int,
float, void, etc. If the function does not return anything, void is used as the return type.
 function_name: It is the identifier of the function. Use appropriate names for the functions that
specify the purpose of the function.
 parameter_list: It is the list of parameters that a function expects in parentheses. A parameter
consists of its data type and name. If we don’t want to pass any parameter, we can leave the
parentheses empty.

R.S Computer Class


Location: Shibnagar, Agartala
Mob:+91-9774148558

You might also like