0% found this document useful (0 votes)
7 views17 pages

CTRL STMT, Functions

Uploaded by

yevob11108
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)
7 views17 pages

CTRL STMT, Functions

Uploaded by

yevob11108
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/ 17

Conditional Statements in C

switch Statement in C

// Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}

Rules of the switch case statement


In a switch statement, the “case value” must be of “char” and “int” type.
There can be one or N number of cases.
The values in the case must be unique.
Each statement of the case can have a break statement. It is optional.
The default Statement is also optional.
The default block can be placed anywhere.

How switch Statement Work?


Step 1: The switch variable is evaluated.
Step 2: The evaluated value is matched against all the present cases.
Step 3A: If the matching case value is found, the associated code is executed.
Step 3B: If the matching code is not found, then the default case is executed if present.
Step 4A: If the break keyword is present in the case, then program control breaks out of the
switch statement.
Step 4B: If the break keyword is not present, then all the cases after the matching case are
executed.
Step 5: Statements after the switch statement are executed.
C program to illustrate using range in switch case

#include <stdio.h>
int main()
{
int arr[] = { 1, 5, 15, 20 };

for (int i = 0; i < 4; i++) {


switch (arr[i]) {
// range 1 to 6
case 1 ... 6:
printf("%d in range 1 to 6\n", arr[i]);
break;
// range 19 to 20
case 19 ... 20:
printf("%d in range 19 to 20\n", arr[i]);
break;
default:
printf("%d not in range\n", arr[i]);
break;
}
}
return 0;
}

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?

How continue statement works?


The working of the continue statement is as follows:

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

Feature Function Declaration 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, ...);

Example int add(int a, int b); int add(int a, int b);

Used when the function


Used when the function definition appears after its
Usage definition appears later in the
call or in another file.
code.

Tells the compiler about the Tells the compiler about the function's existence
Purpose
function's existence. and signature to enable type checking.

Passing Parameters to Functions

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>

void swap(int var1, int var2){


int temp = var1;
var1 = var2;
var2 = temp;
}

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>

void swap(int *var1, int *var2){


int temp = *var1;
*var1 = *var2;
*var2 = temp;
}

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

Syntax of Header Files in C


#include <filename.h> // for files in system/default directory
or
#include "filename.h" // for files in same directory as source file
Types of C Header Files
1. Standard / Pre-existing header files: <float.h>, <math.h>, <stdio.h>.
2. Non-standard / User-defined header files: <conio.h>

Q: How to return multiple values from a function in C or C++?

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.

We can write the main function in many ways in C language as follows:


int main(){} or int main(void){}

main(){} or void main(){} or main(void){} or void main(void){}

Important Points about C main Function

It is the function where the program’s execution starts.


Every program has exactly one main function.
The name of this function should be “main” not anything else.
The main function always returns an integer value or void.
The main function is called by OS, not the user.
Types of C main Functions
Main function with no arguments and void return type

void main()
{
// Function body
}

Main function with no arguments and int return type

Syntax

int main()
{
// Function body
}
or

int main(void)
{
// Function Body
}

Main function with the Command Line Arguments


int main(int argc, char* argv[])
{
// Function body
}

Return Statement vs Exit() in main() in C++

return Statement exit() Function

The return statement is a keyword. exit() is a function.

It is part of the Standard Library


It is part of the language.
<stdlib.h>

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.

exit() Function in main()


When exit() is used to exit from the program, destructors for locally scoped non-static objects are not
called. The program is terminated without destroying the local objects.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class Test {
public:
Test() { printf("Inside Test's Constructor\n"); }

~Test()
{
printf("Inside Test's Destructor");
getchar();
}
};

// Driver code
int main()
{
Test t1;

// using exit(0) to exit from main


exit(0);
}
Output
Inside Test's Constructor

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>

using namespace std;

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

Can main() be overloaded in C++?


Answer: To overload main() function in C++, it is necessary to use class and declare the main as
member function. Note that main is not reserved word in programming languages like C, C++, Java
and C#. For example, we can declare a variable whose name is main, try below example:
#include <iostream>
int main()
{
int main = 10;
std::cout << main;
return 0;
}

Implicit return type int in C

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.

You might also like