Language C
Topics:
1. Introduction
and History
A)
Invention and Publication
1.
Year and Lab
B) Developer
2. Types,
Operator and Expressions
A) Data
Type and Sizes
int, float, double, char and long
B) Operators
Arithmetic, relational, Logical operators,
bitwise operator, Assignment Operator
C) Expression
Conditional
expressions, Precedence
3. Control
Flow
A) Control
Statements(IF and Else)
B) Loops(while,
do-while, for)
C) Jump
Statement(break, continue, exit )
4. Function
and Program Structure
A) Basics
of Function
B) Types
of Variables
External,
scope, header and register
C) Recursion
5. Pointers
and Array
A) Pointers
1. Pointers
and Address
2. Pointers
and Array
3. Character
Pointers
B) Array
1. Introduction
2. Types
of Array
6. Structures
A) Basics
of Structure
B) Array
of Structure
C) Pointers
of Structure
D) Structure
of Functions
7. Input
and Output
A) Standard
Input and Output
B) Formatted
Output
C) Formatted
Input
D) Files
Access
1.
Introduction
and History
Q.1
In which year C Language published?
Answer:
1978
Q.2
C Language was developed for which OS?
Answer:
UNIX
Q.3
In which Lab C Language is developed?
Answer:
Bell Labs
Q.4
C Language was developed by?
Answer:
Dennis Ritchie
Q.5
C Language was developed in which machine?
Answer:
PDP-11 at B Language
Q.6
Why C language was developed?
Answer:
Because at that time B language wasn’t powerful to full-fill UNIX requirement.
Q.7
What is C Language?
Answer: The C programming language is a standardized
programming language developed in the early 1970s by Ken Thompson and Dennis
Ritchie for use on the UNIX operating system. It has since spread to many other
operating systems, and is one of the most widely used programming languages. C
is prized for its efficiency, and is the most popular programming language for
writing system software, though it is also used for writing applications.
2.
Types operator and Expressions
Q.1
Name the Data type in C Language?
Answer:
INT, FLOAT, CHAR, DOUBLE and LONG
Q.2
Name the type of operator in C language?
Answer:
arithmetic, logical, relational, increment and decrement operator and bitwise
operator.
Q.3 Can a variable be constant and volatile?
Answer Yes. The const
modifier means that this code cannot change the value of the variable, but that
does not mean that the value cannot be changed by means outside this code. For
instance, in the example in FAQ 8, the timer structure was accessed through a
volatile const pointer.
The function itself did not change the value of the timer, so it
was declared const. However, the value was changed by hardware on the computer,
so it was declared volatile. If a variable is both const and volatile, the two
modifiers can appear in either order.
Q.4 what is type conversions?
Answer: when an operator has operands of different types, they are
converted to a common type according to a small number of rules.
Q.5 Name some bitwise operator?
Answer: &,|,<<,>>,^,~
Q.6 Name some assignment operator?
Answer: +=,*=,/=,%=,-=
Q.7 Name the operator use for conditional expressions?
Answer: ?:
Answer: ?:
Q.8 Precedence of operator
in C?
Answer: higher to lower brackets,
Increment operator,
arithmetic operator, relational operator,
bitwise operator, logical operator, conditional operator, assignment
operator
Q.9 Name escape sequence in C?
Answer: \a, \b, \f, \n, \r, \t, \v, \\, \?, \”, \’ \ooo, \xhh
Q.10 Which bit wise operator is suitable
for turning off a particular bit in a number ?
Answer: The bitwise AND operator, again. In the following code snippet,
the bit number 24 is reset to zero.
some_int = some_int & ~KBit24;
Q.11 Which bit wise operator is suitable for putting on a
particular bit in a number ?
Answer: The bitwise OR operator. In the following code snippet, the bit
number 24 is turned ON:
some_int = some_int | KBit24;
Q.12 Difference between const char* p and char const* p
Answer: In const char* p, the character pointed by ‘p’ is constant, so u
cant change the value of character pointed by p but u can make ‘p’ refer to
some other location.
in char const* p, the ptr ‘p’ is constant not the character
referenced by it, so u cant make ‘p’ to reference to any other location but u
can change the value of the char pointed by ‘p’.
3 Control
Flow Statements
Q.1
If and else statement are used basically for?
Answer:
They are basically used to give direction to your program by checking
expressions.
Q.2
What is the main purpose of switch statement?
Answer:
It is the multi-way decisions tests.
Q.3
Name the way to do looping in C?
Answer:
While,For,do While
Q.4
what is the purpose of the break statement?
Answer:
It only exists from the innermost loop.
Q.5
What is the purpose of the continue?
Answer:
it causes the next iteration of the enclosing for, while, or do-while.
Q.6
what is the purpose of the return ?
Answer:
it is actually returns the value of the method or function.
Q.7
What is the purpose of exit statement?
Answer:
It is actually used to end the program.
Q.8
For the successful run what should programs’ main method return?
Answer:
0
Functions
Q.1 Advantages macro over function?
Answer: Macro gets to see the Compilation
environment, so it can expand __ __TIME__ __FILE__ #defines. It is expanded by
the preprocessor.
For example, you can’t do this without macros
#define PRINT(EXPR) printf( #EXPR “=%d\n”, EXPR)
PRINT( 5+6*7 ) // expands into printf(”5+6*7=%d”, 5+6*7 );
You can define your mini language with macros:
#define strequal(A,B) (!strcmp(A,B))
Macros are a necessary evils of life. The purists don’t like them, but without it no real work gets done.
For example, you can’t do this without macros
#define PRINT(EXPR) printf( #EXPR “=%d\n”, EXPR)
PRINT( 5+6*7 ) // expands into printf(”5+6*7=%d”, 5+6*7 );
You can define your mini language with macros:
#define strequal(A,B) (!strcmp(A,B))
Macros are a necessary evils of life. The purists don’t like them, but without it no real work gets done.
Q.2 Does there exist any other function which can
be used to convert an integer or a float to a string ?
Answer: Some implementations provide a nonstandard function called itoa(),
which converts an integer to string.
Q.3 What do you mean by recursion?
Answer: when function calls itself known as recursion.
Q.4 What are the different storage class in
C?
Answer: There are four types of storage
classes.
- Automatic
- Extern
- Regiter
- Static
Q.5 What is macro?
Answer: Macros
are the identifiers that represent statements or expressions. To associate
meaningful identifiers with constants, keywords, and statements or expressions.
Q.6 Can static variable be declare in header
files?
Answer: You can’t declare a static variable
without defining it as well (this is because the storage class modifiers static
and extern are mutually exclusive). A static variable can be defined in a
header file, but this would cause each source file that included the header
file to have its own private copy of the variable, which is probably not what
was intended.
Q.7 what are the purpose of main()?
Answer: The function main() invokes other functions within it.It is the
first function to be called when the program starts execution.
- It is the starting function.
- It returns an int value to the
environment that called the program.
- Recursive call is allowed for main( )
also.
- It is a user-defined function
Q.8
What are the advantages of function?
Answer:
It reduces the Complexity in a program by
reducing the code.
- Function are easily understanding and
reliability and execution is faster.
- It also reduces the Time to run a
program.In other way,Its directly proportional to Complexity.
- Its easy to find-out the errors due to
the blocks made as function definition outside the main function.
Q.9
What does static variable mean?
Answer: There are 3 main uses for the static.
1. If you declare within a function:
It retains the value between function calls
2.If it is declared for a function name:
By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files
3. Static for global variables:
By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file.
1. If you declare within a function:
It retains the value between function calls
2.If it is declared for a function name:
By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files
3. Static for global variables:
By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file.
Arrays
Q.1 what is difference b/w string and character
array?
Answer: A major difference is: string will have static storage
duration, whereas as a character array will not, unless it is explicity
specified by using the static keyword.
Actually, a string is a character array with following properties:
* the multibyte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NUL character.
* it not specified what happens if this array, i.e., string, is modified.
* Two strings of same value[1] may share same memory area. For example, in the following declarations:
char *s1 = “Calvin and Hobbes”;
char *s2 = “Calvin and Hobbes”;
the strings pointed by s1 and s2 may reside in the same memory location. But, it is not true for the following:
char ca1[] = “Calvin and Hobbes”;
char ca2[] = “Calvin and Hobbes”;
[1] The value of a string is the sequence of the values of the contained characters, in order.
Actually, a string is a character array with following properties:
* the multibyte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NUL character.
* it not specified what happens if this array, i.e., string, is modified.
* Two strings of same value[1] may share same memory area. For example, in the following declarations:
char *s1 = “Calvin and Hobbes”;
char *s2 = “Calvin and Hobbes”;
the strings pointed by s1 and s2 may reside in the same memory location. But, it is not true for the following:
char ca1[] = “Calvin and Hobbes”;
char ca2[] = “Calvin and Hobbes”;
[1] The value of a string is the sequence of the values of the contained characters, in order.
Q.2
When does the compiler not
implicitly generate the address of the first element of an array ?
Answer: Whenever an array name appears in an expression such as
·
array
as an operand of the sizeof operator
·
array
as an operand of & operator
·
array
as a string literal initializer for a character array
Then the compiler does not implicitly generate the address of the
address of the first element of an array.
Q.3 How do you print an address ?
Answer: The safest way is to use printf() (or fprintf() or
sprintf()) with the %P specification. That prints a void pointer (void*).
Different compilers might print a pointer with different formats.
Your compiler will pick a format that’s right for your
environment.
If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*:
If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*:
printf( %Pn, (void*) buffer );
Q.4 What is the difference between array and pointer?
Answer: Pointers are used to
manipulate data using the address. Pointers use • operator to access the data
pointed to by them.
Arrays is a collection of similar datatype. Array use subscripted variables to access and manipulate data. Array variables can be Equivalently written using pointer expression.
Arrays is a collection of similar datatype. Array use subscripted variables to access and manipulate data. Array variables can be Equivalently written using pointer expression.
Q.5
What are the uses of pointer?
Answer: Pointer is used in the following cases
- It is used to access array elements.
- It is used for dynamic memory
allocation.
- It is used in Call by reference.
- It is used in data structures like
trees, graph, linked list etc
Q.6
What is array?
Answer:
Array is a variable that hold multiple elements which has the same
data type
Q.7 How to declare a array?
Answer:
We can declare an array by specify its data
type, name and the number of elements the array holds between square brackets
immediately following the array name.
syntax :
data_type array_name[size];
syntax :
data_type array_name[size];
Q.8 what is the difference between
arrays and pointers?
Answer: Array is collection of
similar datatype. it is a static memory allocation means we can not increment
and decrement the arry size once we allocated. and we can not increment the
base address, reassign address.
Pointer is a dynamic memory allocation. we can allocate the size as we want, assigning into another variable and base address incrementation is allowed
Pointer is a dynamic memory allocation. we can allocate the size as we want, assigning into another variable and base address incrementation is allowed
Q.9 What is a pointer?
Answer:Pointers
are variables which stores the address of another variable. That variable may
be a scalar (including another pointer), or an aggregate (array or structure).
The pointed-to object may be part of a larger object, such as a field of a
structure or an element in an array.
Structures
Q.1 What is a structure?
Answer:Structure
constitutes a super data type which represents several different data types in
a single unit. A structure can be initialized if it is static or global.
Q.2 what is the difference between class and strucrures?
Answer: By default, the members
ot structures are public while that tor class is private.
- structures doesn’t provide something
like data hiding which is provided by the classes.
- structures contains only data while
class bind both data and member functions.
Q.3 What is self- referential structures?
Answer: A self-referential structure is one of the data structures
which refer to the pointer to (points) to another structure of the same type.
For example, a linked list is supposed to be a self-referential data structure.
The next node of a node is being pointed, which is of the same struct type.
Q.4 What is structure to pointers?
Answer: struct st employee, *stptr;
We point the pointer to employee with the expression stptr = &employee. Then access the given members by de-referencing
the pointer. For this, we have used another structure selection operator which
works on pointers to structures. If p is a pointer to a structure and m is a member of that structure, then p->m.
Q.5 what is lookup table in C?
Answer: A lookup
table is simply an initialized array that contains precalculated information.
They are typically used to avoid performing complex (and hence time consuming)
calculations. For example, it is well known that the speed of CRC calculations
may be significantly increased by use of a lookup table. A suitable lookup
table for computing the CRC used in SMBUS calculations.
Q.6 what is typedef in C?
Answer:
typedef
is a keyword in the C and The
purpose of typedef
is to assign alternative names to
existing types, most often those whose standard declaration is
cumbersome, potentially confusing, or likely to vary from one implementation to
another.
Q.7 what is union?
Answer: A union, is a collection of variables of different types,
just like a structure. However, with unions, you can only store information in
one field at any one time.
You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.
A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds one of them at a time (not like a structure). This can save memory if you have a group of data where only one of the types is used at a time. The size of a union is equal to the size of it's largest data member. In other words, the C compiler allocates just enough space for the largest member. This is because only one member can be used at a time, so the size of the largest, is the most you will need.
You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.
A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds one of them at a time (not like a structure). This can save memory if you have a group of data where only one of the types is used at a time. The size of a union is equal to the size of it's largest data member. In other words, the C compiler allocates just enough space for the largest member. This is because only one member can be used at a time, so the size of the largest, is the most you will need.
Q.8 what is c Bit Fields ?
Answer: The constant-expression specifies the width of the field in
bits. The type-specifier for the declarator must be unsigned int, signed int, or int, and the constant-expression must be a nonnegative integer value.
If the value is zero, the declaration has no declarator. Arrays of bit fields, pointers to bit
fields, and functions returning bit fields are not allowed. The optional declarator names the bit field. Bit fields can
only be declared as part of a structure. The address-of operator (&)
cannot be applied to bit-field components.
Unnamed bit fields
cannot be referenced, and their contents at run time are unpredictable. They
can be used as "dummy" fields, for alignment purposes. An unnamed bit
field whose width is specified as 0 guarantees that storage for the member
following it in the struct-declaration-list begins on an int boundary.
Input and output
Q.1
Name the standard input and standard output?
Answer:
getchar() and putchar()
Q.2
What is formatted output and formatted input?
Answer:
printf() and scanf()
Q.3
The tolower() is defined in which header files?
Answer:#include<ctype.h>
Q.4
What is the proper declaration for printf()?
Answer:
int printf(char *fmt,……)
Q.5
What is error handling library?
Answer:
Stderr and Exit
Q.6
Name the Line output and input in file?
Answer:
fgets() and fputs()
Q.7
Name the functions used for storage management?
Answer:
malloc() and calloc()
Q.8
Name the library where mathematical function are defined?
Answer:
#include<math.h>
Q.9
Name the function which create random function?
Answer:rand()
Q.10
Name the command execution method?
Answer:
system(char *s)
Q.11 What is difference b/w printf() and sprint()?
Answer: sprintf() writes data to the character array whereas
printf(...) writes data to the standard output device.
Q.12 Why does malloc(0)
return valid memory address ? What's the
use ?
Answer: malloc(0) does not return a non-NULL under every
implementation.
An implementation is free to behave in a manner it finds
suitable, if the allocation size requested is zero. The
implmentation may choose any of the following actions:
* A null pointer is returned.
* The behavior is same as if a space of non-zero size
was requested. In this case, the usage of return
value yields to undefined-behavior.
An implementation is free to behave in a manner it finds
suitable, if the allocation size requested is zero. The
implmentation may choose any of the following actions:
* A null pointer is returned.
* The behavior is same as if a space of non-zero size
was requested. In this case, the usage of return
value yields to undefined-behavior.
Q.13 what is the way of accessing file in c?
Answer:Random access files in C and the
following lesson will look at working with text files. Apart from the simplest
of applications, most programs have to read or write files. Maybe it's just for
reading a config file, or a text parser or something more sophisticated. The
basic file operations are
·
fopen - open
a file- specify how its opened (read/write) and type (binary/text)
·
fclose -
close an opened file
·
fread -
read from a file
·
fwrite -
write to a file
·
fseek/fsetpos
- move a file pointer to somewhere in a file.
·
ftell/fgetpos
- tell you where the file pointer is located.
Reference:
Ritchie Dennis M., (). The C programming Language. 2nd ed. : .
(2012). C
Interview Question. [ONLINE] Available at:
http://techpreparation.com/. [Last Accessed 29 May, 2012].
No comments:
Post a Comment