C Interview Question Answers
C Interview Question Answers
Page 3
C Interview Questions
Page 5
What is C Language?
Page 6
C Interview Questions
3. What is a token?
The individual elements of a program are called Tokens. There are following 6 types
of tokens are available in C:
Identifiers
Keywords
Constants
Operators
Special Characters
Strings
7. What is a Preprocessor?
A preprocessor is a software program that processes a source file before sending it to
be compiled. Inclusion of header files, macro expansions, conditional compilation,
and line control are all possible with the preprocessor.
C Interview Questions
Return Value:
On successful conversion, it returns the desired integer value
If the string starts with alpha-numeric char or only contains alpha-num char, 0 is
returned.
In case string starts with numeric character but is followed by alpha-num char,
the string is converted to integer till the first occurrence of alphanumeric char.
C Interview Questions
void do_recursion()
{
... .. ...
do_recursion();
... .. ...]
}
int main()
{
... .. ...
do_recursion();
... .. ...
}
Additionally, C does not feature strict typing. Many things are implicitly convertible to
each other in C. The complexity of overload resolution rules could introduce
confusion in such kind of language
13. What is the difference between global int and static int
declaration?
The difference between this is in scope. A truly global variable has a global scope and
is visible everywhere in your program.
#include <stdio.h>
int my_global_var = 0;
int
main(void)
{
printf("%d\n", my_global_var);
return 0;
}
#include <stdio.h>
int
myfunc(int val)
{
static int my_static_var = 0;
my_static_var += val;
return my_static_var;
}
int
main(void)
{
int myval;
myval = myfunc(1);
printf("first call %d\n", myval);
myval = myfunc(10);
return 0;
}
n++ being a unary operation, it just needs one variable. Whereas, n = n + 1 is a binary
operation that adds overhead to take more time (also binary operation: n += 1).
However, in modern platforms, it depends on few things such as processor
architecture, C compiler, usage in your code, and other factors such as hardware
problems.
While in the modern compiler even if you write n = n + 1 it will get converted into n++
when it goes into the optimized binary, and it will be equivalently efficient.
For Example:
int x;
for(x=97; x<=122; x++)
{
printf("%c", (char)x); /*Explicit casting from int to char*/
}
The term "r-value" refers to a data value stored in memory at a given address.
An r-value is an expression that cannot have a value assigned to it, hence it can
only exist on the right side of an assignment operator(=).
The term "l-value" refers to a memory location that is used to identify an object.
The l-value can be found on either the left or right side of an assignment
operator(=). l-value is frequently used as an identifier.
Whereas in the union, all the member variables are stored at the same location on
the memory as a result to which while assigning a value to a member variable will
change the value of all other members.
C Interview Questions
union foo {
int a; // we can't use both a and b simultaneously
char b;
} foo;
struct bar y;
y.a = 3; // OK to use
y.b = 'c'; // OK to use
union foo x;
x.a = 3; // OK
x.b = 'c'; // NOl this affects the value of x.a!
Pass By Reference
int main()
{
char * ptr = malloc(sizeof(int));
/* Do some work */
/*Not freeing the allocated memory*/
return 0;
}
To avoid memory leaks, you can trace all your memory allocations and think forward,
where you want to destroy (in a good sense) that memory and place delete there.
Another way is to use C++ smart pointer in C linking it to GNU compilers.
C is a language known for its low-level control over the memory allocation of
variables in DMA there are two major standard library malloc() and free. The malloc()
function takes a single input parameter which tells the size of the memory requested
It returns a pointer to the allocated memory. If the allocation fails, it returns NULL.
The prototype for the standard library function is like this:
void *malloc(size_t size);
The free() function takes the pointer returned by malloc() and de-allocates the
memory. No indication of success or failure is returned. The function prototype is like
this:
void free(void *pointer);
There are 4 library functions provided by C defined under <stdlib.h> header file to
facilitate dynamic memory allocation in C programming. They are:
malloc()
calloc()
free()
realloc()
typedef provides an alias name to the existing complex type definition. With typedef,
you can simply create an alias for any type. Whether it is a simple integer to complex
function pointer or structure declaration, typedef will shorten your code.
The standard input library gets() reads user input till it encounters a new line
character. However, it does not check on the size of the variable being provided by
the user is under the maximum size of the data type which makes the system
vulnerable to buffer overflow and the input being written into memory where it isn’t
supposed to.
We, therefore, use gets() to achieve the same with a restricted range of input
Bonus: It remained an official part of the language up to the 1999 ISO C standard,
but it was officially removed by the 2011 standard. Most C implementations still
support it, but at least GCC issues a warning for any code that uses it.
#include<stdio.h>
#include<string.h>
char *func()
{
char str[10];
strcpy(str,"Hello!");
return(str);
}
You are returning an address that was a local variable, which would have gone out of
scope by the time control was returned to the calling function. (Undefined behavior)
*c = malloc(5izeof(int));
free(c);
*c = 3; //writing to freed location!
In the figure shown above writing to a memory that has been freed is an example of
the dangling pointer, which makes the program crash.
A memory leak is something where the memory allocated is not freed which causes
the program to use an undefined amount of memory from the ram making it
unavailable for every other running program(or daemon) which causes the programs
to crash. There are various tools like O profile testing which is useful to detect
memory leaks on your programs.
void function(){
char *leak = malloc (10); //leak assigned but not freed
}
Near Pointer: In general, the near pointer can be considered because it is used
to hold the address, which has a maximum size of just 16 bits. We can't store an
address with a size larger than 16 bits using the near pointer. All other smaller
addresses that are within the 16-bit limit, on the other hand, can be stored.
Because we can only access 64kb of data at a time, you might assume the 16 bits
are insufficient. As a result, it is regarded as one of the near-pointer's biggest
drawbacks, which is why it is no longer commonly used.
Far Pointer: A far pointer is considered a pointer of size 32 bits. It can, however,
use the current segment to access information stored outside the computer's
memory. Although, in order to use this type of pointer, we usually need to
allocate the sector register to store the data address in the current segment.
37. Suppose a global variable and local variable have the same
name. Is it possible to access a global variable from a block
where local variables are defined?
No. This isn’t possible in C. It’s always the most local variable that gets preference.
If we let it, the compiler can build enum values automatically. However, each of
the defined values must be given separately.
Because macros are preprocessors, unlike enums, which are compile-time
entities, the source code is unaware of these macros. So, if we use a debugger to
debug the code, the enum is superior.
Some compilers will give a warning if we use enum values in a switch and the
default case is missing.
Enum always generates int-type identifiers. The macro, on the other hand,
allowed us to pick between various integral types.
Unlike enum, the macro does not have a defined scope constraint.
#include <stdio.h>
int main()
{
int n, a[100], b[100], calc = 0, i, j,count;
printf("Enter no. of elements in array.n");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i<n; i++)
{
for (j = 0; j<calc; j++)
{
if(a[i] == b[j])
break;
}
if (j== calc)
{
b[count] = a[i];
calc++;
}
}
printf("Array obtained after removing duplicate elements");
for (i = 0; i<calc; i++)
{
printf("%dn", b[i]);
}
return 0;
}
#include<studio.h>
#define abc main
int abc ()
{
printf("Hello World ");
return 0;
}
C Interview Questions
41. Write a program to get the higher and lower nibble of a byte
without using shift operator?
#include<stdio.h>
struct full_byte
{
char first : 4;
char second : 4;
};
union A
{
char x;
struct full_byte by;
};
main()
{
char c = 100;
union A a;
a.x = c;
printf("the two nibbles are: %d and %d\n", a.by.first, a.by.second);
}
#include <stdio.h>
#include <conio.h>
int reverse(int num);
int isPalindrome(int num);
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(isPalindrome(num) == 1)
{
printf("the given number is a palindrome");
}
else
{
printf("the given number is not a palindrome number");
}
return 0;
}
#include<stdio.h>
#include<conio.h>
int main() {
int j,num;
printf("Please enter a number :");
scanf("%d",&num);
while(num>0)
{
j=num%10;
if( j!=0 && j!=1 )
{
printf("num is not binary");
break;
}
num=num/10;
if(num==0)
{
printf("num is binary");
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
46.
Can you tell me how to check whether a linked list is
circular?
Single Linked List
A very simple way to determine whether the linked list is circular or not
Traverse the linked list
Check if the node is pointing to the head.
If yes then it is circular.
Let's look at the snippet where we code this algorithm.
-Store the value of first node in temp variable and make it traverse all nodes.
-temp-firstgode
-tempenext node pointed by temp(temp->next)
-run until temp is at null or firstNode
if (temp at null)
not circular and returns false
if (temp points first node)
return true as its circular.
}
-To insert new nodes and link each one of them to the previous node by storing the addr
-Then make them point to NULL.
}
-First insert nodes for circular linked list and check its nature by calling isCircular
-Since it is true through if statement it prints "yes..
-Second insert a normal linked list and check its nature by calling isCircular function
It is majorly related to how the compiler reads( or parses) the entire code and breaks
it into a set of instructions(or statements), to which semicolon in C acts as a
boundary between two sets of instructions.
In fun
In main
In main
Macros Functions
The difference between the Source Code and Object Code is that Source Code is a
collection of computer instructions written using a human-readable programming
language while Object Code is a sequence of statements in machine language, and is
the output after the compiler or an assembler converts the Source Code.
The last point about Object Code is the way the changes are reflected. When the
Source Code is modified, each time the Source Code needs to be compiled to reflect
the changes in the Object Code.
51. What are header files and what are its uses in C
programming?
Header Files in C
In C header files must have the extension as .h, which contains function definitions,
data type definitions, macro, etc. The header is useful to import the above definitions
to the source code using the #include directive. For example, if your source code
needs to take input from the user do some manipulation and print the output on the
terminal, it should have stdio.h file included as #include <stdio.h>, with which we can
take input using scanf() do some manipulation and print using printf().
The keyword “void” is a data type that literally represents no data at all. The most
obvious use of this is a function that returns nothing:
void PrintHello()
{
printf("Hello\n");
return; // the function does "return", but no value is returned
}
we’ve declared a function, and all functions have a return type. In this case, we’ve
said the return type is “void”, and that means, “no data at all” is returned.
The other use for the void keyword is a void pointer. A void pointer points to the
memory location where the data type is undefined at the time of variable definition.
Even you can define a function of return type void* or void pointer meaning “at
compile time we don’t know what it will return” Let’s see an example of that.
For the sum of two numbers, we use the addition (+) operator. In these tricky C
programs, we will write a C program to add two numbers without using the addition
operator.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x, y;
printf("Enter two number: ");
scanf("%d %d",&x,&y);
// method 1
printf("%d\n", x-(-y));
// method 2
printf("%d\n", -(-x-y));
// method 3
printf("%d\n", abs(-x-y));
// method 4
printf("%d", x-(~y)-1);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x, y;
printf("Enter two number: ");
scanf("%d %d",&x,&y);
printf("%d", x+(~y)+1);
return 0;
}
The bitwise complement operator is used in this program. The bitwise complement
of number ~y=-(y+1). So, expression will become x+(-(y+1))+1=x-y-1+1=x-y
C Interview Questions
#include<stdio.h>
int main()
{
int x;
printf("Enter a number: ");
scanf("%d",&x);
printf("%d", x<<1);
return 0;
}
The left shift operator shifts all bits towards the left by a certain number of specified
bits. The expression x<<1 always returns x*2. Note that the shift operator doesn’t
work on floating-point values.
For multiple of x by 4, use x<<2. Similarly x<<3 multiply x by 8. For multiple of the
number x by 2^n, use x<<n.
#include<stdio.h>
int main()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
(x&1)?printf("Odd"):printf("Even");
return 0;
}
The bitwise and(&) operator can be used to quickly check the number is odd or even.
Below is naive implementation for finding the nth member of the Fibonacci
sequence
int main()
{
int n = 8;
return 0;
}