Академический Документы
Профессиональный Документы
Культура Документы
Enumeration
if statement
if-else statement
switch statement
conditional operator statement
for
It is a loop statement.
Executes the statements within the
loop block repeatedly until the condition is
true.
Iterative variable is used.
Syntax:
for(iterative-var-initialization; condition;
iterative-var-incr/decr) {statements}
Do-while Statement
Syntax:
Iterative-var-initialization
while (condition)
{
Statements;
Iterative-var-incr/decr
}
Switch statement
Syntax:
if(condition 1)
{
if(condition 2)
{
statements;
}
else
{
statements;
}
}
Syntax:
switch(expression)
{
case 1:
block 1;
break;
case 2:
block 2;
break;
:
default:
default block;
}
Call by Reference
15. What is the use of function pointer? How do you declare it?
Function Pointers are pointers which point to an address of a function. A function in the
program code also has an address like variable.
A function can take many types of arguments including the address of another function.
Declaration:
int (*fp)(int,int);
Here we define a function pointer fn, that can be initialized to any function that takes two
integer arguments and return an integer. Here is an example of such a function
16. With the help of printf function show how C handles functions with variable
number of arguments.
In C, printf and scanf are two IO functions that take variable number of arguments. The
prototype of printf is
printf(char*, );
where first argument char* is a string that contains the format statement for the print
string (eg: %d %x %i ) and the 3 dots () indicate a variable number of arguments
equal to number of format symbols in the print string.
The C programming language provides an option to create user-defined function which
can accept variable number of parameters based on user requirement.
stdarg.h header file which provides functions and macros(va_start, va_list, va_arg,
va_end) to implement the functionality of variable arguments.
17. Define an array. Give an example.
Array is a data structure, which is a collection of homogeneous data elements accessed by a
single name. Array elements are differentiated by its position called index or subscript that
are always enclosed in square brackets [ ].
General Syntax:
<data_type> <array_name> [<size>];
Types:
One dimensional array
Multi dimensional array
Example: int arr[5];
18. How to declare a character, string and multiple strings in C?
Declaration of character:
char a; - hold single character
Declaration of string:
char str[25]; - hold a single string in 25 character length
Declaration of multiple strings:
Example:
int x=5, *y;
y=&x;
printf(Value of x=%x,*y);
PART-B
1. Explain in detail about conditional and control statements in C with suitable program.
if, if-else, nested if----syntax, explanation with program
(5)
for, while and do-while---- syntax, explanation with program
(6)
switch-case, break, continue--- syntax, explanation with program
(5)
(OR)
2. Explain about functions, function pointers and functions with varying number of
arguments in detail.
What is a Function Pointer?
(2)
Function Pointers are pointers which point to an address of a function. The
running programs get a certain space in the main memory.
The executable compiled program code and used variables are put inside this
memory. Thus a function in the program code also has an address.
Defining a Function Pointer
(2)
Functions like variables, can be associated with an address in the memory. We call
this a function pointer. A specific function pointer variable can be defined as
follows.
int (*fn)(int, int);
Here we define a function pointer fn, that can be initialized to any function that
takes two integer arguments and return an integer. Here is an example of such a
function
int sum(int x, int y)
{
return (x+y);
}
Now to initialize fn to the address of the sum, we can do the following.
fn = ∑ /* make fn points to the address of sum */
or simply
fn = sum; /* just ignore the & . Function names are just like array names */
So we use the sum function in two ways.
int x = sum(10,12); /* direct call to the function */
or
int x = (*fn)(12,10); /* call to the function through a pointer */
Example with explanation----------------------
(4)
(8)
Sometimes we may want to define a function that can take variable numbers of
arguments.
For example printf and scanf are two IO functions that take variable number of
arguments. The prototype of printf is printf(char*, );
where first argument char* is a string that contains the format statement for the print
string (eg: %d %x %i ) and the 3 dots () indicate a variable number of arguments
equal to number of format symbols in the print string.
The C programming language provides an option to create user-defined function
which can accept variable number of parameters based on user requirement. The
following example shows the definition of such a function.
#include<stdarg.h>
int func(int, ... )
{
.
.
.
}
int main()
{
func(2, 2, 3);
func(3, 2, 3, 4);
}
It should be noted that function func() has last argument as ellipses i.e. three dotes
(...) and the one just before the ellipses is always an int which will represent total
number variable arguments passed.
To use such functionality you need to make use of stdarg.h header file which
provides functions and macros to implement the functionality of variable arguments.
Follow the steps:
1. Define a function with last parameter as ellipses and the one just before the
ellipses is always an int which will represent number of arguments.
2. Create a va_list type variable in the function definition. This type is defined in
stdarg.h header file.
3. Use int parameter and va_start macro to initialize the va_list variable to an
argument list. The macro va_start is defined in stdarg.h header file.
4. Use va_arg macro and va_list variable to access each item in argument list.
5. Use a macro va_end to clean up the memory assigned to va_list variable.
Now let us follow the above steps and write down a simple function which can take
variable number of parameters and returns their average:
#include <stdio.h>
#include <stdarg.h>
double average(int num,...)
{
va_list valist;
double sum = 0.0;
int i;
/* initialize valist for num number of arguments */
va_start(valist, num);
/* access all the arguments assigned to valist */
for (i = 0; i < num; i++)
{
sum += va_arg(valist, int);
}
/* clean memory reserved for valist */
va_end(valist);
return sum/num;
}
int main()
{
printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));
printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
}
Program Output:
Average of 2, 3, 4, 5 = 3.500000
Average of 5, 10, 15 = 10.000000
When the above code is compiled and executed, it produces the following result. It
should be noted that the function average() has been called twice and each time first
argument represents the total number of variable arguments being passed. Only
ellipses will be used to pass variable number of arguments.