CTRL STMT, Functions
CTRL STMT, Functions
switch Statement in C
// Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
#include <stdio.h>
int main()
{
int arr[] = { 1, 5, 15, 20 };
Output
1 in range 1 to 6
5 in range 1 to 6
15 not in range
20 in range 19 to 20
C – Loops
TYPES
How for Loop Works?
STEP 1: The loop’s execution starts after the loop condition is evaluated to be true.
STEP 2: The condition of the continue statement will be evaluated.
STEP 3A: If the condition is false, the normal execution will continue.
STEP 3B: If the condition is true, the program control will jump to the start of the loop and all the
statements below the continue will be skipped.
STEP 4: Steps 1 to 4 will be repeated till the end of the loop.
The continue statement in C is used in loops to skip the current iteration and move on to the
next iteration without executing the statements below the continue in the loop body.
Functions
Functions are the block of code that is executed every time they are called during an execution of a
program.
Function Declaration
Types of Functions
C Function Prototype
A function prototype is also known as a function declaration which specifies the function’s name,
function parameters, and return type. The function prototype does not contain the body of the function.
It is basically used to inform the compiler about the existence of the user-defined function which can
be used in the later part of the program.
Function Declaration vs Function Prototype
What it Informs the compiler about the Informs the compiler about the existence,
does existence of a function. parameters, and return type of a function.
ReturnType FunctionName(ParameterType1,
Syntax ReturnType FunctionName();
ParameterType2, ...);
Tells the compiler about the Tells the compiler about the function's existence
Purpose
function's existence. and signature to enable type checking.
1. Pass by Value
Parameter passing in this method copies values from actual parameters into formal function
parameters. As a result, any changes made inside the functions do not reflect in the caller’s
parameters.
#include <stdio.h>
int main()
{
int var1 = 3, var2 = 2;
swap(var1, var2);
return 0;
}
2. Pass by Pointers
The caller’s actual parameters and the function’s actual parameters refer to the same locations,
so any changes made inside the function are reflected in the caller’s actual parameters.
// C program to show use of
// call by Reference
#include <stdio.h>
int main(){
int var1 = 3, var2 = 2;
swap(&var1, &var2);
return 0;
}
Header Files
In C language, header files contain a set of predefined standard library functions. The .h is the
extension of the header files in C and we request to use a header file in our program by including it
with the C preprocessing directive “#include”.
C Header files offer the features like library functions, data types, macros, etc by importing them into
the program with the help of a preprocessor directive “#include”.
Ans:
Below are the methods to return multiple values from a function in C:
By using pointers.
By using structures.
By using Arrays.
You can return multiple values from a function in C by using pointers or structures. With pointers, you
pass memory addresses as arguments to the function and store values at those addresses within the
function. With structures, you define a structure that contains all the values you want to return, and
then return an instance of that structure from the function.
main Function in C
The main function in C is the entry point of a program where the execution of a program starts. It is a
user-defined function that is mandatory for the execution of a program because when a C program is
executed, the operating system starts executing the statements in the main() function.
void main()
{
// Function body
}
Syntax
int main()
{
// Function body
}
or
int main(void)
{
// Function Body
}
It is used to return the program control to the calling It is used to terminate the current
function. process.
When the return is used in main(), all the objects are When the exit() is used, only the
destroyed whether they are local or static. destructor of static objects is called.
class Test {
public:
Test() { printf("Inside Test's Constructor\n"); }
~Test()
{
printf("Inside Test's Destructor");
getchar();
}
};
// Driver code
int main()
{
Test t1;
As we can see in the above example, the destructor of the local object is not called before the
program’s termination.
One thing to note here is that the static objects will be cleaned up even when we call exit(). For
example, see the following program:
// C++ Program to illutrate the distruction of static
// objects while using exit()
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
class Test {
public:
Test() { printf("Inside Test's Constructor\n"); }
~Test()
{
printf("Inside Test's Destructor");
getchar();
}
};
// driver code
int main()
{
static Test t1; // Note that t1 is static
exit(0);
}
Output
Inside Test's Constructor
Inside Test's Destructor
EG:
#include <stdio.h>
fun(int x)
{
return x*x;
}
int main(void)
{
printf("%d", fun(10));
return 0;
}
OUTPUT:
100
The important thing to note is, there is no return type for fun(), the program still compiles and runs fine
in most of the C compilers. In C, if we do not specify a return type, compiler assumes an implicit return
type as int. However, C99 standard doesn’t allow return type to be omitted even if return type is int.
This was allowed in older C standard C89.