C Questions
C Questions
C Questions
What is C Language?
In this article, you will get to know the latest C Interview Questions & Answers you
could expect as a fresher, intermediate, and experienced candidate.
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 so ware 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.
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.
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 le 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.
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
}