C Important Questions
C Important Questions
Actual and formal arguments will be Actual and formal arguments will be
Memory Location
created in different memory location created in same memory location
int t;
t = *a; *a = *b; *b = t;
OUTPUT
======
}
n1: 10, n2: 20
OUTPUT
======
statement 1;
statement 2;
..
..
Test Condition is any logical condition that controls the number of times the loop statements are
executed.
Flowchart:
Working:
It is noted that when ‘for’ loop execution starts, first variable initialization is done, then condition
is checked before execution of statements; if and only if condition is TRUE, statements are
executed; after all statements are executed, iteration of counter of loop is done either increment or
decrement.
Example:
#include <stdio.h>
int main ()
int i ;
for (i=1; i < =5; i++)
{
printf("%d\t",i);
}
Output:
1 2 3 4 5
We can pass multiple addresses of variables as input parameters to a function. Inside function
body, we can store the return values at the passed memory locations.
#include <stdio.h>
swapByReference(&n1, &n2);
int t;
t = *a; *a = *b; *b = t;
OUTPUT
======
Structure contains different types of data under single name so if we return structure object
then we can return multiple values using it.
#include<stdio.h>
struct data
};
struct data myStruct;
myStruct s;
s.sum = a + b;
s.product = a * b;
int main()
int a, b;
myStruct result;
return 0;
In above example, while returning object s , the values under it, sum and product will be
returned.
3. What is file pointer (Significance of file pointer)? Discuss about file opening modes.
File Pointer:
A file pointer is a pointer to a structure, which contains information about the file, including its
name, current position of the file, whether the file is being read or written, and whether errors or
end of the file have occurred.
Recursion Iteration
Iteration is achieved by
Recursion refers to a an iterative function
recursive function in which loops to repeat
Definition
which it calls itself again some section of the code.
to repeat the code.
Infinite recursion is
Infinite looping consumes
Infinite repetition capable of crashing the
CPU cycles repeatedly
system
C Tokens
C tokens are the basic buildings blocks in C language which are constructed together to write a C
program.
Each and every smallest individual units in a C program are known as C tokens.
A selection statement selects among a set of statements depending on the value of a controlling
expression. The selection statements are the if statement and the switch statement.
A repetitive statements are used to execute same statements repeatedly. (for, while, do…while).
01 Test condition Test condition appears at the Test condition appears at the end.
beginning.
03 Execution Each execution occurs by testing Each execution except the first one
condition. occurs by testing condition.
sum = 0; do
n = 1; {
{ scanf("%d", &num);
n = n+ 1; while(num>0);
} ======
========
Difference between break and continue statement
BASIS FOR
BREAK CONTINUE
COMPARISON
Task It terminates the execution of It terminates only the current iteration of the
remaining iteration of the loop. loop.
Control after 'break' resumes the control of the 'continue' resumes the control of the program to
break/continue program to the end of loop enclosing the next iteration of that loop enclosing 'continue'.
that 'break'.
Causes It causes early termination of loop. It causes early execution of the next iteration.
Continuation 'break' stops the continuation of 'continue' do not stops the continuation of loop, it
loop. only stops the current iteration.
Other uses 'break' can be used with 'switch', 'continue' can not be executed with 'switch' and
'label'. 'labels'.
A self referential data structure is essentially a structure definition which includes at least one member
that is a pointer to the structure of its own kind. A chain of such structures can thus be expressed as
follows.
struct name {
member 1;
member 2;
...
};
The above illustrated structure prototype describes one node that comprises of two logical segments. One
of them stores data/information and the other one is a pointer indicating where the next component can be
found. .Several such inter-connected nodes create a chain of structures.
The following figure depicts the composition of such a node. The figure is a simplified illustration of
nodes that collectively form a chain of structures or linked list.
Such self-referential structures are very useful in applications that involve linked data structures, such as
lists and trees.
While passing arrays as arguments to the function, only the name of the array is passed (,i.e, starting
address of memory area is passed as argument).
“C program to pass an array containing age of person to a function. This function should find
average age and display the average age in main function.”
#include <stdio.h>
int main()
return 0;
int i;
sum += age[i];
return avg;
scope i.e where the value of the variable would be available inside a program.
default initial value i.e if we do not explicitly initialize that variable, what will be its default
initial value.
lifetime of that variable i.e for how long will that variable exist.
1. Automatic variables
2. External variables
3. Static variables
4. Register variables
Scope: Variable defined with auto storage class are local to the function block inside which they
are defined.
Lifetime: Till the end of the function/method block where the variable is defined.
A variable declared inside a function without any storage class specification, is by default
an automatic variable. They are created when a function is called and are
destroyed automaticallywhen the function's execution is completed. Automatic variables can
also be called local variablesbecause they are local to a function. By default they are
assigned garbage value by the compiler.
Scope: Global i.e everywhere in the program. These variables are not bound by any function, t
hey are available everywhere.
Lifetime: Till the program doesn't finish its execution, you can access global variables.
A variable that is declared outside any function is a Global Variable. Global variables remain
available throughout the program execution. By default, initial value of the Global variable is
0(zero). One important thing to remember about global variable is that their values can be
changed by any function in the program.
#include<stdio.h>
void main()
number = 10;
Static variables
Scope: Local to the block in which the variable is defined
A static variable tells the compiler to persist/save the variable until the end of program. Instead of
creating and destroying a variable every time when it comes into and goes out of
scope, staticvariable is initialized only once and remains into existence till the end of the
program. A staticvariable can either be internal or external depending upon the place of
declaration. Scope of internal static variable remains inside the function in which it is
defined. External static variables remain restricted to scope of file in which they are declared.
Static variable
Scope: Local to the function in which it is declared.
Lifetime: Till the end of function/method block, in which the variable is defined.
Register variables inform the compiler to store the variable in CPU register instead of memory.
Register variables have faster accessibility than a normal variable. Generally, the frequently used
variables are kept in registers. But only a few variables can be placed inside registers. One
application of register storage class can be in using loops, where the variable gets used a number
of times in the program, in a very short span of time.
Syntax :
Even though we have declared the storage class of our variable number as register, we cannot
surely say that the value of the variable would be stored in a register. This is because the number
of registers in a CPU are limited. Also, CPU registers are meant to do a lot of important work.
Thus, sometimes they may not be free. In such scenario, the variable works as if its storage class
is auto.