Professional Documents
Culture Documents
Ans:
Development of ‘C’:
‘C’ was originally developed in the 1970s by Dennis Ritchie at Bell Telephone Laboratories, Inc.
C was largely confined to use within Bell Laboratories until 1978, when Brian Kernighan and
Dennis Ritchie published a definitive description of the language. In 1983, American National
Standards Institute (ANSI) appointed a technical committee to define a standard for C. The
committee approved a version of C in 1989 which is now known as ANSI C. It was then
approved by International Standards Organization (ISO) in 1990.
Importance of C:
1. It is a robust language whose rich set of built-in functions and operators can
be used to write any complex program.
2. The C compiler combines the capability of an assembly language with the
feature of a high-level language.
3. Well-suited for writing both system software and business packages.
4. Due to its variety of data types and powerful operators, programs written in C
are efficient and fast.
5. C is highly portable.
6. C language is well suited for structured programming that makes program
debugging, testing, and maintenance easier.
7. C has the ability to extend itself. We can continuously add our own functions
to the C library.
Ans:
Structured programming:
Structured programming refers to a programming strategy that encompasses a number of
methodologies to produce good quality design and code, which can be easily understood, tested,
debugged, modified, and maintained in the future. There are three main principles of structured
programming:
‘C’ supports these features of structured programming strategy hence ‘C’ is called ‘Structured
programming language’.
The below figure illustrates the overall process of creating, compiling, and executing a ‘C’
program.
Ans:
C Tokens:
The smallest element identified by the compiler in a source file is called a token. It may be a
single character or a sequence of characters to form a single item. C tokes are classified into six
types: Keywords, Identifiers, Constants, Strings, Special symbols, and operators.
Ans:
Keywords:
In ‘C’, there are certain reserved words that have standard predefined meanings, these are called
keywords. These keywords can be used only for their intended purpose; they cannot be used as
programmer-defined identifiers.
Identifiers:
Identifiers refer to the names of variables, functions, and arrays. These are user-defined names
and consist of a sequence of letters and digits, with a letter as a first character. Both uppercase
and lowercase letters are permitted. The underscore character is also permitted and the identifier
name must not contain any white space.
Ans:
Variable:
A variable is a data name that may be used to store a data value. Unlike a constant that remains
unchanged during the execution of a program, a variable may take different values at different
times during execution.
Ans:
Escape sequence characters:
An escape sequence always begins with a backslash ( / ) and is followed by one or more
characters. For example, a line feed (LF), which is referred to as a newline in ‘C’, can be
represented as \n . Such escape sequences always represent single characters, even though they
are written in terms of two or more characters.
8. Determine which of the following numerical values are valid constants. If a constant is
valid, specify whether it is integer or float. If invalid, explain why?
i) 1.0 ii) 32,767 iii) 1.3e-12.3 iv) 0587 v) 12345678L vi) 2.8E 7
Ans:
String constant:
A sequence of characters is enclosed in double quotes. The characters may be letters,
numbers, special characters, and blank spaces.
Ans:
Constants in C refer to the fixed values that do not change during the execution of a program.
The preprocessor #define is another more flexible method to define constants in a program.
#define TRUE 1
#define FALSE 0
#define NAME_SIZE 20
Here TRUE, FALSE and NAME_SIZE are constant
10. State the differences between the declaration of a variable and the definition of a
symbolic name.
Ans:
A variable may take different values at different times during execution whereas the value of the
symbolic name constants remains unchanged during the execution of the program.
Declaration of variables:
Primary Type Declaration:
data type V1, V2, ….. Vn;
where V1, V2, ….. Vn are the names of variables.
Ans:
The ‘C’ language has four basic data types, namely; int, char, float, double
12. How could we extend the range of values of basic data types?
Ans:
The basic data types can be augmented by the use of the data type qualifiers short, long, signed,
and unsigned. For example, integer quantities can be defined as short int, long int, or unsigned
int. Thus a short int may require less memory than an ordinary int or it may require the same
amount of memory as an ordinary int, but it will never exceed an ordinary int in word length.
Similarly, a long int may require the same amount of memory as an ordinary int or it may require
more memory, but it will never be less than an ordinary int.
Ans:
Library function:
‘C’ library functions are the built-in functions that are accompanied along with the C compiler.
They are defined and declared in their respective header files. They are readily available
functions that can be used directly without writing a code for them. For example: printf ( ) and
scanf ( ) are the formatted input and output functions that are declared and defined in the header
file <stdio.h>
Ans:
stdio.h refers to the “standard I/O header file” containing standard input and output functions.
int i = 8, j = 5;
double x = 8.5, y = -2.1;
char c = ‘E’, s[10] = “CSE CU’’
Determine the value of each of the following expressions which involve use of library
functions.
Ans:
i) 2
ii) 7.000000
iii) 10.000000
iv) 256.000000
v) e
vi) 6
16. What is an Operator? Write the classification of different types of the operator that
are included in C.
Ans:
Operator:
An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operators are used in programs to manipulate data and variables. They usually
form a part of the mathematical or logical expressions.
For example, the expression a=a+1; can be written by using the shorthand assignment operator
as a +=1;
18. What is the difference between ++i and i++? Explain with example.
Ans:
A prefix operator (++i) first adds 1 to the operand and then the result is assigned to the variable
on left. For example, consider the following statement,
i = 5;
y = ++ i;
after execution the value of y = 6 and i = 6.
On the other hand, a postfix operator (i++) first assigns the value to the variable on left and then
increments the operand.
For example, consider the following statement,
i = 5;
y = i++;
after execution the value of y = 5 and i = 6.
The sizeof operator is normally used to determine the lengths of arrays and structures when their
sizes are not known to the programmer. It is also used to allocate memory space dynamically to
variables during the execution of a program.
Ans:
Operator precedence:
Operator precedence is used to determine how an expression involving more than one operator is
evaluated. The operators at higher levels of precedence are evaluated first.
Associativity:
The operators of the same precedence are evaluated either from ‘left to right’ or from ‘right to
left’, depending on the level. This is known as the associativity property of an operator.
Ans:
As the expressions within parentheses assume the highest priority so we can alter the natural
precedence of operations by using parentheses. If two or more sets of parentheses appear one
after another as shown below, the expression contained in the left-most set is evaluated first and
the right-most set in the last.
9-12/(3+3)*(2-1)
Parentheses may be nested and in such cases, evaluation of the expression will proceed outward
from the innermost set of parentheses. Just make sure that every opening parenthesis has a
matching closing parenthesis. For example,
9-(12/(3+3)*2)-1 = 4
Ans:
Automatic / Implicit type conversion:
C automatically converts any intermediate values to the proper type so that the expression can be
evaluated without loosing any significance. This automatic conversion is known as implicit type
conversion. If the operands are of different types, the ‘lower’ type is automatically converted to
the ‘higher’ type before the operation proceeds.
int main()
{
char x;
int i;
x = 'a';
i = x; /*Implicitly convert x to int, and assign the result to i */
}
int main()
{
char x;
int i;
x = 'a';
i = (int)x; /*Explicitly convert x to int, assign the result to i */
}
23. What is the limitation of scanf function in case of taking string data?
Or
What is the disadvantage of scanf function in case of taking string input? How will you
solve this problem?
Or
What is the problem of using scanf ( ) in case of taking a line of text? How can u solve
this problem?
Ans:
The limitation of scanf ( ) function in case of taking string data is that %s specifier cannot be
used to read strings with blank spaces i.e.; it can read only the word before the first space. But this
problem can be solved with the help of %[ ] specification. Blank spaces may be included within
the brackets, thus enabling the scanf ( ) to read strings with blank spaces.
Ans:
The specification %[characters] means that only the characters specified within the brackets are
permissible in the input string. If the input string contains any other character, the string will be
terminated at the first encounter of such a character.
The specification %[^character] does exactly the reverse. That is, the characters specified after
the circumflex (^) are not permitted in the input string. The reading of the string will be
terminated at the encounter of one of these characters.
Ans:
Flag Meaning
- Output is left-justified within the field. The remaining
field will be blank
+ + or – will precede the signed numeric item.
0 Causes leading zeros to appear.
#(with o or x) Causes octal and hex items to be preceded by O or Ox,
respectively.
#(with e, f or g) Causes a decimal point to be present in all floating-point
numbers, even if it is the whole number. It also prevents
the truncation of trailing zeros in g-type conversion.
26. Write the output of the following program. [Spring 2012 3(a)]
Ans:
y = 98.7654
Ans:
Ans:
Decision-making statements are used to execute a part of a statement based on some conditions
and exclude others. The decision-making statements available in C are:
1. if statement.
Simple if statement
if … else statement
Nested if … else
else … if ladder
2. switch statement.
3. Conditional operator statement.
4. goto statement.
29. Define branching. What are the branching techniques used in ‘C’?
Ans:
Branching:
A realistic ‘C’ program may require that a logical test be carried out at some particular point
within the program. One of several possible actions will then be carried out, depending on the
outcome of the logical test. This is known as branching.
30. Show the general form to write if-else statement with an example.
Ans:
The general form of an if-else statement is:
if( test expression )
{
True-block statement(s);
}
else
{
False-block statement(s);
}
statement-x;
Here is an example:
#include<stdio.h>
void main ( ) {
float first, second;
scanf (“%f, %f”, &first, &second);
if (first==second)
printf (“\n Both numbers are equal.”);
else
printf (“\n Numbers are not equal.”);
}
Ans:
The if … else structure is called a double-selection structure because it selects between two
different actions. The general form is,
On the other-hand, else…if ladder structure is used when multipath decisions are involved. The
conditions are evaluated from top of the ladder to downwards. The general form is,
if(condition 1)
statement-1;
else if(condition 2)
statement-2;
else if(condition 3)
statement3;
else
default-statement;
statement-x;
32. Draw the flow chart of nesting if…else and else if ladder.
Ans:
Ans:
Sometimes it becomes cumbersome to write lengthy programs using if and if-else statements. To
avoid this we can use Switch statements. The switch statement is used to select multiple
alternative execution paths. This means it allows any number of possible execution paths.
However, this execution depends on the value of a variable or expression. The switch statement
in C is the best way to test a single expression against a series of possible values and execute the
code. Here is the general form of a switch statement:
Conditional operator:
The conditional operator is the combination of ? and :, and takes three operands. It is useful for
making two-way decisions. The general form is:
The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated
and is returned as the value of the conditional expression. Otherwise, expression2 is evaluated
and its value returned.
Drawbacks:
When the conditional operator is used, the readability of the program is poor. So it is better to
use if statements when more than a single nesting of the conditional operator is required.
Ans:
Comparison between if … else and ? : operator:
if … else ? : operator
The general form of if … else statement is: The general form of ? : operator is:
statement-x;
When if … else is used the code is lengthy When a conditional operator is used, the
and inefficient. code becomes more concise and perhaps,
more efficient.
The code is well-understood i.e.; the The readability of the code is poor.
readability of the code is easy.
Better to use if statements when more than Better to use ? : operator when only a
a single nesting of the conditional operator single nesting of the conditional operator is
is required. required.
37. What are forward jumping and backward jumping? How can we make these jumping
in program?
Ans:
Forward jumping:
In goto, if the label: is placed before the statement goto label; a loop will be formed and some
statements will be executed repeatedly. Such a jump is known as the forward jump.
Backward jumping:
In goto, if the label: is placed after the statement goto label; some statements will be skipped and
the jump is known as the backward jump.
Ans:
‘goto’ statements can cause a jump to any location in your source code, backward or forward.
The indiscriminate use of ‘goto’ statements has caused tangled, miserable, impossible-to-read
programs known as "spaghetti code." Because of this, it is not feasible to create a loop using
goto.
To avoid the use of ‘goto’, more sophisticated, tightly controlled looping commands have been
introduced: for, while, and do...while. Using these makes programs that are more easily
understood, and ‘goto’ is generally avoided, but one might argue that the case has been a bit
overstated. Like any tool, carefully used and in the right hands, ‘goto’ can be a useful construct,
and the ANSI committee decided to keep it in the language because it has its legitimate uses.
Ans:
Loop:
A loop is a sequence of statements that is continually repeated until a certain condition is reached. A
program loop consists of two segments, one known as the body of the loop and the other known as the
control statement.
Ans:
Looping:
A program may require that a group of instructions will be executed repeatedly until some
logical condition has been satisfied. This is known as looping.
Ans:
Entry-controlled loop:
In the entry-controlled loop, the control conditions are tested before the start of the loop
execution. If the conditions are not satisfied, then the body of the loop will not be executed.
Example: while loop.
Exit-controlled loop:
In the case of an exit-controlled loop, the test is performed at the end of the body of the loop and
therefore the body is executed unconditionally for the first time.
Example: do-while loop.
42. What is the minimum number of times that the do-while loop can be executed?
Ans:
Since the test condition is evaluated at the bottom of the loop, the do…while construct provides
an exit-controlled loop and therefore the body of the loop executed at least once.
Ans:
while do … while
The while is an entry-controlled loop that is The do…while is an exit-controlled loop that
the control conditions are tested before the is the test is performed at the end of the
start of the loop execution. If the conditions body of the loop and therefore the body is
are not satisfied, then the body of the loop executed unconditionally for the first time
will not be executed. i.e., the body of the loop executed at least
once though the conditions are not satisfied.
44. Convert the following program from for loop to while loop.
int i, n = 10;
for (i=0; i<n; i++)
{
printf ("I like for loop.\n");
printf ("%d\n",i);
printf ("I also like while loop.\n");
}
Ans:
The converted code is:
int i, n = 10;
i=0;
while ( i<n ) {
printf("I like for loop.\n");
printf("%d\n",i);
printf("I also like while loop.\n");
i=i+1;
}
Ans:
The converted code is:
y=50;
for ( x=5; x<=y; y=y/x)
{
printf("%d", x);
}
Ans:
for(n=1;n<=10;++n) n=1; n = 1;
{ while (n<=10) do
------------- { {
------------- -------------- --------------
} -------------- --------------
n=n+1; n=n+1;
} }
while (n<=10);
Ans:
The comma operator is used in conjunction with the for statement. This operator permits two
different expressions to appear in situations where only one expression would ordinarily be used.
By using comma operator, more than one variable can be initialized at a time. For example the
statements
p = 1;
for ( n=0; n<17; ++n)
can be rewritten as
for ( p=1, n=0; n<17; ++n)
Like the initialization section, the increment section may also have more than one part. For
example, the loop
for ( n=1, m=50; n<=m; n=n+1, m=m-1)
{
p = m / n;
printf ( “%d %d %d \n”, n, m, p)
}
is perfectly valid. Here the multiple arguments in the increment section are separated by commas
48. What is the null statement in the case of looping? Explain its usefulness.
Ans:
Null statement:
The body of the loop contains only a semicolon, known as a null statement. We can set up time-
delay loops using the null statements as follows:
for( j=1000; j>0; j=j-1 )
;
This loop is executed 1000 times without producing any output; it simply causes a time delay.
This implies that ‘C’ compiler will not give an error message if we place a semicolon by mistake
at the end of for statement. The semicolon will be considered as a null statement and the program
causes a time delay.
Ans:
An early exiting from a loop can be accomplished by using the break statement or the goto
statement. Below figure illustrate this:
Fig: Jumping within and Exiting from the loops with goto statement.
Since a goto statement can transfer the control to any place in a program, it is useful to provide
branching within a loop. Another important use of goto is to exit from deeply nested loops when
an error occurs. A simple break statement would not work here.
50. What are the purposes of break and continue statements? Explain with example.
Or
Why do we use the continue statement in a loop?
Ans:
Purpose of continue statement:
The continue statement causes the loop to be continued with the next iteration after skipping any
statements in between. The continue statements tell the compiler “skip the following statements
and continue with the next iteration”. The format of the continue statement is simply:
continue;
In while and do-while loops, continue causes the control to go directly to the test condition and
then to continue the iteration process.