You are on page 1of 13

Syllabus

Repetitive control structures:

Pre-test and post-test loops, initialization and updation, event and counter
controlled loops, while, do..while, for, break and continue statements, comma
expression

Functions:

User-defined functions, Function definition, arguments, return value, prototype,


arguments and parameters, inner-function communication​.

Standard functions:​ ​Math functions, Random numbers.

Scope:​ ​local global.

Parameter passing:​ ​Call by value and call by reference.

Recursive functions:​ ​Definition, examples, advantages and disadvantages.

Macros:​ ​Definition, examples, comparison with functions.


Q1. What are the Repetitive control structures (Loops)? Explain with
example?
➢ The real power of computers is in their ability to repeat an operation or a series of
operations many times.

➢ This repetition, called looping, is one of the basic structured programming concepts.

➢ Each loop must have an expression that determines if the loop is done.

➢ If it is not done, the loop repeats one more time; if it is done, the loop terminates.

Types of Repetitive control structures:

➢ C has Two loop statements:

➢ pretest loop

➢ posttest loop

➢ The ​while and for ​are pretest loops, and ​do…while​ is a post-test loop.

While Do-While For

While Loop is first check the Do-While Loop is first For Loop is first check the
condition. The condition is execute the statements After condition. The condition is
true it execute the Statements. check the condition. true it execute the Statements.

While Loop is pretest loop Do-While Loop is post-test For Loop is pretest loop
loop

While Loop Syntax: Do-While Loop Syntax: For Loop Syntax:


while(condition) do for(initialization; condition;
{ { increment/decrement)
Statements; Statements; {
} }while(condition); Statements;
}
​ hile loop we are not use
W Do-While loop we are using For loop we are not use
semicolon. semicolon. semicolon.

While Loop Example: Do-While Loop Example​: For Loop Example:

#include<stdio.h> #include<stdio.h> #include<stdio.h>


