ADVANCED C PROGRAMMING
UNIT1: INTRODUCTION
|
Describe
the c language history in short.
|
||||||||||||||||||||||||
|
1.
C language is a structure
oriented programming language, was developed at Bell Laboratories in 1972 by
Dennis Ritchie
2.
C language features were
derived from earlier language called “B” (Basic Combined Programming Language
– BCPL)
3.
C language was invented for
implementing UNIX operating system
4.
In 1978, Dennis Ritchie and
Brian Kernighan published the first edition
“The C Programming Language” and commonly known as K&R C
5.
In 1983, the American National
Standards Institute (ANSI) established a committee to provide a modern,
comprehensive definition of C. The resulting definition, the ANSI standard,
or “ANSI C”, was completed late 1988.
|
||||||||||||||||||||||||
|
Why
C language is called middle-level language?
|
||||||||||||||||||||||||
|
It is not as difficult like
assembly level language. It combines features of high level language and
functionality of assembly language. It is well suited for writing both
application software and system software.
|
||||||||||||||||||||||||
|
List
the characteristic features of C language.
|
||||||||||||||||||||||||
|
1.
It is highly structured
language.
2.
It uses the features of both
high level language and assembly language.
3.
C is machine independent
language and it is highly portable.
4.
It supports a variety of data
types and a powerful set of operators.
5.
It supports dynamic memory
management by using concept of pointers.
6.
It is the middle level
language.
|
||||||||||||||||||||||||
|
What
are the uses of C language?
|
||||||||||||||||||||||||
|
C language is used for developing
system applications that forms major portion of operating systems such as
Windows, UNIX and Linux. Below are some examples of C being used.
1.
Database systems
2.
Graphics packages
3.
Word processors
4.
Spread sheets
5.
Operating system development
6.
Compilers and Assemblers
7.
Network drivers
8.
Interpreters
|
||||||||||||||||||||||||
|
What
are the C standards?
|
||||||||||||||||||||||||
|
1.
C89/C90
standard – First standardized specification for C
language was developed by American National Standards Institute in 1989. C89
and C90 standards refer to the same programming language.
2.
C99
standard – Next revision was published in 1999 that
introduced new futures like advanced data types and other changes.
|
||||||||||||||||||||||||
|
What
is the basic structure of C program?
|
||||||||||||||||||||||||
|
Structure of C program is defined
by set of rules called protocol, to be followed by programmer while writing C
program. All C programs are having sections/parts which are mentioned below.
1.
Documentation section
2.
Link Section
3.
Definition Section
4.
Global declaration section
5.
Function prototype declaration
section
6.
Main function
7.
User defined function
definition section
|
||||||||||||||||||||||||
|
Why
header files are included in ‘C’ programming?
|
||||||||||||||||||||||||
|
This section is used to include the
function definitions used in the program.
Each header file has ‘h’ extension
and include using ’# include’ directive at the beginning of a program.
|
||||||||||||||||||||||||
|
What
are the rules for writing program in C?
|
||||||||||||||||||||||||
|
1.
All statements should be
written in lower case letters. Uppercase only for symbolic constants. Eg. PI
2.
Blank spaces should be
inserted between the words to improve readability.
3.
It is not necessary to fix the
position of statement in the program.
4.
The opening and closing braces
should be balanced.
|
||||||||||||||||||||||||
|
List
out some of the rules used for ‘C’ programming.
|
||||||||||||||||||||||||
|
1.
All statements should be
written in lower case letters. Upper case letters are only for symbolic
constants.
2.
Blank spaces may be inserted
between the words. This improves the readability of statements.
3.
It is a free-form language; we
can write statements anywhere between ‘{‘ and ‘}’. a = b + c;
d
= b*c;
(or)
a
= b+c; d = b*c;
Opening
and closing braces should be balanced.
|
||||||||||||||||||||||||
|
List
the various sections of C program.
|
||||||||||||||||||||||||
|
1.
Include header file section
2.
Global Declaration Section
3.
Function main
4.
Declaration part
5.
Executable part
6.
User-defined functions
|
||||||||||||||||||||||||
|
Write
the description for each section of a C program.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
Write
short notes about main ( ) function in ’C’ program.
|
||||||||||||||||||||||||
|
1.
Every C program must have main
( ) function.
2.
All functions in C, has to end
with ‘( )’ parenthesis.
3.
It is a starting point of all
‘C’ programs.
4.
The program execution starts
from the opening brace ‘{‘ and ends with closing brace
5.
‘}’, within which executable
part of the program exists.
|
||||||||||||||||||||||||
|
What
do you mean by variables in ‘C’?
|
||||||||||||||||||||||||
|
1.
A variable is a data name used
for storing a data value.
2.
Can be assigned different
values at different times during program execution.
3.
Can be chosen by programmer in
a meaningful way so as to reflect its function in the program.
4.
Some examples are: Sum
percent_1 class_total
|
||||||||||||||||||||||||
|
Why
we don’t use the symbol ‘&’ symbol, while reading a String through
scanf()?
|
||||||||||||||||||||||||
|
The ‘&’ is not used in scanf()
while reading string, because the character variable itself specifies as a
base address.
Example: name, &name[0] both
the declarations are same.
|
||||||||||||||||||||||||
|
What
are the c tokens?
|
||||||||||||||||||||||||
|
C tokens can be classified as
follows:
1.
Keywords
2.
Identifiers
3.
Constants
4.
Strings
5.
Special Symbols
6.
Operators
|
||||||||||||||||||||||||
|
What
is the difference between ‘a’ and “a”?
|
||||||||||||||||||||||||
|
‘a’ is a character constant and “a”
is a string.
|
||||||||||||||||||||||||
|
What
are the different data types available in advanced C?
|
||||||||||||||||||||||||
|
There are
four data types in C language. They are,
|
||||||||||||||||||||||||
|
Define
with example integer and floating type of data in C language.
|
||||||||||||||||||||||||
|
Integer
data:
1.
Integers are whole numbers
with a range of values supported by a particular machine. Integers occupy a
one word of storage and word size varies from 16 to 32 bits.
2.
It is defined by a keyword
called int. Short integer occupies 2 bytes of memory, Long integer occupies 4
bytes of memory, Signed integer occupies 2 bytes of memory, and unsigned
integer occupies 2 bytes of memory. Example: int a, long int c=10.
Floating
data:
1.
Floating point or real numbers
are stored in 32 bits with 6 digits precision.
2.
It is defined by a keyword
called float. It occupies four bytes of memory. Example: float a.
|
||||||||||||||||||||||||
|
How many bytes are occupied by the
int, char, float, long int and double?
|
||||||||||||||||||||||||
|
int - 2 Bytes
char - 1 Byte float - 4 Bytes long int
- 4 Bytes double - 8 Bytes
|
||||||||||||||||||||||||
|
What
are the modifiers available in C language?
|
||||||||||||||||||||||||
|
There are 5 modifiers available in
C language. They are,
1.
short
2.
long
3.
signed
4.
unsigned
5.
long long
|
||||||||||||||||||||||||
|
What
is type casting?
|
||||||||||||||||||||||||
|
Type casting is the process of
converting the value of an expression to a particular data type.
Example:
int x,y;
c = (float) x/y; where a and y are
defined as integers. Then the result of x/y is converted into float.
|
||||||||||||||||||||||||
|
Categorize
the operators used in c.
|
||||||||||||||||||||||||
|
1.
Arithmetic operators
2.
Relational operators
3.
Logical operators
4.
Assignment operators
5.
Increment and decrement
operators
6.
Conditional operators
7.
Bitwise operators
8.
Special operators
|
||||||||||||||||||||||||
|
What
is an Operator and Operand?
|
||||||||||||||||||||||||
|
An operator is a symbol that
specifies an operation to be performed on operands.
Example: *, +, -, / are called
arithmetic operators.
The data items that operators act
upon are called operands.
Example: a+b; In this statement a
and b are called operands.
|
||||||||||||||||||||||||
|
What
is Ternary operators or Conditional operators?
|
||||||||||||||||||||||||
|
Ternary operators is a conditional
operator with symbols ? and :
Syntax: variable = exp1 ? exp2 :
exp3
If the exp1 is true variable takes
value of exp2. If the exp2 is false, variable takes the value of exp3.
|
||||||||||||||||||||||||
|
What
is a Modulo Operator?
|
||||||||||||||||||||||||
|
‘%’ is modulo operator. It gives
the remainder of an integer division
Example:
a=17, b=6. Then c=%b gives 5.
|
||||||||||||||||||||||||
|
What
is Bit wise operation in C?
|
||||||||||||||||||||||||
|
Bitwise operators are special C
operators that help to manipulate data at bit level. C supports various
bitwise operators for performing bitwise operations. Some of the typical
bitwise operations are
1.
Bitwise AND &
2.
Bitwise OR |
3.
Bitwise XOR ^
4.
Shift Left <<
5.
Shift Right >>
|
||||||||||||||||||||||||
|
What
are the logical operators available in „C‟?
|
||||||||||||||||||||||||
|
The logical operators available in
‘C’ are
1.
Logical AND &&
2.
Logical OR ||
3.
Logical NOT !
|
||||||||||||||||||||||||
|
What
is the difference between Logical AND and Bitwise AND?
|
||||||||||||||||||||||||
|
Logical AND (&&): Only used
in conjunction with two expressions, to test more than
one condition. If both the
conditions are true the returns 1. If false then return 0.
Bitwise AND (&): Only used in
Bitwise manipulation. It is a unary operator.
|
||||||||||||||||||||||||
|
What
is the difference between „=‟ and „==‟
operator?
|
||||||||||||||||||||||||
|
Where = is an assignment operator
and == is a relational operator. Example: while (i=5)
is an infinite loop because it is a
non zero value and while (i==5) is true only when i=5.
|
||||||||||||||||||||||||
|
What
is the use of sizeof( ) operator?
|
||||||||||||||||||||||||
|
The sizeof ( ) operator gives the
bytes occupied by a variable.
No of bytes occupied varies from
variable to variable depending upon its data types.
Example:
int x,y;
printf(“%d”,sizeof(x));
Output:
2
|
||||||||||||||||||||||||
|
Distinguish Increment and Decrement operators.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
What
is the difference between ++a and a++?
|
||||||||||||||||||||||||
|
++a means do the increment before
the operation (pre increment) a++ means do the increment after the operation
(post increment) Example:
a=5;
x=a++; /* assign x=5*/
y=a; /*now y assigns y=6*/
x=++a; /*assigns x=7*/
|
||||||||||||||||||||||||
|
What
is a loop control statement?
|
||||||||||||||||||||||||
|
Many tasks done with the help of a
computer are repetitive in nature. Such tasks can be done with loop control
statements.
|
||||||||||||||||||||||||
|
What
is the difference between if and while statement?
|
||||||||||||||||||||||||
|
If statement:
1.
It is a conditional statement
2.
If the condition is true, it
executes some statements.
3.
If the condition is false then
it stops the execution the statements.
While statement:
1.
It is a loop control statement
2.
Executes the statements within
the while block if the condition is true.
3.
If the condition is false the
control is transferred to the next statement of the loop.
|
||||||||||||||||||||||||
|
Differentiate
between do-while and while-do (or) while loop.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
What
is the difference between while loop and do…while loop?
|
||||||||||||||||||||||||
|
In the while loop the condition is
first executed. If the condition is true then it executes the body of the
loop. When the condition is false it comes of the loop. In the do…while loop
first the statement is executed and then the condition is checked. The
do…while loop will execute at least one time even though the condition is
false at the very first time.
|
||||||||||||||||||||||||
|
Give
the syntax for the ‘for’ loop statement
|
||||||||||||||||||||||||
|
for (Initialize counter; Test
condition; Increment / Decrement)
{
statements;
}
Initialization counter sets the
loop to an initial value. This statement is executed only once.
The test condition is a relational
expression that determines the number of iterations desired or it determines
when to exit from the loop. The ‘for’ loop continues to execute as long as
conditional test is satisfied. When condition becomes false, the control of
program exists the body of the ‘for’ loop and executes next statement after
the body of the loop.
The increment / decrement parameter
decides how to make changes in the loop.
The body of the loop may contain
either a single statement or multiple statements.
|
||||||||||||||||||||||||
|
Compare
switch( ) and nestedif statement.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
Differentiate break and continue statement
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
Write
rules for defining real constants.
|
||||||||||||||||||||||||
|
1.
It must have at least one
digit.
2.
It must have a decimal point.
3.
It should not have blank
spaces and comma inside.
4.
Negative values should be
written by an explicit minus (-) sign. If no sign is mentioned by default it
is assumed to be positive.
|
||||||||||||||||||||||||
|
Differentiate
between signed and unsigned integer.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
What
are the Escape Sequences present in „C‟.
|
||||||||||||||||||||||||
|
1.
\n - New Line
2.
\b – Backspace
3.
\t - Form feed
4.
\\ - Backspace
5.
\t – Tab
6.
\r - Carriage return
7.
\” - Double quotes
|
||||||||||||||||||||||||
|
Write
the limitations of getchar( ) and sacnf( ) functions for reading strings.
|
||||||||||||||||||||||||
|
getchar(
):
To read a single character from stdio, then getchar() is the appropriate.
scanf(
):
scanf( ) allows to read more than just a single character at a time.
|
||||||||||||||||||||||||
|
What is a global variable?
|
||||||||||||||||||||||||
|
The global variable is a variable
that is declared outside of all the functions. The global variable is stored
in memory, the default value is zero. Scope of this variable is available in
all the functions. Life as long as the program’s execution doesn’t come to an
end.
|
||||||||||||||||||||||||
|
What
are delimiters and what is the use?
|
||||||||||||||||||||||||
|
They are special symbols such as
1.
: Colon Used for label
2.
; Semi colon Terminates
statement
3.
() Parenthesis Used in
expression and function
4.
[] Square Bracket Used for
array declaration
5.
{} Curly Brace Scope of
statement
6.
# Hash Preprocessor directive
7.
, Comma Variable Separator
|
||||||||||||||||||||||||
|
Write
short notes about main ( ) function in “C” program.
|
||||||||||||||||||||||||
|
1.
Every C program must have main
( ) function.
2.
All functions in C, has to end
with ‘( )’ parenthesis.
3.
It is a starting point of all
‘C’ programs.
4.
The program execution starts
from the opening brace ‘{‘and ends with closing brace ‘}’, within which
executable part of the program exists.
|
||||||||||||||||||||||||
|
What
is the difference between auto and static storage class?
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
Write
a program to swap the values of two variables (without temporary variable).
|
||||||||||||||||||||||||
|
#include <stdio.h>
#include <conio.h>
void main( )
{
int a =5; b = 10;
clrscr( );
prinf(“Before swapping a = %d b =
%d “, a , b);
a = a + b; B = a – b;
a = a – b;
prinf(“After swapping a = %d b =
%d”, a,b);
getch( );
}
Output:
Before swapping a = 5 b = 10
After swapping a = 10 b = 5
|
||||||||||||||||||||||||
|
What
is the output of the following program when, the name given with spaces?
|
||||||||||||||||||||||||
|
main()
{
char name[50]; printf(“\n name\n”);
scanf(“%s, name); printf(“%s”,name);
}
Output:
SANTHOSH(It only accepts the data
upto the spaces)
|
||||||||||||||||||||||||
|
What
is the output of the programs given below?
|
||||||||||||||||||||||||
|
main() main()
{ {
float a; float a;
int x=6, y=4; int x=6, y=4;
a=x\y; a=(float) x\y;
printf(“Value of a=%f”, a);
printf(“Value of a=%f”,a);
} }
Output:
Output:
1. 1.500000
|
||||||||||||||||||||||||
|
What
is a String?
|
||||||||||||||||||||||||
|
String is an array of characters.
Construct an infinite loop using
while?
while (1)
{
}
Here 1 is a non zero, value so the
condition is always true. So it is an infinite loop.
|
||||||||||||||||||||||||
|
What
is meant by Control String in Input/Output Statements?
|
||||||||||||||||||||||||
|
Control Statements contains the
format code characters, specifies the type of data that the user accessed
within the Input/Output statements.
|