Showing posts with label c interview questions and answers. Show all posts
Showing posts with label c interview questions and answers. Show all posts

c interview questions and answers

c interview questions and answers

Top 150+ C Interview Questions and Answers – Beginner to Advanced 

If you’re preparing for technical interviews, C is still one of the most commonly asked programming languages—especially in companies like TCS, Infosys, Wipro, and product-based firms.

basic to advanced C interview questions, including output-based questions, programs, and real-world concepts.

Basic C Interview Questions (Freshers)

1. What is a static variable?

A static variable retains its value between function calls.


#include <stdio.h>

void func() {
    static int x = 0;
    x++;
    printf("%d ", x);
}

int main() {
    func();
    func();
    func();
    return 0;
}

Output: 1 2 3

verizon C interview quetions

Here is a listing of C programming interview questions on “File Access” along with answers, explanations and/or solutions:
1. The first and second arguments of fopen are
a) A character string containing the name of the file & the second argument is the mode.
b) A character string containing the name of the user & the second argument is the mode.
c) A character string containing file poniter & the second argument is the mode.
d) None of the mentioned of the mentioned
View Answer
Answer:a
2. For binary files, a ___ must be appended to the mode string.
a) Nothing
b) “b”
c) “binary”
d) “01″
View Answer
Answer:b
3. If there is any error while opening a file, fopen will return
a) Nothing
b) EOF
c) NULL
d) Depends on compiler
View Answer
Answer:c
4. Which is true about getc.getc returns?
a) The next character from the stream referred to by file pointer
b) EOF for end of file or error
c) Both a & b
d) Nothing.
View Answer
Answer:c
5. When a C program is started, O.S environment is responsible for opening file and providing     pointer for that file?
a) Standard input
b) Standard output
c) Standard error
d) All of the menitoned
View Answer
Answer:d
6. FILE is of type ______ ?
a) int type
b) char * type
c) struct type
d) None of the mentioned
View Answer
Answer:c
7. What is the meant by ‘a’ in the following operation?
    fp = fopen(“Random.txt”, “a”);
a) Attach
b) Append
c) Apprehend
d) Add
View Answer
Answer:b
8. Which of the following mode argument is used to truncate?
a) a
b) f
c) w
d) t
View Answer
Answer:c
9. Which type of files can’t be opened using fopen()?
a) .txt
b) .bin
c) .c
d) None of the mentioned
View Answer

Answer:d

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

* and ++ operators c interview question


* and ++ operators c interview questions

Question: What would be the output of the following code and why?
#include

int main(void)
{
    char *ptr = "Linux";
    printf("\n [%c] \n",*ptr++);
    printf("\n [%c] \n",*ptr);

    return 0;
}
Answer: The output of the above would be :
[L] 

[i]
Since the priority of both ‘++’ and ‘*’ are same so processing of ‘*ptr++’ takes place from right to left. Going by this logic, ptr++ is evaluated first and then *ptr. So both these operations result in ‘L’. Now since a post fix ‘++’ was applied on ptr so the next printf() would print ‘i’.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

void* and C structures interview question


void* and C structures interview question
Question: Can you design a function that can accept any type of argument and returns an integer? Also, is there a way in which more than one arguments can be passed to it?
Answer: A function that can accept any type of argument looks like :
 int func(void *ptr)
if more than one argument needs to be passed to this function then this function could be called with a structure object where-in the structure members can be populated with the arguments that need to be passed.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

free() function interview question c programming language


Question: The following program seg-faults (crashes) when user supplies input as ‘freeze’ while it works fine with input ‘zebra’. Why?
#include

int main(int argc, char *argv[])
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return -1;
    }
    else if(argc == 1)
    {
        printf("\n Usage  \n");
    }
    else
    {
        memset(ptr, 0, 10);

        strncpy(ptr, argv[1], 9);

        while(*ptr != 'z')
        {
            if(*ptr == '')
                break;
            else
                ptr++;
        }

        if(*ptr == 'z')
        {
            printf("\n String contains 'z'\n");
            // Do some more processing
        }

       free(ptr);
    }

    return 0;
}
Answer: The problem here is that the code changes the address in ‘ptr’ (by incrementing the ‘ptr’) inside the while loop. Now when ‘zebra’ is supplied as input, the while loop terminates before executing even once and so the argument passed to free() is the same address as given by malloc(). But in case of ‘freeze’ the address held by ptr is updated inside the while loop and hence incorrect address is passed to free() which causes the seg-fault or crash.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

