Theoretical Questions
1.
Write down the
features of C programming language.
a) Simple: C is a simple language in the sense that it
provides a structured
approach (to break the problem into parts), the rich set of library functions, data types, etc.
b) Machine
Independent or Portable: Unlike assembly
language, c programs can
be executed on different machines with some machine
specific changes. Therefore, C is a machine independent language.
c) Mid-level
programming language: Although, C is intended to do low-level programming.
It is used to develop system applications such as kernel, driver, etc. It also supports the features of a high-level
language. That is why it is known as mid-level language.
d) structured
programming language: C is a structured
programming language in the sense that we can break the program into parts using
functions. So, it is easy to understand and modify.
Functions also provide code reusability.
e) Rich
Library: C provides a lot of inbuilt functions that make the development fast.
f) Memory
Management: It supports the feature
of dynamic memory
allocation. In C language, we can free the allocated memory at
any time by calling the free() function.
g) Fast
Speed: The compilation and
execution time of C language is fast since there are lesser inbuilt functions
and hence the lesser overhead.
h) Pointers: C provides the feature of pointers. We can
directly interact with the memory by using the pointers. We can use pointers for memory, structures,
functions, array, etc.
i) Recursion: In C, we can call the function within the function.
It provides code reusability for every function. Recursion enables us to use
the approach of backtracking.
j) Extensible: C language is extensible because it can easily adopt new features.
2.
List out the
advantages and disadvantages of using C programming language.
Advantages
of C Programming Language:
Ø C is a highly portable language. This means that C programs
written for one computer can easily run on another computer without any change
or by doing a little change.
Ø C Language code or syntax is very easy to understand. It uses a
keyword like if, else, switch, main, etc. This kind of keyword we all are using
in our day to day life to get some decisions.
Ø C compiler is very fast compared to other language compilers. C
compiler can compile around 1000 lines of code in a second. It is more
efficient than unstructured code because of minimized branching of the code.
Maintenance the C code is easy due to the readability of the code.
Ø Adding new feature is easier and faster.
Ø Program code is secured.
3.
Explain the
character set used in c program.
Ø A character set in C programming language is set of all
valid characters that can be used to form words, numbers and expressions in
source programs. The source character set consist of the characters used for
the source program text, while the execution character set is the set of
character used during the execution of the program.
The different character set used in C program are:
Ø Alphabets: C language supports all the alphabets from
the English language. Lower and upper case letters together supports 52
alphabets.
Lower case letters- a to z
Upper case letters- A to Z
Ø Digits: c language supports 10 digits which are used to
construct numerical values in C language.
Digits- 0 to 9
Ø Special Symbols: C language supports a rich set of
special symbols that include symbols to perform mathematical operations, to
check conditions, white spaces, backspaces and other special symbols.
Special Symbols
- ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? .
> , < \ | tab newline space NULL bell backspace vertical
tab etc.,
4.
What is the
difference between a keyword and an indentifier ?
The difference between keyword and identifier are as
given below:
Keword ·
Keywords are
the reserved words of a given language. ·
It specify the
kind/type of entity. ·
It consider
only letters. ·
Uses only
lowercase. ·
No special
symbol, punctuation is used. ·
Keywords are
not further classified. ·
It always
starts with a lower case letter. ·
Ex:int,char,if,while,do,class,
etc |
Identifier ·
Identifiers are
the user defined names of variable, function and labels. ·
Identify the
name of particular entity. ·
It consider
letters, underscore, digits. ·
Lower and upper
case both are allowed. ·
No punctuation
of special symbol except underscore is used. ·
Identifiers are
classified into external name and internal name. ·
First character
can be uppercase, lowercase letter or underscore. ·
Ex: Test,
count1, high_speed,etc. |
5. Define
identifier, keyword, constant and variable.
Identifier:
the name you provide to an entity in a program so that it can be uniquely
identified is called “identifier”. The names of variables, functions, labels of
a class and various other user defined entities are identifiers. The
identifiers can never be used as a keyword.
Keyword:
the words which are reserved by C are called keywords. These keywords cannot be
used to name an identifier and to name other entity of the program. Each
keyword has a distinct meaning and is used by a compiler to perform a specific
action.
Constant:
C constant is the most fundamental and essential part of the C programming
language. Constants in C are the fixed values that are used in a program, and
its value remains the same during the entire execution of the program.
Variable:
A variable is a name given to a storage area that our programs can manipulate.
Each variable in C has a specific type, which determines the size and layout of
the variables memory.
6. “All
data types are keywords but all keywords are not data types”, Justify this
statement.
7. Describe
different data types which are used in C programming.
Ø A data type in C programming is a set of values and is
determined to act on those values. C provides various types of data-types which
allow the programmer to select the appropriate type for the variable to set its
value.
The
different data types used in C are:
Ø Primary data types: Primary
data types are those data types which are already defined in the programming
languages also called primitive or in-built data types. These are the different
types of primary data types.
·
Void: it holds no value and is
generally used for specifying the type of function or what it returns. If the
function has a void type, it means that the function will not return any value.
·
Int: used to denote an integer
type. It is of 2 bytes in size.
·
Char: used to denote a character
type. It is of 1 bytes in size.
·
Float,double: used to denote a
floating point type. Float is of 4 bytes in size, whereas double is of 8 byte.
·
Int*,float*,char*: used to denote a
pointer type
Ø Derived data types: derived data types are primary data types but a little
twisted or grouped together like array, structure, union and pointer.
·
Array: an array is a collection of
data items, all of the same type, accessed using a common name.
·
Pointer: a pointer is a variable
whose value is the address of another variable, i.e. direct address of the
memory location.
Ø User defined data types:
derived data types are primary data types but a
little twisted or grouped together like array, structure, union and pointer.
·
Structure: a structure in C is a
collection of items of different types. You can think of a structure as a
record is in pascal or a class in java without methods.
Union: these
allow storing of various data types in the same memory location. Programmers
can define a union with different members, but only a single member can contain
a value at a given time.
8. What
are constants in C ? How many types of cnstants are allowed in C
Ø C constant is the most fundamental and essential part of
the C programming language. Constants in C are the fixed values that are used
in a program, and its value remains the same during the entire execution of the
program. The different types of constants allowed in C are:
9. What is
the difference between ‘x’ and “x” in C ?
·
The literal 'x'
is a char
. The literal "x"
is a string literal of
type const
char[2]
, a null-terminated
char array holding values x
and \0
.
10. What is
escape sequence ? Explain any four of them.
·
An escape sequence in C language is
a sequence of characters that doesn’t represent itself when used inside literal
or character. The different types of escape sequence available in C are:
·
\n : for new line
·
\t : for tab(horizontal)
·
\v : for vertical tab
·
\” : for double quote
11. What is
an operator ? Explain different types of operators that are included in C.
Ø An operator is a symbol that tells the compiler to
perform a certain mathematical or logical manipulation. Operators are used in
programs to manipulate data and variables.
C
operators can be classified into following types:
v Arithmetic operators: C supports all the basic
arithmetic operators.
Operator |
Description |
+ |
Adds two operands |
- |
Subtract second operands from the
first |
* |
Multiply two operand |
/ |
Divide numerator by denominator |
% |
Remainder of division |
v Relational operators: the following tables show relation
operators supported by C.
Operator |
Description |
== |
Check if two operand are equal |
!= |
Check if two operand are not
equal |
> |
Check if operand on the left is
greater than operand on the right |
< |
Check operand on the left is
smaller than right operand |
>= |
Check left operand is greater or
equal to right operand |
<= |
Check if operand on left is
smaller than or equal to right operand |
v Logical operators: these are logical operators supported
in C.
Operator |
Description |
Example |
&& |
Logical AND |
(a&&b) is false |
|| |
Logical OR |
(a||b) is true |
! |
Logical NOT |
(!a) is false |
v Bitwise operators: bitwise operator perform
manipulations of data at bit level. These operators perform shifting of bits
from right to left.
Operator |
Description |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise exclusive OR |
<< |
Left shift |
>> |
Right shift |
v Assignment operators: Assignment operators supported by
C language are as follows:
Operator |
Description |
Example |
= |
assigns values from right side operands
to left side operand |
a=b |
+= |
adds right operand to the left operand
and assign the result to left |
a+=b is same as a=a+b |
-= |
subtracts right operand from the left
operand and assign the result to left operand |
a-=b is same as a=a-b |
*= |
mutiply left operand with the right
operand and assign the result to left operand |
a*=b is same as a=a*b |
/= |
divides left operand with the right
operand and assign the result to left operand |
a/=b is same as a=a/b |
%= |
calculate modulus using two operands and
assign the result to left operand |
a%=b is same as a=a%b |
v Conditional operators: conditional operators return one
value if condition is true and returns another value if condition is false.
This operator is also called as ternary operator.
Syntax: (condition?true_value:false_value);
Example: (a>100?0:1);
In the given example, if a is greater than 100, 0 is
returned else 1 is returned. This is equal to if else conditional statements.
12. Describe
the limitations of using getchar () and putchar () functions for reading
strings.
End
1. What is
the different control structures used in C programming? Explain briefly
Ø The statement that is used to control the flow of
execution in a program is called control structure. It combines instruction
into logical unit. The different control structure are given below:
1.
Conditional control/decision making: the conditional structure test a conditioin
and allow to execute some statements on the basis of results of the test(i.e
either true or false). The different conditional structure are:
a)
If statement: The if-else statement is used to
carry out a logical test and then take one of two possible actions depending on
the outcome of the test (ie, whether the outcome is true or false).
Syntax:
If (condition)
{
Statement;
}
b)
If-else statement: The if-else statement is used to perform two
operations for a single condition. The if-else statement is an extension to the
if statement using which, we can perform two different operations, i.e., one is
for the correctness of that condition, and the other is for the incorrectness
of the condition.
Syntax:
If(condition)
{
Statement1;
}
Else
{
Statement2;
}
c)
If
else-if ladder Statement: The if-else-if ladder statement is an extension to the
if-else statement. It is used in the scenario where there are multiple cases to
be performed for different conditions. In if-else-if ladder statement, if a
condition is true then the statements defined in the if block will be executed,
otherwise if some other condition is true then the statements defined in the
else-if block will be executed, at the last if none of the condition is true
then the statements defined in the else block will be executed.
Syntax;
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
d)
Switch statement: When there are numbers of options available and one of
them is to be selected on the basis of some criteria , switch statement is
used.
Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
2.
Repetition or Loop Control Instruction: In this
structure the statements are executed more than one time. It is also known as
iteration or loop e.g while loop, for loop do-while loops etc.
a.
while loop: this is used to execute a set of statement repeatedly as
long as the specified condition is true.
Syntax:
While(logical statement)
{
Statement;
….
}
b.
do while loop: The do while loop is a post tested loop. Using
the do-while loop, we can repeat the execution of several parts of the
statements. The do-while loop is mainly used in the case where we need to
execute the loop at least once.
Syntax:
do{
//code to be executed
}while(condition);
c. For loop: The for loop in C language is
used to iterate the statements or a part of the program several times. It is
frequently used to traverse the data structures like the array and linked list.
Syntax:
for(Expression 1; Expression 2; Expression 3){
//code to be executed
}
d. Nested for loop: The nested for loop means any type of loop which is defined
inside the 'for' loop.
Syntax:
for (initialization; condition; update)
{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
e.
Nested while loop: The nested while loop means any type of loop which is
defined inside the 'while' loop.
Syntax:
while(condition)
{
while(condition)
{
// inner loop statements.
}
// outer loop statements.
}
2. Describe
‘Sequence’, ‘Selection’ and ‘Loop’ with a flowchart. Write a program to check
if a number is odd or even.
Ø Sequence: Statements are executed in a specified order.
No statement is skipped and no statement is executed more than once.
Ø Selection: It selects a
statement to execute on the basis of condition. Statement is executed when the
condition is true and ignored when it is false e.g if, if else, switch
structures.
Ø Loop: In this structure the statements are executed
more than one time. It is also known as iteration or loop e.g while loop, for
loop do-while loops etc.
/* program to check a number is odd or even
#include<stdio.h>
Int main()
{
Int num;
Printf(‘enter an integer:”);
Scanf(“%d”, &num);
If(num%2==0)
Printf(“%d is even”,num);
Else
Printf(“%d is odd”,num);
Return 0;
}
3. Explain
different types of conditional structures with syntax and example.
Ø The
different types of conditional structures are as given below:
a)
If statement: The if-else statement is used to
carry out a logical test and then take one of two possible actions depending on
the outcome of the test (ie, whether the outcome is true or false).
Syntax:
If (condition)
{
Statement;
}
Ex: /* program to find
greater number among two numbers
#include
<stdio.h>
int main()
{
int num1, num2;
printf("Enter two integers :");
scanf("%d%d", &num1, &num2);
if (num1 >
num2)
{
printf("Largest number is %d.", num1);
}
else
{
printf("Largest number is %d.", num2);
}
return 0;
}
b)
If-else statement: The if-else statement is used to perform two
operations for a single condition. The if-else statement is an extension to the
if statement using which, we can perform two different operations, i.e., one is
for the correctness of that condition, and the other is for the incorrectness
of the condition.
Syntax:
If(condition)
{
Statement1;
}
Else
{
Statement2;
}
Ex: /* program to check a number is odd or even
#include<stdio.h>
Int main()
{
Int num;
Printf(‘enter an integer:”);
Scanf(“%d”, &num);
If(num%2==0)
Printf(“%d is even”,num);
Else
Printf(“%d is odd”,num);
Return
0;
}
c)
If
else-if ladder Statement: The if-else-if ladder statement is an extension to the
if-else statement. It is used in the scenario where there are multiple cases to
be performed for different conditions. In if-else-if ladder statement, if a
condition is true then the statements defined in the if block will be executed,
otherwise if some other condition is true then the statements defined in the
else-if block will be executed, at the last if none of the condition is true
then the statements defined in the else block will be executed.
Syntax;
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Ex: // program to
illustrate if else ladder
#include
<stdio.h>
int main()
{
int i = 25;
if (i >= 0 && i <= 10)
printf("i is between 0
and 10");
else if (i >= 11 && i <=
15)
printf("i is between 11
and 15");
else if (i >= 16 && i <=
20)
printf("i is between 16
and 20");
else
printf("i is greater
than 20");
return 0;
}
d)
Switch statement: When there are numbers of options available and one of
them is to be selected on the basis of some criteria , switch statement is
used.
Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Ex: // program to illustrate switch statement
#include <stdio.h>
int main () {
char grade =
'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is
%c\n", grade );
return 0;
}
4. What is
looping? Differentiate between while and do-while loop.
Ø A loop in a computer program is an instruction
that repeats until a specified condition is reached. The difference between
while and do-while loop are :
While loop |
For loop |
Do-while loop |
·
n 'while' loop the controlling condition
appears at the start of the loop. |
·
The 'for' loop used only when we already
knew the number of iterations. |
·
In 'do-while' loop the controlling
condition appears at the end of the loop |
·
The iterations do not occur if, the
condition at the first iteration, appears false. |
·
The for loop is used only when we already
knew the number of iterations. |
·
The iteration occurs at least once even if
the condition is false at the first iteration. |
·
Syntax: while ( condition) { |
·
Syntax: for(initialization; condition; iteration){ |
·
Syntax: do{ |
5. Compare
“while”, “do-while” and “for” loops with an example.
v For difference refer above answer
Example of while loop
#include<stdio.h>
Int main()
{
Int count=1;
While(count<=4)
{
Printf(“%d”,count);
Count++;
}
Return
0;
}
Example of do while loop
#include <stdio.h>
int main () {
int a = 10;
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
Example of for loop
#include <stdio.h>
int main ()
int a;
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}
return 0;
}
6. What are
the differences between break and continue statement?
Ø The differences are as given below:
Break statement |
Continue statement |
·
It terminates the execution of remaining
iteration of the loop. |
·
It terminates only the current iteration of
the loop. |
·
'break' resumes the control of the program
to the end of loop enclosing that 'break'. |
·
'continue' resumes the control of the
program to the next iteration of that loop enclosing 'continue'. |
·
'break' stops the continuation of loop. |
·
'continue' do not stops the continuation of
loop, it only stops the current iteration. |
·
It causes early termination of loop. |
·
It causes early execution of the next iteration. |
7. Discuss
nested loop and an infinite loop with an example.
Ø An infinite loop is a looping construct
that does not terminate the loop and executes the loop forever. It is also
called an indefinite loop
or an endless loop.
It either produces a continuous output or no output.
Ex:
#include<stdio.h>
Int
main()
{
Int
var=6;
While(var>=5)
{
Printf(“%d”,var);
Var++;
Return
0;
}
Ø Nested for loop: The nested for loop means any type of loop which is defined
inside the 'for' loop.
Ex:
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n :");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=10;j++)
{
printf("%d\t",(i*j)); .
}
printf("\n");
}
Ø Nested while loop: The nested
while loop means any type of loop which is defined inside the 'while' loop.
Ex:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1,j;
while(i<=10){
j=1;
while(j<=10){
printf("%d",j);
j++;
}
printf("\n");
i++;
}
getch();
return 0;
}
8. What is
switch case statement? Explain with an example.
Ø When there are numbers of options available and one of
them is to be selected on the basis of some criteria , switch statement is
used.
Ex.:
#include<stdio.h>
#include<conio.h>
Void main()
{
Int choice;
Printf(“\n select 1 for file, 2 for edit, 3 for save”);
Printf(“\n1==> file\n 2==> edit\n 3==> save”);
Scanf(“%d”,&choice);
Switch(choice)
{
Case 1:
Printf(“\n you have choosen file menu”);
Break;
Case 2:
Printf(“\n you have choosen edit menu”);
Break;
Case 3:
Printf(“\n you have choosen save menu”);
Break;
Printf(“invalid choice”);
}
Getch();
}
No comments:
Post a Comment