Programming in C Quan Book
Programming in C Quan Book
Programming in C
Descriptive Questions
Q-1 Write a note on History of C
OR
C is not a specific Language but is a general purpose language. Appreciate
the sentence.
OR
The next version of C will be P and not the. D. Evaluate the statement.
By 1960, number of computer languages had come into existence, almost each for
a specific purpose. At this stage, people started thinking for a general purpose
language. Therefore an international committee was formed to develop such a
language. To reduce the abstractness and generality, a new language called CPL
(Combined Programming Language) was developed at Cambridge. However, it
turned to be so big, having so many features that it was hard to earn and difficult
to implement.
Basic Combined Programming Language (BCPL) was developed by Martin
Richards to solve the problem by bringing CPL down to its good features.
Meanwhile, Ken Thompson has developed language B as a further simplification
of CPL.
Dennis Ritchie inherited the features of B and BCPl in C, added some of his own
and developed the language C. Dennis Ritchie's main achievement is the
restoration of lost generality and still keeping it powerful.
Various stages in evolution of C is as follows:
Year Language Developed by Remarks
1960 ALGOL Int. Committee Too general, too abstract
1963 CPL Cambridge Uni. Hard to learn, difficult to implement
1967 BCPL Martin Richards Could deal with only specific problems
1970 B Ken Thompson Could deal with only specific problems
1972 C Dennis Ritchie Loss generality of BCPL and B restored.
The integers are the whole numbers with a range of values supported by a
particular machine. Generally integers occupy one word of storage. The Floating
point numbers are stored in 32 bits with 6 digits of precision. Floating point
numbers are defined in C by the keyword float. When the accuracy provided by a
float number is not sufficient, the double data type should be used to define the
number. A double data type member uses 64 bits giving a precision of 14 digits. A
single character can be defined as a character data type. Characters are usually
stored in 8 bits (one byte) of internal storage.
Q.6 Write a note on C Tokens.
In C program smallest individual units are known as C Tokens. C has six types of
tokens namely Keywords, Identifiers, Constants, Strings, Special Symbol and
Operators.
Every C word is classified as either a keyword or an identifier. All keywords are
pre-defined words with pre-defined meaning and serve as basic building blocks
for program statements. Identifiers refer to the names of variables, functions and
arrays. These are the user defined names which consist of a sequence of letters
and digits with a letter as a first character. Both uppercase and lowercase
characters are permitted. Constants in C refer values that do not change during the
execution of a program. C supports basically Integer Constants, Real Constants
and Character Constants.
Q.7 Write a note or C Constants
Constants in C refer to fixed values that do not change during the execution of a
program. C supports basically Integer Constants, Real Constants and Character
Constants. An Integer constant refers to a sequence of digits. There are three
types of integers namely decimal, octal and hexadecimal. The numbers
containing fractional part are called real or floating pointconstants. These numbers
are shown in decimal notation, having a whole number followed by a decimal
point and the fractional part. A real number may also be expressed in exponential
notation. The general form of scientific notation is
Mintissa e exponent
A variable can be used to store a value of any data type. That is, the name has
nothing to do with its type. The syntax of declaring variable is as follows:
Data-type variable1, variable2, …… variable-n;
variable1, variable2 are the names of variables separated by commas. A
declaration statement must end with a semicolon. For example:
int no;
char name;
double percentage;
Here, int, char and double are the keywords to represent integer type, character
type and real type data respectively.
Q.11 What is escape sequence? Explain.
OR
Discuss the %[Conversion character] codes and backslash codes and their
uses.
C Language recognises the following escape sequences. When used with output
function like print( ), putcs( ), putchar( ) etc. the escape sequence help in
formatting output.
Escape Sequence Meaning
\n Newline
\t Tab
\b backspace
\r Carriage return/enter
\a Alert
\f form feed
\\ backslash
\- single quote
\" double quote
Q-12 What is user-defined type declaration ?
OR
What is similar in typedef and enum function ? Explain.
[b] print()
The printf() function is used to print out a message, either on screen or an paper.
The syntax of printf( ) function may be narrated as follows:
Syntax:
printf("Control string", variable1, variable2);
The control string of print( ) function/statement covers text message, escape
sequence and & codes. The print ( ) statement like all other statements in C should
be ended with a semicolon. Example:
Print(" Welcome to QUANBOOK of C");
(c) gets( )
A string is a array of characters. In simple terms, a string is a collection of more
than one characters represented together. To input string through keyboard one
can use gets function. General format of this command is …
gets (variable);
Ex. char name[15];
gets (name);
It's equivlent statement is…
scanf("%[\n]", name);
(d) puts( )
To display value stored in string variable, this function can be used. General
format is..
puts (variable);
Ex. puts (name);
To display a single character putchar( ) is used where as to display the array of
character, i.e. string, this function/statement is generally used. With the help of
loop construct, putchar( ) function can also display the array of characters one by
one.
(e) Write a note on getchar ( ) or getch ( ) or getche( )
The simplest of all input/output operations is reading a character from the
standard input/output device, i.e. keyboard and writing it to standard output unit,
i.e. monitor/screen. Reading a single caracter can be done by using the function
getchar( ). The syntax of getchar ( ) function is as follows:
Variable_name = getchar ( );
Variable_name is a valid C variable name that ha been declared as a character
type. When this statement is encountered, the computer waits until a key is
pressed and then assigns this character as a value to getchar function.
Char name;
Name = getchar( ),
The getchar ( ) function may be called successively to read the characters
contained in a line of text. It is important to note that getchar( ) function accepts
any character keyed in which includes RETURN and TAB even.
The print( ) statement is used to provide formatted output on the terminal. The
format codes plays important role ion generating formatted output. The
commonly used print format codes are listed hereinbelow.
Code Meaning
%c prints a single character
%d prints a decimal integer
%e prints a floating point value in exponent form
&f prints a floating point value without exponent
%g prints a floating point value either e-type of f-type depending on
value.
&i Print a single decimal integer
&o print an octal integer without leading zero
&s print a string
&u prints an unsigned decimal integer
&x prints a hexadecimal integer without leadiong Ox
Q-18 Explain the following decision making statements
(a) if statement
The if statement, as its name implies, is used to make decisions. If a
certain condition is true, we direct the computer to take one course of
action; if the condition is false, we direct it to do something else. This is
one of the most fundamental concept in computer science.
C allows decisions to be made by evaluating a given expression as true or
false. Such an expression involves the relational and logical operators.
Depending on the outcome of the decision, program execution proceeds in
one direction or another. The C construct that enables there testo be made
is called the if statement.
Syntax:
if (expression)
statement;
OR
If (expresson)
(
statement-1;
statement-2;
}
The keyword if must be followed by a set of parentheses containing the
expression to be tested. If the expression evaluates as true, the statements
in the braces will be executed, otherwise the same will be skipped by the
compiler. Consider the following segment of a program that is written for
processing of marks obtained in an entrance exam.
if total > 70
{
print(" Distinction);
}
The simple if condition can be explained with the following logical segments.
1. if (bank balance is zero)
borrow money
2. if (room is dark)
put on light
3. if (code is 1)
person is male.
4. if (age is more than 55)
person is retired.
(b) The if…else statement
The if statement by itself will execute a single statement or a group of statements,
where the condition is following if is true. It does nothing when it is false. To
execute one group of statements if the condition is true and another group of
statements if the condition is false, if…else statement can be used.
The general format of if…else statements can be narrated as under:
If (condition is true)
{
statement - 1;
statement -2;
}
else
{
statement-1;
statement-2;
}
A few regarding if…else is worth noting…
(1) The group of statements after the if up to and not including the else is called the if
block. Similarly the statements after the else forms the else block.
(2) The else is written exactly below the if. The statements in the if block and those in
the else block have been indented to the right.
(3) Had there been only one statement to be executed in the if block and only one
statement in the else block we could have dropped the pair of braces.
(4) As with the if statement, the default scope of else is also the statement
immediately after the else.
In case of calculating the salary of the employee or to calculate the marks of the
students, this statement can be used.
Example:
/* Calculation of Marks of Students */
/* Author : Ratish Kakkad Date : 12/12/2000 */
main( )
{
float m1, m2, m3, m4, m5, m6, m7, total, avg;
/* Accept marks of seven subjects*/
print("Enter Marks of Seven Subjects*/
print ("Enter Marks of Seven Subjects");
scanf ("%f %f %f %f %f %f %f", &m1, &m2, &m3,
&m4, &m5, &m6, &m7);
/* Calculate the total of marks obtained in 7 sub*/
total = m1+m2+m3+m4+m5+m6+m7;
The switch works as follows: When the statement is encountered, the switch
variable or expression is evaluated to yield a specific value. Program control is
transferred to the statement immediately following the case label whose constant
is equal to the value of the switch variable or expression. This statement is
executed, followed by all the succeeding statements in the body of the switch
statement.
A special label, default can be placed. This label is to allow for the possibility that
the value of the switch expression does not match ay of the case labels.
Continue;
Else
Print("%d",k);
Here on encountering continue control passes to I++, which increments the value
of I and then passes control to I<=45, which determines whether the body of the
loop should be executed or not.
Q.23 Write a note on ARRAY in C.
An array is a group of elements that share a common name, and that are
differentiated from one another by their position within the array.
The subscript used to declare an array sometimes is called the dimension and the
declaration itself often called dimensioning. As the regular variables, declaration
does not assign values, but simply set aside memory for the array. The dimension
used to declare on array must always be a positive integer constant or an
expression that can be evaluated to a constant when the program is compiled. The
characteristics of an array can be smmarized as follows:
1. An array is a collection of similar elements stored in adjacent memory locations.
2. Before using an array its size and type must be declared.
3. The first element (subscript) in the array is numbered O, so the last element is 1
less than the size of the array.
4. If array elements are not given any specific values, they are supposed to contain
garbage values.
5. There is no check to see if the subscript used for an array exceeds the size of the
array. Data entered with a subscript exceeding the array size will simply be placed
in memory outside the array; this will lead to unpredictable results.
Q.24 What is user defined function ? Explain the need of user defined functions.
OR
What is user defined function? Narrate the advantages of modular
programming?
C functions can be classified mainly into two categories, namely user-defined
functions and library functions. Main ( ) is a user defined function while print ( )
and scanf ( ) are library functions. The main distinction between these two
categories is that library functions are not required to be written by us whereas a
user-defined function has to be developed by the user at the time of writing a
program. However, a user-defined function can later be a part of the C program
library.
When, a program is coded utilizing only one main function, it leads to a number
of problem. The program may become too large and complex as a result the task
of debugging, testing and maintaining becomes too large and complex and as a
result the task of debugging, testing and maintaining becomes difficult. If a
program is divided into functional parts, then each part may be independently
coded and later combined into a single unit. These subprograms called 'functions'
are much easier to understand, debug and test. It may be summarized that a
function is a self-sections approach results in a number of advantages listed
hereinbelow:
(1) It facilitates top-down modular approach programming. In this programming
stype, the high level logic of the overall program is solved first while the details
of each lower-level function are addressed later.
(2) The length of a source program can be reduced by using functions at appropriate
places. This factor is particularly critical with microcomputers where memory
space is limited.
(3) As mentioned earlier, it is easy to locate the isolate a faulty function for further
investigation.
(4) A function may be used by other programs.
Q.25 Discuss the form of a C function.
All functions which may be used in a C program should have the following form
or format;
Function-name (argument list)
Argument declaration;
{
local variable declaration;
executable statement1;
executable statement2;
return (expression);
}
A function must follow the same rules of formation as other variables names in C
as regards the function name. The argument list contains valid variable names
separated by commas. The list must be surrounded by parentheses. No semicolon
follows the closing parenthesis. All argument variables must be declared for their
type after function header and before the opening braces of function body.
A function may or may not send back any value to the calling function. If it does,
it is done through the return statement.
int a, f;
print(" Enter any number");
scanf ("%d", &a);
f=recfact(a);
printf ("\n Factorial value = %d", f);
}
recfact(x)
int x;
{
int fact;
if (x==1)
return(1);
fact = recfact (x-1)"x;
return (fact)
}
When writing recursive functions, you must have an if statement somewhere in
the recursive function to force the function to return without recursive call being
executed. If you don’t' do this and you call the function, you will fall in an
indefinite loop and will never return from the called function which is a very
common error in writing recursive functions.
int subject-2;
int subject-3;
};
will store the marks of 3 subjects. To store the result of 10 students we should
assign struct marks class[10]; will give 10 subscript values to class which contains
3 records per subscript.
Q-29 What is a Pointer? Explain.
When an identifier identifies a memory location, which holds some value, is
called variable, if it identifies memory location that holds address of memory cell
is called pointer.
Pointers are much used in C, because:
1. A pointer enables us to access a variable that is defined outside the function.
2. Pointers are more efficient in handling the data tables.
3. Pointers reduce the length and complexity of a program.
4. They increase speed if execution.
5. The use of a pointer array to character string results in saving of data storage
space in memory.
To declare a variable as pointer, one has to use Value At Address Operator (*). It
returns the value stored at a particular address. It is also called direction operator.
Another operator used with pointer is & (Address of operator).
Ex. int |=5, *I;
I=&l;
printf (" value %d stored at %u address ", *I, I);
These statements tell the C compiler
1. Reserve the space in memory.
2. Associate name 1 with memory location.
3. Store value 5 at this location.
4. Assign address of | to pointer variable i.
You can access value 5 using variable | or by pointer variable i. Instead of array
pointer provides much faster access to your operation. You can use pointer with
any type of data type like int, cha float as well as with structure and function too.
We will discuss here how to use pointer with all these data types.
Q.30 Write a note on Array of Pointers
When an array is declared, value stored in contiguous memory locations. The base
address is the location of the first element of the array. If array contains 5 element
and the base address is 1000 the other element stored at address 1002, 1004, 1006,
1008. So if we declare p as an integer pointer as follows:
Int *p,x[5];
P =x; // equivalent to p=&×[0];
We can access every value of × using p++ to move from one element to another.
Example:
#include <stdio.h>
#include <conio.h>
main( )
{
int no[5], I, *I;
for (I=0;<5;I++)
scanf(%d", & no(i);
j=no;
for (I=0;I<5;I++)
<
printf(" %d", *I);
I++; // give two statements or print("%d", *(I+I));
}
}
Q.31 What is character pointer? Explain the use of pointers in defining character
string.
As we know string is an array of characters; terminated with a null character. You
can use character pointer to access each character of string. Rather than use
pointer in declaration of string, so that size can be ignored at the time of
declaration.
Example:
#include<conio.h>
main ( )
{
char *s; // length of string is not required in case of pointer
clrscr( );
print ("enter string : “);
gets (s);
puts ("\n string is … ”);
puts(s);
}
Q-32. Write a note on use of Pointers in Structures.
The pointers can be used in structures to make the execution fast and efficient. As
we know structure is a combination of more than one field of different data types,
the use of pointers in structure make it more easier to address the subscript of any
element of the structure. You can use pointer to access each element of the
structure. In a function call, if we refer pointe makes it more easier for the
programmer to pass the structure as a argument, i.e. actual argument.
Example:
#include<stdio.h>
#include<conio.h>
struct stud
{
char *name;
int age;
};
main ( )
{
struct stud st[3], t;
int I;
clrscr( );
[2] fscanf( )
It deals with formatted ionput conversion.
Syntax : fscanf(file pointer, const char *format, arguments…);
Ex. :fscanf(fp,”%s “%d”, name,&rollno);
[3] fgetc( )
It is same as getc function. It returns the next character from a named input
stream. If error or end of file encountered it returns EOF.
Syntax : fgetc(file pointer);
Ex. : ch=fgetc(fp);
Statement returns next character of a file to variable of type character.
[4] fputc( )
This function stores character to a specified stream.
[b] In case of DOS, a new line is stored a the sequence O×Od O×Oa both on disk and
in memory.
Q-39 Why a linked list is called a dynamic data s structure? What are the
advantages of using linked lists over arrays?
A linked list is a dynamic data structure. therefore, the primary advantage of
linked lists over array is that linked lists can grow or shrink in size during the
execution of a program. A linked list can be made just as long as reguired.
Another advantage is that a linked list does not waste memory space. It uses the
memory that is just needed for the list at any point of time. This is because it is
not necessary to specify the number of nodes to be used in the list.
The third, and more important advantage is that the linked lists provide flexibility
in allowing the items to be rearranged efficiently. It is easier to insert or delete
items by rearranging the links.
The major limitation of linked lists is that the access to any arbitrary item is little
cumbersome and time consuming. Whenever we deal with a fixed length list, it
would be better to use an array rather than a linked list. We must also note that a
linked list will use more storage than an array with same number of items because
each item has an additional link field.
Q-40 Describe different types of linked lists.
There are different types of linked lists. They can be listed as below:
[1] Singly Linked lists.
[2] Circular Linked lists.
[3] Two way or doubly linked lists.
[4] Circular doubly linked lists.
The circular linked lists have no beginning and no end. The last item point back to
the first item. The doubly linked lists uses double set of pointers, one pointing to
the next item and other pointing to the preceding item. This allows us to traverse
the list in either direction. Circular doubly linked lists employs both the forward
pointer and backward pointer in circular form.
The preprocessor as its name implies, is a program that processes the source code
before it passes through the compiler. It operates under the control of what is
known as preprocessor command lines or directive. Preprocessor directives are
placed in the source program before main ( ) line. Before the source code passes
through the compiler, the preprocessor for any preprocessor directives examines
it. if there are any appropriate actions are taken and then the source program is
handed over to the compiler.
Preprocessor directives follow special syntax rules that are different from the
normal C syntax. They all begin with the symbol # in the column one and do not
require a semicolon at the end. The preprocessor directives can be divided into
three categories:
[1] Macro Substitution Directives.
[2] File inclusion Directives.
[3] Campiler Control Directives.
The following is a list of preprocessor directives
Directive Function
#define Defines a macro substitution
#undef Underfiones a macro.
#include Specifies the files to be included
#ifdef Tests for a macro definition
#endif Specifies the end of #if
#ifndef Tests whether a macro is not defined.
#if Tests a compile time condition
#else Specifies alternative when #if test fails.
Q-42 What is a macro and how is it different from a C variable name?
Macro substitution is a process where an identifier in a program is replaced by a
predefined string composed of one or moretakens. The preprocessor accomplishes
this task under the direction of #define statement. This statement, usually known
as a macro definition takes the following format.
#define identifier string.
Simple string replacement is commonly used to define constants. The following
are the examples of constants:
#define identifier string.
with its macro expansion while in a function along with their arguments and
retunes back with useful value.
With the help of macro, program runs faster but on the other hand, increases the
program size which is against structured programming whereas functions make
the program smaller and compact but makes the processing/exection slower.
It is to be suggested/advocated that if there is a small function, it is advisable to
use macro and in big branches, functions are useful.
Q-46 What is COMMAND LINE ARGUMENTS? What its' significant in a c
program.
As we all know C language was developed under the UNIX operating system. Ion
Unix when you execute any compiled program, the program name (like a
command at prompt) can be followed by various arguments on the same line.
Which specifies how the program is to operate. C compiler also provides facility
where by the program can access command line arguments. In short it is a
parameter supplied to a program when the program is invoked. Main function
may have two arguments they are…
A. argc (Argument counter)
B. argv (Argument vector)
argc is always type of integer. It contains total no. of argument passed to main
function. Whereas, argv is a pointer to an arrayof character string that contain the
arguments one per string.
In order to be able to access command line arguments, the main function of the
program must be defined as having two arguments as given below:
main (int argc,char *argv[])
Example :
Following program shows the use of command line argument to find the
multiplication of numbers passed as arguments to main function.
// program name : GUNAKAR.C
#include <stdio.h>
#include <conio.h>
main(int argc,char *argv[])
{
long int m=1, k;
clrscr( );
if (argc<2)
{
printf(“two few arguments…”);
getch ( );
exit(0);
}
for (k=1;k<argc;k++)
m=m *atoll (argv[k]);
pritnf(“Answer = %ld", m);
getch( );
}
While executing at prompt you may give commands as:
Gunakar 1 2 3 4 5
Output: Answer = 120
Here,
Argc will be 4.
Argv[0]= program name i.e. gunakar
argv[1]=”1"
argv[2]=”2"
argv[3]=”3"
argv[4]=”4"
argv[5]=”5"
Q-47 What is DYNAMIC MEMORY ALLOCATION ?
It is important to programmers to write programs that use a minimum of memory
as well as programs that are fast. But most often we face situations in
programming where the data is dynamic, means number of data items keep
changing during execution of the program. Array can be used fro mass data but
they are of fixed size. The programmer must know the size of the data or array
while writing the programs. But it is not possible to know the exact size of
memory required until run time; here dynamic memory allocation is required.
C. provides facility to allocate memory for the local variables from heep (the
program instruction and global variables are stored in a region known as
permanent storage area and the local variables are stored in another area called
stack. The memory space the located between these two regions is available for
dynamic memory allocation during execution of the program. This free memory
region is called the heap) while executing a program is known as Dynamic
Memory Allocation. It means size of memory required is not pre determined but
as per users requirement it will be allocated after execution being started. You
may use calloc( ), malloc ( ) free ( ) and realloc ( ) functions by dynamic memory
allocation.
Q.48 Write a note on Functions which may be used for Dynamic Memory
Allocation
The header file associated with these functions is <stdlib.h>.
char * calloc(int items, int size);
This function is usd to allocate dynamically a block of memory for nitems
elements of size bytes each. That is, the total number of bytes allocated us nitems
* size. The block of memory is cleared to zeros. A pointer to the allocated block is
returned. It can be cast into a pointer of any type.
void free(char *block);
This function de-allocates the block of memory pointed to by block, making it
available for latter allocation. The block must have been allocated by a previous
call to caloc, malloc or realloc, but not preciously freed by a call to free. Once a
block of memory is freed, it should not be referenced.
Char *malloc(int size);
This function is used to allocate dynamically a block of memory of size bytes.
The block of memory is not initialised. A pointer to the allocated block is
returned.
char realloc(char *old, int size);
SHORT QUESTIONS
[1] What is a variable ?
It is a symbolic name for a memory location in which a value is stored.
[2] What is an Integer ?
It is a whole number, one without a decimal point.
[3] Why the assignment statement regarded as symbolic?
Because during execution of the program, the expression to the rights of the equal
sign is reduced to a single number which is then stored in variable specified on
the left.
[4] How many variables may appear on the left of the eqal sign ion an
assignment statement?
Only one
[5] What are the rules for the naming of variables ?
They must begin with a letter [including the underscore], may contain only letters
and digits, may not include special characters may not be a keyword and the first
eight characters or so [depending upon the implementation] must be unique.
[6] What purpose is served by including white space in a program?
It highlights program statements and output, and makes the program listing easier
to read.
[7] Which of the two operators, multiplication or division has the higher
precedence?
Neither; they have the same priority level. If they are both present, they are
evaluated from left to right.
[8] Distinguish between the binary and the unary minus.
The binary minus has an operand before and after the minus sign, and is used for
subtraction. The unary minus has only one operand - to its right - and serves to
convert a positive value into its corresponding negative value and vice versa.
[9] What is meant by truncation?
It is the rounding down effect achieved, among other times, when an integer is
divided by another integer and resulting fractional portion is chopped off.
Floating point numbers have a broader range; may have a fractional portion; are
stored differently; use the %e of %f conversion specifier rather than %d; and may
result in slower operations.
[15] What is a character constant?
It is a single character enclosed by single quotation marks.
[16] What is the maximum number of characters, which a variable of type char
can hold?
One
[17] What function enables a user to ionput information while the program is in
execution ?
The Scanf( ) function
[18] Which statement in C permits decisions to be made?
The if statement
[19] How many relational operators are there and what they are?
Six, as follows:
== equal to
!= not equal to
>= grater than or equal to
<= less than or equal to
< less than
> greater than
[23] Which operators can be used with the updating assignment operators?
All binary operators (+,-,*,/,% and others) can be used with the updating
assignment operators.
[24] What is the difference between pre-decrement and post-decrement
operators?
The pre-decrement decreases the value before the test expression while the post
decrement operator will decrease the value after the completion of the test
expression.
[25] What is the essential difference between a while and do..while loop?
A while loop prefers its test before the body of the loop is executed, whereas a
do… while loop makes the test after the body is executed.
[26] What happens if a condition in a while loop is initially false?
The whole loop is skipped over.
[27] Which is the better loop to use, the for loop or the while loop?
It depends on the nature of the problem to be solved, and upon the preference of
the programmer.
[28] Which is a special advantage of the for loop ?
The initialisation, condition and counter, i.e. modifier expression of the loop all
are specified within a single set of parenthesis.
In nested for loop, the body of one for loop contains another for loop.
[32] When is the comma operator used in a for loop ?
When the first [initialisation] and/or third [counter/modifier] expression of the for
statement contain more than one expression.
[33] What conversion specifications are used to print out the following ?
[a] an Integer
[b] a floating point number in decimal point.
[c] a floating point number in scientific notations.
[d] a single character.
%d is used for Integer, %f for floating point number in decimal point, %e
fro exponent values and %c for single character.
[34] What does the word ASCII stands for?
American Standard Code for information Interchange, which is the most popular
code used fro microcomputers.
[35] Which letters have higher ASCII values, upper case of lower case?
Lowercase characters.
[36] What are the ASCII codes for A to Z ?
65 to 90 respectively.
[37] What are the control characters?
The control characters are the unprintable characters through which the user can
control the output.
[38] Give some examples of control characters.
Backspace, tab, newline, carriage return, from feed and bell can be narrated as the
examples of control characters.
[39] How can a % sign be printed within a control string ?
By using two adajacent % signs.
[40] What is the difference between conversion specification letters used in printf
and those used ion scanf?
Generally there is no difference in the set of letters used. There is some difference
in operation, however; for example, %f is used in print always displays a floating
The content of variable may change during execution of the program, but its
address cannot be changed.
[71] What is the role-played by the & operator in a call to the scanf function ?
It passes the address of the variable being input, making it possible for the
contents of that location to be changed.
[72] What are the restrictions regarding the use of the & operator?
It can be used only before the name of a scalar variable or a subscripted array
element, not before an unsubscripted array name or an expression.
[73] Can addresses be stored? If so, where?
Addresses can be stored in pointer variable.
[74] What is the indirection operator, and what role does it play?
The indirection operator is the asterisk (*). When placed before the name of a
pointer variable, it means that the location being referred to is not pointer variable
but the location pointed out by the pointer variable.
[75] What does passing of reference mean?
Passing by reference is the term used to refer to the passing of a variable address
to a function.
[76] What is the fundamental difference between passing by value and passing by
reference?
When a variable is passed by value to a function, only its value is passed. The
function cannot change the contents of the variable. Passing by reference means
that the variable address is passed, giving the function the power to change the
contents of that variable.
[77] When a function is written to swap the value of two variables, what special
provisions must be made?
The parameters must be the addresses of the variables, and the contents of the
locations pointed to by the addresses must be exchanged.
[78] In what ways are arrays and pointer involved with each other?
The un-subscripted name of an array is interpreted as constant pointer; only a
pointer to an array can be passed to a function; any pointer variable can be
subscripted.
[79] Why & operator is not used with the array names in a scanf statement?
Since the name of an array is already & constant pointer, the & would be both
illegal and redundant.
[80] Why is only the address of an array's first element necessary in order to
access the entire array?
Since all the elements of an array are of the same type, and all the elements are
stored contiguously in memory, the location of the first element can be used to
find the second element, the third element and so on.
[81] In what order must the parameters of a function be declared?
They may be declared in any order.
[82] What can be said about adding an integer to a pointer?
It is illegal. It returns to a pointer, which is the original pointer incremented by the
number of target units specified by the integer.
[83] Under what circumstances can be pointer be subtracted from an integer?
Under no circumstances, it is legal. [The pointer can be caste into an integer, but
this rarely would have any value]. It is however, legal to subtract an integer from
a pointer.
[84] What is returned when a pointer is subtracted from a pointer and under
what conditions is tat permitted?
The result is the distance between pointer expressed in target units. The two
pointers must be of the same type, and the first pointer must have a greater value
than the one being subtracted from it.
[85] What are the conditions under which the <and> operators are meaningful
when used with pointer?
The two pointers must point to elements of the same array.
[86] Why it is harder to compare string than numeric values?
String cannot be compared in a single operation; each character must be compared
with its corresponding element in the other array.
[87] How can an array of strings be represented in C?
As a two-dimensional array of characters, in which each row is a separate string.
[88] What is the maximum number of labels permissible in a switch statement?
switch body, the rest of the statements in the body are skipped and execution
resumes after the switch statement.
[97] How can a string sorting technique be improved by resorting to pointer?
A pointer of any depth can represent the string. [limited only by the compiler]
instead of physically exchanging two strings when necessary, the pointers are
exchanged.
[98] What is the advantage of representing an array of strings by an array of
pointers to string?
If allows for easy moving of the strings by moving their pointers. It also allows
for the possibility that all the car array being pointed to may be of different
maximum lengths, thus, allowing for more efficient use of memory.
[99] What is another name sometimes given to arrays of type char?
Strings
[100] How is the end of a string recognized in C ?
By the null character ('\0')
[101] How is a literal accessed within a program?
By a pointer to the literal.
[102] What is the null string? What is its length?
The null string is an empty pair of double quotation marks@@. It is stored as null
character with no preceding characters. Its length is zero.
[103] What are static variables? Compare them with standard local variables?
A static variable is created when the program begins execution, and remains in
existence until the program terminates. A local variable declared, as static does
not disappear when the function ion which it is declared terminates.
[104] Explain the meaning of auto variable.
An auto variable comes to its existence when the function in which it is declared
is executed. It disappears when the function returns. The next time the function is
called, it is recreated.
[105] What is the role of the sprintf function ?
If stores its output in a string [ its first argument] instead of displaying or printing
it directly.
It returns the size in bytes [or, more generally char-sized units] of its operand.
[116] What is the min reason fro using structure?
In day-to-day applications, it is helpful to group together non-homogeneous data
into a single entity.
[117] What distinguishes an array from a structure?
Whereas the elements of an array are always of the same data type, the members
of a structure can be of different types.
[122] What is the connection between a structure and a new data type?
Associating a tag with a template makes it possible to use the tag [preceded by the
keyword struct] in the same way a type keyword is used, for example, to declare
variables or the types returned by functions.
MULTIPLE CHOICE
[1] C Language ha been developed by
[a] Ken Thompson [b] Dennis Ritchie
[c] Peter Norton [d] Martin Richards
[2] Language has been developed at
[a] Microsoft Corp. USA [b] At & T Bell Labs USA
[C] Borland International USA [d] IBM USA
[3] C Language come into existence in the year
[a] 1971 [b] 1957
[c] 1972 [d] 1983
[4] C is a
[a] Middle level language [b] 1957
[c] Low level language [d] None of above
[5] C can be used on
[a] Only DOS Operating System [b] Only UNIX Operating System
[c] Only Xenix operating system [d] All of above
[6] C programs are converted into machine level language with the help of:
[a] An Interpreter [b] A Compiler
[c] An operating system [d] None of above
[7] The real constant in c can be expressed in which of the following forms:
[a] Factorial form only [b] Exponential form only
[c] An operating system [d] None of above
[7] The real constant in c can be expressed in which of the following forms:
[a] Factorial form only [b] Exponential form only
[c] ASCII form only [d] Both fractional and exponential forms
[17] A do-while loop is useful when we want the statements within the loop must
be executed:
[a] Only once [b] At-least twice
[c] More than once [d] None of the above.
[22] What is the difference between the 5@ in these two expressions? (Select the
correct answer)
int num [5];
num [5] = 11;
[a] first is particular element, second is type
[b] first is array size, second is particular element
[c] first is particular element, second is array size
[d] both specify array size.
[23] What will happen if you try to put so many values into an array when you
initialise is that the size of the array is exceeded?
[a] nothing [b] possible system malfunction
[c] error message from the compiler [d] other data may be overwritten.
[24] What will happen if you put too few elements in an array when you initialise
it?
[a] nothing [b] possible system malfunction
[c] error message from the compiler [d] elements with garbage values
[25] What will happen if you assign a value to an element of an array whose
subscript exceeds the size of the array?
[a] the element will be set to 0 [b] nothing, its done all the time
[c] other data may be overwritten [d] error message from the compiler
[26] When you pass an array as an argument to a function, what actually gets
passed ?
[a] address of the array [b] value of the elements of array.
[c] address of the first element of array [d] number of element of the array.
[28] If you do not initialize a static array, what will be the elements set to ?
[a] 0 [b] print( )
[c] scanf( ) [d] puts ( )
[31] To receive the string "We have got the guns, you get the glory" in an array char
str[100] which of the following functions would you use?
[a] scanf(“%s”, str); [b] gets (str);
[c] getche(str); [d] fgetchar(str);
[32] Which function would you use if a single key is to be received through the
keyboard?
[a] scanf( ); [b] gets( ) ;
[c] getche( ) ; [d] getchar( );
[33] Ifan integer is to be entered through the keyboard, which function would you use?
[a] scanf( ); [b] gets( ) ;
[c] getche( ) ; [d] getchar( );
[34] If a character string is to be received through the keyboard which function would
work faster?
[a] scanf( ) ; [b] gets ( ) ;
[38] On opening a file for reading which of the following activities are performed:
[a] The disk is searched for existence of the file.
[40] The order of three parts of a loop expression in the for loop should be:
[a] Initialization, test, increment [b] test, initialisation, increment
[c] increment, test, initialization
[44] The declaration void function1 [int] indicates the function1 is a function
which
[a] has no arguments [b] returns something
[c] both a and b [d] none of the above.
[54] Which of the following 'C' type is not a primitive data structure?
[a] int [b] float
[61] In a for loop with a multi statement loop body, semicolons should appear ion
following:
[a] the for statement itself.
[b] the crossing brace in the multiple statement loop body.
[c] each statement within the loop body and the text expression.
[d] none of the above.
[64] A pointer is
[a] Address of a variable.
[b] An indication of the variable to be accessed next.
[c] a variable for storing address.
[d] None of the above.
[70] In order to check whether an input number is vowel or not, an efficient multiway
decision making routine can be written using
[a] while loop [b] switch
[c] if-then-else [d] none of the above
[73] Which of the following statements is true after execution of the program.
Int a[10], I, *p;
A[0] = 1;
A[1] = 2;
P = a;
(*p) ++;
a. a[0] = 2 b. a[1] = 3
c. a[1] = 2 d. all
[75] Given the following program fragment, which one of the alternatives is correct ?
main ( )
{
char status;
int balance;
balance = 1000;
status = (balance >= 1000)? ‘C’: ‘O’ ;
}
a. status = ‘O’ b. status = ‘C’
c. status = O; d. status = NIL
[80] What is the value of average after the following program is executed ?
main ( )
{
int, sum, index;
index = 0;
sum = 0;
for (; ;)
{
sum = sum + index ;
++ index;
if (sum >= 100) break;
}
average = sum/index;
}
a. 91/4 b. 91/13
b. 105/15 c. 105/14
[81] What is the output of the following program ?
main ( )
{
int x, y, z;
X=2
Y = 1;
Z = 1;
If (x > y + z)
Print(“Hello\n”);
Else if (x < y + z)
Printf (“Hey!n”);
Else
Printf (“Hey!n”);
}
a. Hi ! b. Hello !
c. Hey ! d. None
POLYGON = (L = = B) ? 1:0:
}
a. 0 b. 1
c. 2 d. None of the above
a. 00 b. 0 1
c. 11 d. 1 0
main ( )
{
int B, X, Y, Z;
X = 1;
Y = 2;
Z = 3;
if ((X > 1) (Y > 1))
if (Z > 1)
print ("O.K.=n“) ;
else break ;
if (X > 1) && (Z >3))
printf (“Bye\n”);
print(“Hello“) ;
}
a. O.K. Bye b. Bye
c. O.K. d. Hello!
}
a. good account b. caution
b. good account caution d. good account caution.
(87) The following lines, if included in a program, will cause one of the following
errors. Indicate the correct one.
{double c;
scanf(“%c”, c);
}
a. Runtime error b. Compilation error
c. Typedf error d. No error
[88] If the following variable are set to the values as shown below, then what is the
value of the expression following it ?
answer = 2;
marks = 10;
!(answer > 5) (marks > 2))
a. 1 b. 0
c. -1 d. 2
[91] If c is a variable initialised to 1, how many times will the following loop be
executed ?
while ((c > 0) && (c < 60)) {
loop body
c ++; }
a. 60 b. 59
c. 61 d. 1
[92] If x and y are variable are declared as double x = 0.005, y = -0.01; What is the
value of cell (x + y), where cell is a function to computer celling of a number ?
a. 1 b. 0
c. 0.005 d. 0.5
[93] The for statement which can precede a loop to be executed 50 times or till a
Boolean variable “found” becomes falase is give by
a. for (i = 0; i <= 50 found == false; i++)
b. for (i = 0; i < 50 found = = true; i++)
c. for (i = 1; i < = 50 found = = true; i++)
d. none of the above
typedef struct {
char name [20];
char middlename [5];
char surname [20];
} NAME
NAME class [20];
Here,
a. class is an array of 20 characters only
b. class is an array of 20 names where each name consists of a name, middlename
and surname
c. class is a new type
d. none of the above
[95] What would be the values assigned to a,b,c if the statement scanf(“%d %d %d”,
&a, &b, &c) is extended with input data item 123456 ?
a. a = 12, b = 34, c = 56
b. a = 1, b = 2, c = 3
c. a = 123456 and nothing is assigned to b and c
d. a and b are not assigned anything, c = 123456
[96] What would be the values assigned to a, b and c is the statement scanf(“3d, %3d,
%3d”, &a,&b, &c) is executed with input data as 1234b5678b9 (b denotes
blank) ?
a. a = 123, b = 4, c = 567 b. a = 123, b = 567, c = 9
c. a = 123, b = 456, c = 789 d. a = 1234, b = 5678, c = 9
[97] What would be the values of I, x and c if scanf(“3d. %5f. %c”, &I, &x, &c) is
executed with inpout data 10b256.875T ?
a. i = 10, b = 56,875, c = T b. i = 100, b - 256.87, c = T
c. i = 010, b = 256.87 c = '5' d. i = 10, b = 256.8, c = '7'
[98] What would be the assignments if char s[100]; int d; float f; scanf(“%s, %*f, %f”,
&d, &f) is executed with input data fastener b12345b5
a. s = “fastener”, d = 12345, f = 0.05 b. s = “fastener”, d = 123*45, f = 0.05
c. s = “fastener”, f = 0.05 d. s = “fastener”, d = 0.05
[107] How many times will the following loop be executed if the input data
item is 01234 ?
while (c = getchar( )!) {
a. infinitely b. never
c. once d. 5 times
int c;
float a, b;
a = 245.05;
b = 40.02;
c = a + b;
[109] What would be the value of i and k ?
{
int i, j, k;
I = 5;
I = 2 * j/2;
K = 2* (i/2);
}
a. i = 5, k = 5 b. i = 4, k = 4
c. i = 5, k = 4 d. 4
struct name {
int age ;
char name [20];
}
a. 5 b. 24
c. 2] d. 22
[114] The library function sqrt operates on a double precision argument. If, i
is an integer variable, which one of the following calls would
correctly compute sqrt(i) ?
a. sqrt ((double)i) b. (double) sqrt(i)
c. (double)(sqrt(i)) d. sqrt(i)
{ :
:
If (error < 0.005) break :
}
The above loop will
a. always run for 10,000 times b. will never run for 10,000
times
c. may or may not run for 10000 times d. none of the above.