0% found this document useful (0 votes)
30 views

C Interview Questions

The document contains 20 interview questions about C programming. Some key topics covered include data types and variables (scope, NULL pointers), functions (call by value vs reference, dynamic memory allocation), control flow (if/else, for loops), I/O (printf, scanf), arrays, and more. Correct code snippets are provided as answers to questions about syntax, operators, and common errors.

Uploaded by

lovetube
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

C Interview Questions

The document contains 20 interview questions about C programming. Some key topics covered include data types and variables (scope, NULL pointers), functions (call by value vs reference, dynamic memory allocation), control flow (if/else, for loops), I/O (printf, scanf), arrays, and more. Correct code snippets are provided as answers to questions about syntax, operators, and common errors.

Uploaded by

lovetube
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

3RI Technologies Pvt Ltd

C INTERVIEW QUESTIONS
1. Write a program to print "hello world" without using semicolon?

#include<stdio.h>
void main()
{
if(printf("hello world")){}
}

2. What is scope of a variable? How are variables scoped in C?

Scope of a variable is the part of the program where the variable may directly be accessible. In C, all
identifiers are lexically (or statically) scoped.

3. What is NULL pointer?

NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize
pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer
NULL when memory pointed by it is deallocated in the middle of a program.

4. What is the difference between call by value and call by reference in C?

We can pass value to function by one of the two ways: call by value or call by reference. In case of call by
value, a copy of value is passed to the function, so original value is not modified. But in case of call by
reference, an address of value of passed to the function, so original value is modified

5. What functions are used for dynamic memory allocation in C language?

 malloc()
 calloc()
 realloc()
 free()

6. What is auto keyword in C?

In C, every local variable of a function is known as automatic (auto) variable. Let's explain with an
example:
void f()
{
int i ;
auto int j;
}
1
3RI Technologies Pvt Ltd
7. Can we compile a program without main() function?

Yes, we can compile but it can't be executed.


But, if we use #define, we can compile and run C program without using main() function. For example:
#include<stdio.h>
#define start main
void start() {
printf("Hello");
}

8. What is the difference between ++var and var++?

The ++ operator is called the increment operator. When the operator is placed before the variable
(++var), the variable is incremented by 1 before it is used in the expression. When the operator is placed
after the variable (var++), the expression is evaluated, and then the variable is incremented by 1.
The same holds true for the decrement operator (--). When the operator is placed before the variable,
you are said to have a prefix operation. When the operator is placed after the variable, you are said to
have a postfix operation.

9. Can static variables be declared in a header file?

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.

10. Other than in a ‘FOR’ statement, when is the comma operator used?

The comma operator is commonly used to separate variable. declarations, function


arguments, and expressions, as well as the elements of a for statement.

11. What is the difference between “int main()” and “int main(void)” in C?

Both definitions work in C also, but the second definition with void is considered technically better as it
clearly specifies that main can only be called without any parameter.In C, if a function signature doesn’t
specify any argument, it means that the function can be called with any number of parameters or
without any parameters.

12. What are the returned values of printf() and scanf()

In C, printf() returns the number of characters successfully written on the output and scanf() returns
number of items successfully read.

13. In C programming, how do you insert quote characters (‘ and “) into the output screen?

2
3RI Technologies Pvt Ltd
To insert the quote character as part of the output, use the format specifiers \’ (for single quote), and \”
(for double quote).

14. What is wrong in this statement? scanf(“%d”,whatnumber);

An ampersand & symbol must be placed before the variable name whatnumber. Placing & means
whatever integer value is entered by the user is stored at the “address” of the variable name. This is a
common mistake for programmers, often leading to logical errors.

15. What will be the outcome of the following conditional statement if the value of variable s is 10?

s >=10 && s < 25 && s!=12

The outcome will be TRUE. Since the value of s is 10, s >= 10 evaluates to TRUE because s is not greater
than 10 but is still equal to 10. s< 25 is also TRUE since 10 is less then 25. Just the same, s!=12, which
means s is not equal to 12, evaluates to TRUE. The && is the AND operator, and follows the rule that if
all individual conditions are TRUE, the entire statement is TRUE.

16. Do these two program statements perform the same output?

1) scanf(“%c”, &letter); 2) letter=getchar()

Yes, they both do the exact same thing, which is to accept the next key pressed by the user and
assign it to variable named letter.

17. What is the use of a semicolon (;) at the end of every program statement?

A semicolon acts as a delimiter, so that the compiler knows where each statement ends, and can
proceed to divide the statement into smaller elements for syntax checking.

18. How arrays can be passed to a user defined function?

We cannot pass the entire array to a function. Instead, you pass to it a pointer that will point to the
array first element in memory. To do this, you indicate the name of the array without the brackets

19. What is wrong with this program statement? void = 10;

The word void is a reserved word in C language. You cannot use reserved words as a user-defined
variable.

20. Is using exit() the same as using return?

No. The exit() function is used to exit your program and return control to the operating system. The
return statement is used to return from a function and return control to the calling function. If you issue
a return from the main() function, you are essentially returning control to the calling function, which is
the operating system. In this case, the return statement and exit() function are similar.

You might also like