Вы находитесь на странице: 1из 9

Subject code & Name: CS6202 Programming and Data Structures-I

Part-A Questions and Answers


Unit-I
1. What do meant by tokens in C?
Tokens are the basic building blocks in C programming language which are
constructed together to write a C program. Each and every smallest individual unit in
a C program is referred as tokens or lexical units.
C tokens are of six types. They are,
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators
2. Define variable with example.
A variable is a name given to a storage area that C program can manipulate. All
variable must be declared before they used.
Each variable has a specific type, which determines the size of memory, range of
values and set of operations that can be applied to.
Declaration Syntax:
<data_type> <variable_name>;
For example:
int a;
char var;
3. Mention the rules for specifying identifier. Give an example.
Name of identifier includes alphabets, digit and underscore.
First character of any identifier must be either alphabets or underscore.
Any keyword of C cant be used as identifier name.
White spaces cannot be used within a variable name.
Identifiers are case sensitive and no limit for length of an identifier.
Examples:
Valid Identifiers: _name, calculate, abc123
Invalid Identifiers: 12abc, float, a -bc
4. Define Data types.
In C programming language, data type refers to an extensive system for declaring
variables of different types. The language itself provides basic arithmetic types and
syntax to build array and compound types.
Types of data types:
1. Fundamental Data Types:
Integer types
Floating Type
Character types
2. Derived Data Types:
Arrays
Pointers
Structures

Enumeration

5. What are the qualifiers to data types?


The keywords which are used to modify the properties of a datatype to variable are called
qualifiers. It provides an option to basic data types for different size of memory allocation to
the variable. There are four types of qualifiers available in C language. They are,

Short applied to int data type only


Long applied to int and double data type
Signed applied to int and char data type
Unsigned applied to int and char data type

6. What is decision making statement? What are they in C?


Decision making statement is used to decide the order of execution of statements of C
program based on certain conditions. It is also referred as selective statements. C language
handles decision-making by supporting the following statements,

if statement
if-else statement
switch statement
conditional operator statement

7. Define looping. What are they in C?


Loop is a type comes under conditional statements in C. Execution of a group of statements
are repeated until certain condition is satisfied. Loop statements in C are:
1. for
2. while
3. do-while
8. State the difference between if and for statement.
if
It is a selective or decision making
statement.
If the condition is true, it executes
Statements under that condition only once.
Iterative variable is not used.
Syntax: if(condition) {statements}

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}

9. Compare while and do-while statements in C.


While Statement

Do-while Statement

The statements are executed


under the loop block only if the
condition is true.

While loop is entry control loop

Syntax:
Iterative-var-initialization
while (condition)
{
Statements;
Iterative-var-incr/decr
}

First iteration, without checking


the condition, the statement is
executed under the loop block
only once. In further iteration,
statements are executed under the
loop block only if the condition is
true.
Do-while is exit control loop. It
is used for a block
of code that must be executed at
least once.
Syntax:
Iterative-var-initialization
do
{
Statements;
Iterative-var-incr/decr
}while(condition);

10. Define conditional or ternary operator in C with example.


?: is called conditional or ternary operator. Ternary operator takes on 3 arguments.
Syntax:
(Condition) ? <True-statement> : <False-statement>
If the condition is true then True-statement is executed only once otherwise Falsestatement is executed only once.
Example:
int num=5;
(num%2= =0)?printf("Even"):printf("Odd");
11. Compare switch( ) case and Nested-if statements in C.
Nested-if else statement

Switch statement

It is decision making or selective


statements in C

It is also decision making or selective


statements in C

If can evaluate relation or logical


expressions.

Switch can test only the constant


values.

Statements under if block are


executed only once if the condition is
true otherwise else block executed.

In Switch statement, based on the


constant value arrived from the
condition, the statements under the
case for that constant is executed only
once. If no case for that constant then
default will be executed.

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;
}

12. Define function and its types.


A function is a group of statements that can be perform a task. Every C program has at
least one function, which is main(), and possibly user-define additional functions.
Function reduces the amount of coding and it can be called from another program or
function.
Types:
There are 4 types of functions in C.
1. Functions with no arguments and no return value.
2. Functions with arguments but no return value.
3. Function with no arguments but with return value.
4. Functions with arguments and with return value.
13. Differentiate call-by-value and call-by-reference methods.
Call by value

If we call a function by passing


variables or constants as
arguments then such type of
function call is known as Call by
value.
Formal arguments (in called
function) values are photocopies
of actual arguments (in caller
function) values.
In this method, the changes made
to the formal arguments values do
not affect the actual arguments
values.

Call by Reference

If we call a function by passing


address identifiers as arguments
then such type of function call is
known as Call by reference.

Formal arguments values are


pointers to actual arguments
values.

Since the address is passed, the


changes made to the formal
arguments have same/resemble on
the values of actual arguments.

14. Define recursive function with example.


A recursive function is a function that calls itself during its execution. This enables the
function to repeat itself several times until end of function execution is reached.
Example:

Count (integer N){


if (N <= 0) return "Must be a Positive Integer";
if (N > 9) return "Counting Completed";
else return Count (N+1);
}
The function Count() uses recursively for displaying the numbers from specified integer
passed to function to 10. For example, Count(1) would return 2,3,4,5,6,7,8,9,10. Count(7)
would return 8,9,10.

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:

char name[5][25]; - hold five strings each in 25 character length.


19. Define preprocessor in C and give examples.
The C pre-processor or cpp are used to link library files (header files) in to source
program, that are always placed before the main() function. All pre-processor commands
begin with a pound symbol (#). It has three pre-processor directives,
Macro inclusion
Conditional inclusion
File inclusion
Example:
#define- used for macro inclusion
#include-used for file inclusion
20. Define macro substitution with example.
Syntax:
#define name replacement text
In Macro substitution, subsequent occurrences of the token name will be replaced by
the replacement text. The name in a #define has the same form as a variable name; the
replacement text is arbitrary. Substitutions are made only for tokens, and do not take place
within quoted strings.
Example:
#define max(A, B) ((A) > (B) ? (A) : (B))
21. Define pointer and its uses.
Pointers are variables that store the memory address of other variables. They are declared the
same way like any other variable but they are prefixed by a " * " which is called a dereference
operator. By using the "*" we stating that the actual variable is a pointer.
The pointer's data type must match the data type of the variable it points to. Once declared
the pointer variable can be assigned using the " & " operator , which means "the address of ".
Uses:
Pointers are more efficient in handling the data in arrays
Pointers reduce the length and complexity of the program
They increase the execution speed
The pointers save data storage space in memory
22. Define NULL pointer.
NULL Pointer is a pointer which is pointing to nothing (value is 0 or NULL).
In case, if you dont have address to be assigned to pointer then you can simply use
NULL.
NULL is macro constant defined in the header files stdio.h, alloc.h, stdlib.h
Example:
int *a;
a=NULL;
23. What is the purpose of address and indirection operators in C?
& (Address operator) used to return the memory address of variable, pointer, array,
structure, union, file etc.,
Example:
int x;
printf(Address of x=%x,&x);
* (Indirection operator) used for declaring pointer variable and also used to retrieve
the data stored in the address hold by pointer variable.

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 = &sum; /* 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)

Functions with variable arguments

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

Вам также может понравиться