8/4/2019 C Interview Questions 2
1/53
Welcome to the
Seminar on C Interview
Questions
8/4/2019 C Interview Questions 2
2/53
How Would You Print Hello
World on Screen without using anysemi-colons in the code?
8/4/2019 C Interview Questions 2
3/53
Solution
1. Semi-Colon is the statement terminator in C.
2. Whatre the statements that dont require a semi-colon?
For Instanceif,while.
3. What does printf() return? An int value containing the
number of bytes output.
4. How to embed a printf() within an if() or a while()?
8/4/2019 C Interview Questions 2
4/53
Solution
int main()
{
if(printf(Hello World)){
}
//while(!printf(Hello World));
}
8/4/2019 C Interview Questions 2
5/53
What would be the output of:
printf(%c,5[abcdefg]);
8/4/2019 C Interview Questions 2
6/53
Solution
1. Array Subscripting is Commutative in C.
2. a[e] is identical to *((a)+(e)), for *any* two expressions
a and e, as long as one of them is a pointer expression
and one is integral.
3. Thus,the output for printf(%c,5[abcdefg]); would
be the same as printf(%c,abcdefg[5]);
4. The Output would be: the character at index 5 in the
string abcdefg i.e. f.
8/4/2019 C Interview Questions 2
7/53
How would you swap the values of
two variables without using a thirdvariable?
8/4/2019 C Interview Questions 2
8/53
Solution
1. Taking an example:
int a=5,b=2;
a=a+b; // a = 5+2=7, b = 2
b=a-b; // a = 7, b = 7 - 2=5
a=a-b; //a = 7 - 5=2, b=5
8/4/2019 C Interview Questions 2
9/53
Solution
2. Order of Operations
B = (((A + A) / 2) + (A = B) B);
The trick in this solution is that ((A + A) / 2) yields thevalue A without being stored in the variable A anymore.
So to this we add (A = B), which in turn evaluates to B.
So the total expression has become A + B. To make this
only A, we have to subtract off the extra B.
8/4/2019 C Interview Questions 2
10/53
What is sizeof('A') ?
8/4/2019 C Interview Questions 2
11/53
Solution
1. sizeof(A) is the same as sizeof(int).
2. Character constants have type int in C.
3. This is one area where C differs from C++.InC++,sizeof(A)=sizeof(char)
8/4/2019 C Interview Questions 2
12/53
What's the difference between a
header file and a library?
8/4/2019 C Interview Questions 2
13/53
Solution
1. A header file typically contains declarations and
definitions, but it never contains executable code.
(A header file arguably shouldn't even contain any
function bodies which would compile into executablecode.)
2. A library, on the other hand, contains only compiled,
executable code and data. A third-party library is oftendelivered as a library and a header file. Both pieces are
important. The header file is included during
compilation, and the library is included during linking.
8/4/2019 C Interview Questions 2
14/53
What is the difference between
#include and #include file?
8/4/2019 C Interview Questions 2
15/53
Solution
1. #include
This method of inclusion tells the preprocessor to look
for the file in the predefined default location. It is often
used to include standard headers such as stdio.h or
stdlib.h. This is because these headers are rarely
modified, and they should always be read from your
compilers standard include file directory.
8/4/2019 C Interview Questions 2
16/53
Solution
2. #include file
This method of inclusion tells the preprocessor to look
for the file in the current directory first, then look for it
in the predefined locations you have set up. The
#include file method of file inclusion is often used toinclude nonstandard header files that you have created
for use in your program. This is because these headers
are often modified in the current directory, and you will
want the preprocessor to use your newly modifiedversion of the header rather than the older, unmodified
version.
8/4/2019 C Interview Questions 2
17/53
What is the purpose of main( )
function?
8/4/2019 C Interview Questions 2
18/53
Solution
1. The function main( ) invokes other functions within it.It
is the first function to be called when the program startsexecution.
2. It returns an int value to the environment that called the
program.
3. Recursive call is allowed for main( ) also.
4. It has two arguments 1)argument count and 2) argument
vector (represents strings passed).
Any user-defined name can also be used as parameters
for main( ) instead of argc and argv
8/4/2019 C Interview Questions 2
19/53
What are command-line arguments?
8/4/2019 C Interview Questions 2
20/53
Solution
1. Command-Line arguments are the arguments passed to
main() via the command line while executing the
program.
2. int main(int argc,char *argv[])
argc specifies the number of arguments passed.
argv is an array of strings that contains the argumentspassed as strings.
8/4/2019 C Interview Questions 2
21/53
Solution
3. The first argument i.e. argv[0] always contains the EXE
name.
4. For e.g. if the program EXE is called TEST.EXE and
you execute it like:
C:\>TEST.EXE HELLO WORLD
argc=3
argv[0]=TEST.EXE, argv[1]=HELLO,argv[2]=WORLD
8/4/2019 C Interview Questions 2
22/53
How much memory space is
occupied by a pointer variable?
8/4/2019 C Interview Questions 2
23/53
Solution
1. A pointer variable is a variable that may contain the
address of another variable or any valid address in the
memory.
2. Space occupied by a int pointer is the same as that
occupied by a float pointer.
3. Memory occupied by a pointer is 16 bits.
8/4/2019 C Interview Questions 2
24/53
What is the difference between these
initializations?
char a[] = "string literal";
char *p = "string literal";
8/4/2019 C Interview Questions 2
25/53
Solution
1. A string literal can be used in two slightly different
ways. As an array initializer (as in the declaration of
char p[]), It specifies the initial values of the characters
in that array.
2. Anywhere else, it turns into an unnamed, static array of
characters, which may be stored in read-only memory,
which is why you can't safely modify it and thus theprogram may crash if you try to change a[i]s value in
the 2nd case i.e. char *p = "string literal";
8/4/2019 C Interview Questions 2
26/53
What does *p++ increment?
8/4/2019 C Interview Questions 2
27/53
Solution
1. The ++ operator has higher precedence than the value
at(*) operator.
2. Thus,*p++ increments the pointer p and not the value
pointed to by p.
3. To increment the value of what p points to , use (*p)++;
8/4/2019 C Interview Questions 2
28/53
What's wrong with...?
char a[]=hello;
if(a==hello)
{//do something }
8/4/2019 C Interview Questions 2
29/53
Solution
1. Applied to pointers, the == operator compares only
whether the pointers are equal. To compare whether the
strings are equal, you'll have to call strcmp.
2. The if condition would never be true because the value
stored in the array and the string literal its being
compared with,would be stored in different locations.
3. The correct statement would be:
if(strcmp(a,hello)==0) { }
8/4/2019 C Interview Questions 2
30/53
What is the difference between
malloc() and calloc()?
8/4/2019 C Interview Questions 2
31/53
Solution
1. Both malloc() and calloc() allocate blocks of memory
from the heap. It allows a program to allocate memoryexplicitly as it's needed, and in the exact amounts
needed.
2. The major difference between the two is that whilemalloc() does not set the value of the memory
allocated,calloc() sets the allocated memory to zeroed
bytes.
3. The minor difference is in their syntax:
malloc(10*sizeof(char));
calloc(10,sizeof(char));
8/4/2019 C Interview Questions 2
32/53
What is the difference between the
stack and the heap?
8/4/2019 C Interview Questions 2
33/53
Solution
1. The heap is where malloc(), calloc(), and realloc() get
memory. Getting memory from the heap is much slowerthan getting it from the stack. On the other hand, the
heap is much more flexible than the stack. Memory can
be allocated at any time and deallocated in any order.
Such memory isnt deallocated automatically; you have
to call free().
2. If you can keep data in a local variable (and allocate it
from the stack), your code will run faster than if you putthe data on the heap. Sometimes you can use a better
algorithm if you use the heapfaster, or more robust, or
more flexible. If memory is allocated from the heap, its
available until the program ends.
Solution
8/4/2019 C Interview Questions 2
34/53
Solution
1. The stack is where all the functions local (auto)variables are created. The stack also contains some
information used to call and return from functions.
2. The stack is very inflexible about allocating memory;
everything must be deallocated in exactly the reverse
order it was allocated in. For implementing function
calls, that is all thats needed. Allocating memory off the
stack is extremely efficient. One of the reasons Ccompilers generate such good code is their heavy use of
a simple stack.
8/4/2019 C Interview Questions 2
35/53
What are function pointers?
Solution
8/4/2019 C Interview Questions 2
36/53
Solution
1. Function Pointers are pointers, i.e. variables, which
point to the address of a function. You must keep in
mind, that a running program gets a certain space in themain-memory. Both, the executable compiled program
code and the used variables, are put inside this memory.
2. Their declaration is easy: write the declaration as itwould be for the function, say
int func(int a, float b); and simply put brackets around
the name and a * in front of it: that declares the pointer./* pointer to function returning int */
int (*func)(int a, float b);
Solution
8/4/2019 C Interview Questions 2
37/53
Solution
3. When to use a function pointer?
A very useful application is in replacing a lengthyswitch() statement,or if-else sequences.
Say you want to call a different function for displaying a
letter of the alphabet.Instead of using a switch and 26
cases,you can use an array of function pointers.
void (*fparr[])(void) = {&PrintA(), &PrintB()};
Then,index into this array to call the different functions.fparr[0] to call PrintA(), fparr[1] to call PrintB()
8/4/2019 C Interview Questions 2
38/53
What is the difference between
printf() and sprintf() ?
Solution
8/4/2019 C Interview Questions 2
39/53
Solution
1. printf prints to the standard output stream stdout,which
is normally the screen.
sprintf() prints to a string.The syntax is the same as
printf but sprintf takes one extra parameter,the string to
write to.
printf(%d,10);
char buffer[10];
sprintf(buffer,%d,10);Note: This is a very simple way to convert numbers to
strings.
8/4/2019 C Interview Questions 2
40/53
What are storage classes?
Solution
8/4/2019 C Interview Questions 2
41/53
Solution
A storage class is an attribute that changes the behavior of a
variable. It controls the lifetime, scope and linkage.
There are four types of storage classes :
1)auto 2)static 3)extern 4)register
auto : is the default storage class.The scope and lifetimeof an auto variable is limited to the block in which it is
defined.
static: The scope is limited to the block in which thevariable is declared but the lifetime is the entire
program execution.
Solution
8/4/2019 C Interview Questions 2
42/53
Solution
register : The register modifier hints to the compilerthat the variable will be heavily used and should be kept
in the CPUs registers, if possible, so that it can be
accessed faster. In fact, many compilers actually ignore
the register modifier, which is perfectly legal, because itis only a hint and not a directive.
extern : The scope of an extern variable extends across
multiple source files and the lifetime is the entireprogram execution.
8/4/2019 C Interview Questions 2
43/53
What would be the value of i afterthese statements?
int i=5;
sizeof(i++);
Solution
8/4/2019 C Interview Questions 2
44/53
Solution
1. sizeof does not evaluate the argument passed to it.
2. Thus,the value of i will not change as the i++ instruction
does not get executed.
8/4/2019 C Interview Questions 2
45/53
How would you print a % with
printf()?
8/4/2019 C Interview Questions 2
46/53
Solution
1. Why doesnt\% work?
Backslash sequences are interpreted by the compiler
(\n, \", \0, etc.), and \% is not one of the recognized
backslash sequences.
2. printf(%%); is the correct solution.
8/4/2019 C Interview Questions 2
47/53
How can %f work for type double in
printf if %lf is required in scanf?
8/4/2019 C Interview Questions 2
48/53
Solution
1. In printf, the "default argument promotions" apply, and
type float is implicitly converted to double. So printf
always receives doubles, and defines %f to be the
sequence that works whether you had passed a float or adouble.
2. scanf, on the other hand, always accepts pointers, and
the types pointer-to-float and pointer-to-double are very
different.No implicit promotions apply.
8/4/2019 C Interview Questions 2
49/53
How can you you use sizeof to
determine the number of elements inan array?
8/4/2019 C Interview Questions 2
50/53
Solution
1. Taking an example:
int array[10],size;
size=sizeof(array)/sizeof(array[0]);
2. sizeof(array) returns the total number of bytes occupied
by array.In this case,it returns 10*sizeof(int)
3. To get the number of elements,divide the total numberof bytes by the size of each element of the array.
8/4/2019 C Interview Questions 2
51/53
What would be the output?
if(a=0) {printf(First);}
if(a=1) {printf(Second);}
8/4/2019 C Interview Questions 2
52/53
Solution
1. An assignment operation returns the value assigned. i.e.
a=0 returns 0 and a=1 returns 1.
2. The first if condition will never be true since a=0 returns
0 and if(a=0) is the same as if(0).
8/4/2019 C Interview Questions 2
53/53
How would you write a program in
C that wouldnt compile in C++?
Recommended