main() main() main()
{ { {
int i=1,n=10; int i=1,n=10; int i,n=10;
while(i<=n) do
{ { for(i=1;i<=n;i++)
Printf(“%d”,i); Printf(“%d”,i); {
i++; i++; Printf(“%d”,i);
} } while(i<=n); }

Q2. Write a Sum of individual digits of a program in c?


#include<stdio.h>
main()
{
int sum,n,rem;
sum=0;
printf("Enter the number : ");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("The sum of digits of given number = %d",sum);
}
Q7.To display the multiplication table using do while.
#include<stdio.h>
main()
{
int i=1,n,c;
printf("Enter the number of table:");
scanf("%d",&n);
do
{
c=n*i;
printf("\n\t%d\t*\t%d = %d",n,i,c);
i++;
}while(i<=10);
}
Q8.To display the multiplication table using for.
#include<stdio.h>
main()
{
int i=1,n,c;
printf("Enter the number of table:");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
c=n*i;
printf("\n\t%d\t*\t%d = %d",n,i,c);
}
}

Q9.what is a Function? Advantages Of a function?


Functions in c:

➢ A ​function is a self-contained block of code that carries out some specific and
well-defined task.

➢ In general, the ​purpose of a function is to ​receive zero or more pieces of data, operate
on them, and ​return​ at most one piece of data.

➢ C functions are classified into two categories

1. ​Library​ Functions

2. ​User​ ​Defined​ Functions

Advantages of Functions:

Modular Programming:​ It facilitates top down modular programming.

Reduction of Source Code:​ The length of the source program can be reduced by using
functions at appropriate places.

Easier Debugging:​ It is easy to locate and isolate a faulty function for further
investigation.
Code Reusability:​ A program can be used to avoid rewriting the same sequence of
code at two or more locations in a program.

Function Sharing:​ Programming teams does a large percentage of programming.


If the program is divided into subprograms, each subprogram can be written by one or two
team members of the team rather than having the whole team to work on the complex
program.

Q10. Difference of Function prototype &Function definition?

Function Declaration (or) Prototype:


➢ The ANSI C standard expands the concept of forward function declaration.
➢ This expanded declaration is called a function prototype.
A function prototype performs two special tasks:
➢ First it identifies the ​return​ ​type​ of the function so that the compile can generate the
correct code for the return data.
➢ Second, it specifies the ​type​ and number of arguments used by the function.

The general form of the function prototype or declaration is

Function Definition:

The function definition contains the code for a function.

It is made up of two parts:


➢ The ​function header​ and the ​function body​, the compound statement is must.

Function header consists of three parts:

➢ The ​return type​,

➢ The ​function name​, and

➢ The ​formal parameter​ ​list​.

1. Function body contains local declarations and function statement.

2. Variables can be declared inside function body.

3. Function cannot be defined inside another function.

The general form of function Definition

Return-type:

➢ Specifies the type of value that a function returns using the return statement.
➢ It can be any valid data type.
➢ If no data type is specified the function is assumed to return an integer result.

Function-name:

➢ Must follow same rules of variable names in C.

➢ No two functions have the same name in a C program.

​Argument declaration:
➢ It is a comma-separated list of variables that receive the values of the argument when
function is called.

➢ If there is no argument declaration the bracket consists of keyword void.

The Function Call:

➢ A function call is a postfix expression.

➢ The operand in a function is the function name.

➢ The operator is the parameter lists (…), which contain the actual parameters.

Example:

void main ()

​sum (a, b);

➢ When the compiler encounters the function call, the control is transferred to the
function ​sum ()​.

➢ The function is executed line by line and produces the output for the sum of two
numbers and then control is transferred back to the main function

Q11. What are the passing parameters? Explain? (Or)

What is call by value and call by reference?


Whenever we call a function then sequence of executable statements gets executed. We can
pass some of the information to the function for processing called argument.

Two Ways of Passing Argument to Function in C Language:

• Call by Value

• Call by Reference

Call by Value:
➢ When a function is called with actual parameters, the values of actual parameters are
copied into the formal parameters.

➢ If the values of the formal parameters changes in the function, the values of the actual
parameters are not changed.

➢ This way of passing parameters is called call by value (pass by value).

/* Example: call by value*/

#include<stdio.h>
void swap (int , int ); /*function prototype */
void main ()
{
int a, b;
printf (“\n Enter the values of a and b: “);
scanf (“%d%d”, &a, &b);
swap (a, b); /*function calling*/
}
void swap (int x, int y) /* function definition */
{
int temp;
temp=x;
x=y;
y=temp;
printf (“\n The Values of a and b after swapping a=%d, b =%d”, x, y);
}

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

/*Example: call by reference*/

#include<stdio.h>
void swaping(int *, int *); /*function prototype */
int main()
{
int a,b;
printf("Enter a & b values : ");
scanf("%d%d",&a,&b);
printf("\nBefore swapping values:");
printf("\n\tx=%d \n\ty=%d",a,b);
​swaping(&a,&b); /* function calling*/
}
void swaping(int *x, int *y) /* function definition */
{
int z;
z=*x;
*x=*y;
*y=z;
printf("\nAfter swapping values:");
printf("\n\tx=%d \n\ty=%d",x,y);
}

Difference between Call by Value and Call by Reference

Call by Value Call by Reference

When Function is called the When a function is called address of


values of variables are passed. variables is passed.

Formal parameters contain the Formal parameters contain the


value of actual parameters. address of actual parameters.

Change of formal parameters in


The actual parameters are changed
the function will not affect the
since the formal parameters indirectly
actual parameters in the calling
manipulate the actual parameters
function

Execution is slower since all the


Execution is faster since only
values have to be copied into
addresses are copied.
formal parameters.

Q12. What is Recursion in c? What are the Advantages & Disadvantages of


recursion?

Recursion in C:

➢ In C, functions can call themselves.

➢ A function calling itself is called recursive function.

➢ Recursion is a repetitive process, where the function calls itself.


Concept of recursive function:

➢ A recursive function is invoked or called to solve a problem.

➢ The statement that solves a problem is known as the ​base case.

➢ Every recursive function must have a ​base case.

➢ The rest of the function is known as the ​general case.

➢ The recursion step is done until the problem converges to become the ​simplest case.

➢ This simplest case will be solved by the function which will then return the answer to the
previous copy of the function.

➢ The sequence of returns will then go all the way up until the original call of the function
finally return the result.

Example: Recursive Factorial Function

​Iteration Definition:

fact (n) = 1 if n=0

= n*(n-1)*(n-2)…….3*2*1 if n>0

Recursion Definition:

fact (n) = 1 if n=0 (Base Case)

= n*fact (n-1) if n>0 (General Case)

Function Recursion Advantages:

1. Recursion using we can avoid unnecessary calling of functions.


2. Recursion is usually simplicity.
3. Recursion is used to divide the problem into same problem of subtypes and hence
replaces complex nesting code.
4. Through Recursion one can solve problems in easy way while its iterative solution is very
big and complex.

Function Recursion Disadvantages:

1. Too many recursive functions there may be confusion in the code.


2. Recursive solution is always logical and it is very difficult to trace.(debug and
understand)
3. Recursive calling increase the space complexity.
4. The algorithm may require large amounts of memory if the depth of the recursion is very
large. High memory consumption is due to large function call number

Q13.Write a Factorial Recursion Program in c?


#include<stdio.h>
long factorial (long); /* function prototype */
void main (void)
{
long int i;
i=4;
printf ("%2ld! = %1ld\n", i, factorial (i)); /* function call */
}
long factorial (long number) /* function definition */
{
if (number = =0)
return 1;
else
return (number * factorial (number-1));
}

Q14. Difference between Iteration (loop) and Recursion?

ITERATION(LOOP) RECURSION
Iteration explicitly uses repetition Recursion achieves repetition by calling the
structure. same function repeatedly.
Iteration is terminated when the loop Recursion is terminated when base case is
condition fails satisfied.
May have infinite loop if the loop Recursion is infinite if there is no base case or
condition never fails if base case never reaches.
Iterative functions execute much faster Recursive functions are slow and takes a lot
and occupy less memory space. of memory space compared to iterative
functions

Q. What is a Macro? E​xplain any four directives​?


Macro Definition:

➢ A macro definition ​command associates a name ​with a ​sequence of tokens.


➢ The name is called the macro name and the tokens are referred to as the macro body.
The following syntax is used to define a macro:

#define macro_name(<arg_list>) macro_body

Here #define is a define directive, macro_name is a valid C identifier.


macro_body is used to specify how the name is replaced in the program before it is
compiled

Example:​ ​#define square (a) (a*a)

➢ Macro definition without arguments is referred as a constant.

#define PI 3.14159

S.no Preprocessor Syntax Description


1 File inclusion #include <file_name> The source code of the file “file_name” is included in
directive Ex: the main program at the specified place
#include<stdio.h>
2 Define directive #define This macro defines constant value and can be any of
Ex: the basic data types.
#define pi 3.14
3 Undefined #undef, #pragma #undef is used to undefine a defined macro variable.
directive Ex #Pragma is used to call a function before and after
#undef number main function in a C program

4 Conditional #ifdef, #endif, #if, Set of commands are included or excluded in source
compilation #else, #ifndef program before compilation with respect to the
condition

Q15.Difference between Macro and Function


Macro Function

Macro is ​Preprocessed Function is ​Compiled


No Type Checking Type Checking​ is Done
Code​ Length ​Increases Code​ Length remains ​Same
Speed of Execution is ​Faster Speed of Execution is ​Slower
Before Compilation macro name is During function call , Transfer of Control
replaced by macro value takes place
Useful where small code appears many Useful where large code appears many time
time
Generally Macros do not extend beyond Function can be of any number of lines
one line
Macro does not Check ​Compile Errors Function Checks ​Compile Errors

You might also like