far and near pointers in c

What is "far" and "near" pointers in "c" ?
A:“Near" and "far" pointers are actually non-standard qualifiers that you'll find only on x86 systems. They reflect the odd segmentation architecture of Intel processors. In short, a near pointer is an offset only,which refers to an address in a known segment. A far pointer is a compound value, containing both a segment number and an offset into that segment. Segmentation still exists on Intel processors, but it is not used in any of the mainstream 32-bit operating systems developed for them, so you'll generally only find the "near" and "far" keywords in source code developed for Windows 3.x,MS-DOS, Xenix/80286, etc.

Difference %d and %*d

What is the difference between %d and %*d in c language?
A: %d give the original value of the variable and %*d give the address of the variable.
eg:-int a=10,b=20;
printf("%d%d",a,b);
printf("%*d%*d",a,b);
Result is 10 20 1775 1775 .Here 1775 is the starting address of the memory allocation for the integer.a and b having same address because of contagious memory allocation.

When typecast be used?

When should a type cast be used?
A:There are two situations in which to use a type cast. The first use is to change the type of an operand to an arithmetic operation so that the operation will be performed properly.
The second case is to cast pointer types to and from void * in order to interface with functions that expect or return void pointers. For example, the following line type casts the return value of the call to malloc() to be a pointer to a foo structure.
struct foo *p = (struct foo *) malloc(sizeof(struct foo));

Searching in array list carried out?

How is searching in an array list carried out?
For searching an element in an array list, first we traverse the array list and with traversing, we compare each element of array with the given element.

Operations performed on C++ lists?

What are the operations performed on C++ lists?
The operations that can be performed on lists are insertion, deletion, traversal and search.

Funny C Interview Questions

Funny C Interview Questions
1.Write a C program which prints Hello World! without using a semicolon?

a) #include
int main()
{if(printf("HelloWorld")){}
2. Write a small C program to determine whether a machine's type is little-endian or big-endian.?
Big endian order:-Here the “big end” (most significant value in the sequence) is stored first, at the lowest storage address. The most significant byte is stored in the leftmost position
a)#include
int main()
{
unsigned int number = 10;
char *ptr;
ptr = (char *)&number;
if(*ptr)
Printf(“little-endian \n”);
else
Printf(“big-endian \n”);
return 0;
}
3. What is the format specifiers for printf to print double and float values?
a)printf(“%lf , %f”);
%lf - double
%f - float
4. What is the difference between memcpy and memmove?
memcpy memmove
Takes 3 arguments a Void *ptr, const void *cptr , and an int n. Takes 3 arguments a Void *ptr, const void *cptr , and an int.
Copies ‘n’ characters from const void ptr cptr to void ptr ptr and returns void ptr. Copies ‘n’ characters from const void ptr cptr to void ptr ptr and returns void ptr.
Not reliable Reliable when an overlap.like objects cptr and ptr overlap.
4. Write a C program to find the smallest of three integers, without using any of the comparision operators.
a) #include
#include
int main() {
int x = 2;
int y = 1;
int z = 3;
int r;
r = y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
r = z + ((r - z) & ((r - z) >> (sizeof(int) * CHAR_BIT - 1)));
printf("%d\n", r);
}
5.What does the format specifier %n of printf function do?
a)Tthe format specifier “ %n” will give the number of characters actually formatted in the printf
function.
Int num = 10;
Char *a= “helloworld”;
Printf(“ %3x,%n ” ,num, &a);
Printf(“%d no of bytes are formatted here into hex”,a);
6. What's the difference between the following two C statements?
const char *p;
char* const p;
a) const char *p; p is a pointer to a constant character;
b) char* const p; p is a constant character pointer;
both are same.
7. How do you print I can print % using the printf function?
a) using %% in the format specifier.
Printf(“%d %% %d” ,a ,b);
Output is a % b;
8.Whats the difference between bitwise and logical operators?
Bitwise Example:
if ((val==1)|(val==2)) printf("%d\n",val);
else printf("False");??
a)bitwise operators are operated on bits. for a bitwise OR operator, both of the left hand side and right hand side values/expressions are evaluated. then the 'evaluated values' are ORed together.
int a = 33, b = 40, c;
c = a | b;
/***********************
33 => 100001 a
40 => 101000 b
------------ OR
41 => 101001 c
***********************/
on the other hand, if expressions are used as the operands of bitwise operator OR:
int val = 2, a;
a = (val == 1) | (val == 2);
printf("%d\n", a);
then the output for the above code is always 1 if val is either 1 or 2, and the output is 0 otherwise. because, in the above case, (val == 1) is evaluated. if this is true, then (val==1) is considered 1, otherwise 0. similarly, (val == 2) is evaluated and is considered 1 or 0 depending if it is true or false. then these two 0 or 1 are ORed together.
logical operators work a little bit different way. say, the code is
(val==1) || (val==2)
the above is a logical expression, whose value is either true or false. to evaluate the above expression, first (val==1) is checked. if this is found true, then the entire expression become true and no further checking is required to check whether (val==2) or not, computer skips checking the right hand side, since they are ORed. if (val==1) is found false, then the value of the expression depends on whether (val==2) is true or false.

