Q1. What is a string constant?
Ans:
A string constant is a group of
characters surrounded by double quotes. These characters may be letters,
numbers or any special characters.
Example: "Hello! World", "2015", "welcome"
Q2. Briefly explain the stages in array
initialization.
Ans: An array can be declared and initialized in various ways.
Array Declaration by Initializing Elements
An array can be initialized at the time of its
declaration. In this method of array declaration, the compiler will
allocate an array of size equal to the number of the array elements. The
following syntax can be used to declare and initialize an array at the same
time.
int array[] = {10, 20, 30 40,
50};
In the above syntax, an array
of 5 elements is created and even though the array size has not been specified
here, the compiler will allocate a size of 5 integer elements.
Array Declaration by Specifying
the Size and Initializing Elements
An array can also be created
by specifying the size and assigning array elements at the time of declaration.
This method of array creation is different from the previous one. Here, if the
number of initialized elements is less than the size of the array specified,
then the rest of the elements will automatically be initialized to 0 by the
compiler. The number of initialized elements
cannot be larger than the size of the array.
int array1[5] = {100, 200,
300, 400, 500};
int array2[5] = {100, 200,
300};
In the above array syntax,
my_array1 is an array of size 5 with all five elements initialized. Whereas,
my_array2 is an array of size 5 with only three of its elements initialized.
The remaining two elements of the second array will be initialized to 0 by the
compiler.
array2 = {100, 200, 300, 0, 0};
Array Initialization Using a Loop
An array can also be
initialized using a loop. The loop iterates from 0 to (size - 1) for accessing
all indices of the array starting from 0. The following syntax uses a “for
loop” to initialize the array elements. This is the most common way to initialize
an array in C.
// declare an array.
int array[5];
// initialize array using a
"for" loop.
int i;
for(i = 0; i < 5;
i++)
{
array[i]
= 2 * i;
}
// array = {0, 2, 4, 6,
8}
In the above syntax, an array
of size 5 is declared first. The array is then initialized using a for loop
that iterates over the array starting from index 0 to 4.
Q3. Explain the concept of recursive function with example.
Ans: In
C, it is possible for the function to call itself. The function which calls itself is known as recursive function.
A
recursive function is a function defined in terms of itself via self-calling
expressions. This means that the function will continue to call itself and
repeat its behavior until some condition is satisfied to return a value.
Recursive
functions allow programmers to write efficient programs using a minimal amount of
code.
The
recursive functions should be used very carefully because, when a function
called by itself it enters into the infinite loop. And when a function enters
into the infinite loop, the function execution never gets completed. We should
define the condition to exit from the function call so that the recursive
function gets terminated.
Recursive
functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number, generating Fibonacci series, etc.
Recursive
function example:
Program to
find factorial of a given number recursively to illustrate recursive function
#include<stdio.h>
#include<conio.h>
long int fact(int n);
int main( )
{
int num,f;
printf(“\n Enter a number: ”);
scanf(“%d”,&num);
f=fact(num);
printf(“\nFactorial of %d is :%ld”,num,f);
getch();
return 0;
}
long int fact(int n)
{
if (n<=1)
return
(1);
else
return
(n*fact(n-1)) ;
}
Output:
Enter a number: 4
Factorial of 4 is: 24
The function fact calls itself repeatedly with n-1 and terminate
when the variable becomes 1.
Q4.
What is string? Write any two string function with their action.
Ans:
A
string is any sequence of characters. In C strings and arrays of characters are
exactly the same thing. It is always terminated by the NULL character. Common
string operations include finding lengths, copying, searching, replacing and
counting the occurrences of specific characters and words.
Following are two useful string handling
functions supported by C.
·
strlen()
·
strcpy()
These functions are defined in string.h header file. Hence we need to include this header file whenever we use these string handling functions in the program.
strlen()
This is the string
length function. It
is used to find out the length of a string i.e. the number of characters in the
string excluding the terminating NULL character. It accepts a single argument
which is pointer to the first character of the string.
Syntax:
strlen( string )
For example:
printf ( "%d", strlen(
"Sumit" )) ;
would print out 5. (Note that the
terminator is not counted as a character in the string.)
strcpy():
This is the string copy
function. It is used to copy one string into another string.
The function strcpy(str1,str2) copies
str2 to str1 including the NULL character. Here str2 is the source string and
str1 is the destination string. The old content of the destination string str1
are lost. The function returns a pointer to destination string str1.
Syntax:
strcpy(destination_String,source_String);
char ch1[20] = "Hello World" ;
char ch2[20];
strcpy( ch2, ch1) ;
would result in ch2 holding the string "Hello
World".
Q5. Draw the basic
structure of a C programming.
Ans: Every C program is basically a group of different section that is used for different purpose. The structure gives us a basic idea of the order of the sections in a program. A well-defined structured layout makes program more readable, easy to modify, consistent format and gives better clarity and the concept of a program. Whenever we write a program in ‘C’ language, we can divide that program into six different sections. These sections are as follows:
- Documentation section
- Linkage section
- User definition section
- Global declaration section
- Main() function section
- Subprogram section
Pictorial Representation of Basic Structure
of C Programming:
Q6. Write the use of continue statement.
Ans: continue statement
is used to go to the top of the loop from anywhere within the loop during its
execution. In certain circumstances, it requires to skip a part of the body of the loop and restart from the beginning
part of the loop. In such cases, continue statement
is used.
Continue statement is used for continuing next iteration of loop after
skipping some statement of loop. When it encountered control automatically
passes through the beginning of the loop.
The continue statement can be used only
inside a looping statement. It is usually associated with if statement. It is
useful when we want to continue the program without executing any part of the
program.
The difference between break and
continue is, when the break encountered loop is terminated and it transfer to
the next statement and when continue is encounter control come back to the
beginning position.
Syntax: continue;
for(initialization; test condition;
modifier expression)
{
…………………..
if(condition)
continue;
……….
}
Example:
for(i=1;i<=6;i++)
{
if(i==4)
continue;
printf(“\t%d”,i);
}
Output:1 2 3 5
6
In the above example, only i=1,2,3,5,6
will be printed. When i is equal to 4, control returns to the beginning of the
loop.
Q7.
What is a flowchart?
Draw the flowchart for maximum of two numbers.
Ans: Flowchart in C is a diagrammatic
representation of a sequence of logical steps of a program. It uses various symbols to show the operations and
decisions to be followed in a program
A flowchart is a graphical depiction of decisions and the
results of those decisions.
Programmers often use it as a
program-planning tool to solve a problem. It makes use of symbols that are
connected among them to indicate the flow of information and processing. The
process of drawing a flowchart for an algorithm is known as “flowcharting”.
Flowchart for maximum of two numbers:
Q8. Why do we use #define in our program?
Ans: #define directive is used to define
constants or an expression in our program.
The #define
preprocessor directive lets a programmer or a developer define the macros
within the source code.
In the C Programming
Language, the #define directive allows the definition of macros within our
source code. These macro definitions allow constant values to be declared for
use throughout our code.
One of the common ways
to define constants in C is to use the # define preprocessor directive.
The syntax for creating a constant using
#define in the C language is:
Syntax:
#define CNAME value
OR
#define CNAME expression
Where CNAME = name of the constant
Examples:
Defining
a value:
#define MAX 10
Defining
an expression:
#define SUM(a,b) a+b
Q9.
What is the use of ‘\n’?
Ans: ‘\n’ is a Escape Sequence which produces a new line. It performs newline i.e. it moves the cursor to next
line.
For example:
Consider two statements:
printf(“Hello!”);
printf(“ Welcome to India.”);
The output of these statements
is:
Hello! Welcome To India.
If we want the
second printf() to produce its message in the next line, then we have to use the
new line character ‘\n’.
printf(“Hello!”);
printf(“\n Welcome to India.”);
The Output will be:
Hello!
Welcome to India.
If you don't include the ‘\n’, the next print statement will continue on the same line.
Q10. What is call
by call by value and call by reference?
Ans: Call by
value and call by reference is type of function calls. In C language, these two methods are used to pass the data into the function
Functions can be
invoked in two ways: Call by Value or Call by Reference.
Call by value:
When a function is called by value, a
copy of the actual parameter is created and sent to the formal parameter of the
called function. The called function uses only the copy of the actual
parameter. Therefore, the value of the actual parameter cannot be altered by
the called function. Any changes made inside called function are not reflected
in actual parameters of the calling function.
Call
by reference:
When a function is called by reference, the address of the actual parameters is sent
to the formal parameter of called functions. If we make any change in the
formal parameters, it shows the effect in the value of the actual parameter.
When parameters are passed to a
function, the parameters are passed by value. That is a copy of the actual
parameter is sent to the called function. Therefore, called function cannot
change the value of the actual parameter of the calling function. But, there
might be some cases where we need to change the values of actual parameter in
the calling function.
In order to enable the called function
to change the variable of the calling function, the address of the actual parameter must be passed. When a parameter is
passed by reference, we are not passing copy of its value, but we are passing
the parameter itself to the called function. Any changes made inside the called
functions are actually reflected in actual parameters of the calling function.
The called function can alter the value of the actual parameters.
Q11.
Why floating point numbers are not used for equality in expressions?
Ans: In the case of floating-point numbers, the relational operator(==) does not produce correct
output, this is due to the internal precision errors in rounding up
floating-point numbers.
The equality operators (= =
and !=) are much more troublesome. The = = operator, returns true only if its
operands are exactly equal. Because even the smallest rounding error will cause
two floating point numbers to not be equal, operator= = is at high risk for
returning false when a true might be expected. Operator!= has the same kind of
problem.
For this reason, use of
these operators with floating point operands should be avoided.
Q12. How does type float differ
from double in C programming?
Ans: Float and
double are both widely used data types in programming that have the ability to
store decimal or floating-point numbers. Though Float and Double both of them
are used for assigning real (or decimal) values, there are some differences
between these two data types.
FLOAT |
DOUBLE |
It has single precision data-type. |
It has double precision data type or
we can say two times more precision than float. |
Float data types can store up to 7
digits. |
Double data type can store up to
15 digits |
The format specifier for float is
%f. |
The format specifier for double is
%lf. |
It is a 32-bit floating-point data
type. |
It is a 64-bit floating-point data
type. |
It takes 4-bytes of memory space. |
It takes 8-bytes of memory space. |
It has 6-digits of precision. |
It has 15-digits of precision. |
A value having a range within 1.2E-38 to 3.4E+38 can be
assigned to float variables. |
A value having range within 2.3E-308 to 1.7E+308 can be assigned to double type variables |
Q13. What is null string?
Ans: A null ring has no values
at all. It is represented by null. It is an empty char array, one that has not been
assigned any elements. The string exists in memory, so its not a null pointer. It’s
just absent any elements.
Q14.
What is null pointer?
Ans: A pointer variable that is initialized
with the NULL value at the time of pointer declaration is called Null pointer. A
Null pointer is a pointer which points nowhere, it doesn't have an address of any function or a
variable.
The Null pointer doesn’t point to any memory
location. We
can specify any of the following values for a null pointer constant:
- 0
- NULL
Syntax:
datatype
*var_name= NULL;
Q15.
Explain in brief the nested if statement?
Ans: In C programming, if statement may itself contain another if statement. When if statement comes within another if statement then this is known nested if statement.
Nested if statement is
placing if statement inside another if statement. Nested if in C is helpful if we
want to check the condition inside a condition.
If outer if statement fails, then the compiler skips the
entire block irrespective of their inner if statement.
The following example illustrates the nested
if statement.
if(x>=3)
{
if(x<=5)
printf(“
The number lies between 3 and 5”);
}
else
printf(“The number does not lie between 3
and 5”);
Q16:
What is “sizeof” operator?
Ans: The sizeof operator is a much used operator in C. It is a compile time unary operator which can be used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables.
The sizeof
operator gives the amount of storage, in bytes,
required to store an object of the type of the operand. When
sizeof() is used with the data types, it simply returns the amount of memory
allocated to that data type.
sizeof
is greatly used in dynamic memory allocation.
sizeof(type_name)
Example:
sizeof(int) returns
2, since an integer occupies 2 bytes of memory
Q17. What is a register variable in C?
Ans: Register
variables tell the compiler to store the variable in CPU register instead of
memory. Registers are faster than
memory to access, so the variables which are most frequently used in a C
program can be put in registers. “register” keyword is used to declare the register variables. The keyword register hints
to compiler that a given variable can be put in a register.
register data_type var_name;
register int i;
Q18. What do you mean by unary operators?
Define unary operators.
Ans: C has a type of operators that act upon a single operand to produce a new value. These types of operators are known as unary operators. Unary operators generally precede their single operands, though some unary operators are written after their operands.
A unary operator is an operator used to operate on a single operand to return a new value. All the unary operators have equal precedence, and their associativity is from right to left. When we combine the unary operator with an operand , we get the unary expression.
Q19. What are the basic data types available in C?
Ans: The basic data types available in C are:
- Integer data type (int)
- Floating point type (float)
- Character data type (Char)
Q20. What is the use of goto statement?
Ans: The goto statement is used to alter the normal flow of control in a program. This statement transfers control to any part of the program unconditionally. In certain cases, we may require to transfer control to the other part of the program. C provides goto statement to jump unconditionally from one part to another part of the program. This helps in controlling the flow of execution based on certain specific condition.
Q21. Write the difference between while loop and do-while loop.
Ans: Differences between while loop and do-while loop:
Basis for comparison |
While loop |
Do-while loop |
Controlling
condition |
The
controlling condition appears at the beginning of the loop. |
The
controlling condition appears at the end of the loop. |
Checking condition |
The condition is checked first and then loop body is
executed |
The loop body is executed first
and then the condition is checked. |
Loop Type |
It is entry-controlled loop |
It is exit-controlled loop |
Semicolon |
No semicolon at the end of while. while
(condition) |
Semicolon at the end of the while.
while (condition); |
Loop
execution |
The
loop body would be executed only if the given condition is true. Statement(s) is executed zero times if condition is false. |
The
loop body would be executed at least once even the given condition is false. |
Initialization of variable |
Variable in condition is
initialized before the execution of loop body. |
Variable may be
initialized before or within the loop body. |
Requirement of Brackets |
If
there is a single statement, brackets are not required. |
Brackets
are always required. |
Syntax |
while(condition) { statements;//Body of the loop} |
do { statements;//Body of the loop } while(condition);
|
Iteration |
The iterations do not occur if, the condition
at the first iteration appears false. |
The iteration occurs at least once even if
the condition is false at the first iteration. |
0 Comments
if you have any doubts plz let me know...