similarly, for logical AND operator,

(a==1) && (b==2)

first (a==1) is evaluated. if this is found false, then the entire expression become false and computer skips checking (b==2), since they are ANDed together. if (a==1) is found true, then the checking (b==2) is required to evaluate the whole expression.

so use logical operators when "if this AND that" or "if this OR that" kind of checking is required. go for the bitwise operator when u really need a new bit stream. remember, logical operators skip evaluating in certain conditions where bitwise operators always evaluate both sides.

C interview Lint Interview Questions

C interview Lint Interview Questions
1.I just typed in this program, and it’s acting strangely. Can you
see anything wrong with it?

A:Try running lint first.

2.How can I shut off the “warning: possible pointer alignment
problem” message lint gives me for each call to malloc?

A:It may be easier simply to ignore the message, perhaps in an
automated way with grep -v.

3.Where can I get an ANSI-compatible lint?

A: See the unabridged list for two commercial products.

C Interview :Variable-Length Argument Lists

C Interview :Variable-Length Argument Lists
1.How can I write a function that takes a variable number of
arguments?

A:Use the header.

2.How can I write a function that takes a format string and a
variable number of arguments, like printf, and passes them to
printf to do most of the work?

A:Use vprintf, vfprintf, or vsprintf.

3.How can I discover how many arguments a function was actually
called with?

A:Any function which takes a variable number of arguments must be
able to determine from the arguments themselves how many of them
there are.

4.How can I write a function which takes a variable number of
arguments and passes them to some other function (which takes a
variable number of arguments)?

A:In general, you cannot.

C Preprocessor Interview Questions

C Preprocessor Interview Questions
1.How can I write a generic macro to swap two values?
A:There is no good answer to this question. The best all-around
solution is probably to forget about using a macro.

2.I have some old code that tries to construct identifiers with a
macro like “#define Paste(a, b) a/**/b”, but it doesn’t work any
more.

A:Try the ANSI token-pasting operator ##.

3.What’s the best way to write a multi-statement cpp macro?
A: #define Func() do {stmt1; stmt2; … } while(0) /* (no trailing ; ) */

4.How can I write a cpp macro which takes a variable number of
arguments?

A:One popular trick is to define the macro with a single argument,
and call it with a double set of parentheses, which appear to the
preprocessor to indicate a single argument:

#define DEBUG(args) {printf(“DEBUG: “); printf args;}
if(n != 0) DEBUG((“n is %d\n”, n));

C cpp Interview Questions

c cpp Interview Questions
1)How do you decide which integer type to use?
2)What should the 64-bit integer type on new, 64-bit machines be?
3)What’s the best way to declare and define global variables?
4)What does extern mean in a function declaration?
5)What’s the auto keyword good for?
6)I can’t seem to define a linked list node which contains a pointer to itself.
7)How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
8)How can I declare a function that returns a pointer to a function of its own type?
9)My compiler is complaining about an invalid redeclaration of a function, but I only define it once and call it once. What’s happening?
10)What can I safely assume about the initial values of variables which are not explicitly initialized?