C constructs usually mean the basic building blocks of the C language things like variables, data types, operators, loops, conditionals, functions, arrays, pointers, etc.

1. What is the difference between declaration and definition of a variable/function

Declaration

  • Declares that a variable or function exists in the program.
  • Memory is not allocated.
  • Provides important information about the type of the variable/function.
  • For a variable: program knows its data type.
  • For a function: program knows its arguments, their data types, order of arguments, and return type.

Definition

  • Includes everything that a declaration does.
  • Allocates memory for the variable or function.
  • For a function: provides the actual body/implementation.
  • Can be seen as a superset of declaration (or declaration as a subset of definition).

// This is only declaration. y is not allocated memory by this statement
extern int y;

// This is both declaration and definition, memory to x is allocated by this statement.
int x;

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. See this for more details.

3. How will you print “Hello World” without semicolon?

#include <stdio.h>
int main(void)
{
    if (printf("Hello World"))
    {
    }
}

4. Write down the smallest executable code?’

main is necessary for executing the code. Code is

void main()
{
}

5. What are entry control and exit control loops? 

C support only 2 loops:  

  • Entry Control: This loop is categorized in 2 part while loop & for loop
  • Exit control: In this category, there is one type of loop known as, do while loop.

6. Why pre-processor directive does not have a semi-colon at last? 

Semi-colon is needed by the compiler and as the name suggests Preprocessors are programs that process our source code before compilation. Therefore the semi-colon is not required.

7. What is the difference between including the header file with-in angular braces < > and double quotes ” “? 

If a header file is included within < > then the compiler searches for the particular header file only within the built-in include path. If a header file is included within ” “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built-in include path.

8. Why does “type demotion” does not exist instead of “type promotion”? Also, it would consume less space resource than by doing it from type promotion? 

Let’s take an example to understand it. 
Suppose : double a=1.5; int b=10 and we want to calculate a+b 

By type demotion, float type a will convert to int. Therefore a=1 and a+b=1+10=11 but we know that correct answer is 11.5 which will only get by type promotion. So the conclusion is that by type demotion we will not get the correct answer.

9. Difference between #include in C and import in Java?  

#includeimport
#include is a statement not a keyword. 
 
While import is a keyword.
It is processed by pre-processor software.It is processed by compiler.
It increases the size of the code.It doesn’t increases the size of the code. Here, even if we write 
import java.lang.*; 
it will not attach all the class. Rather it will give permission to access the class of java.lang

10. What are local static variables? What is their use?

A local static variable is a variable whose lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value as 0. For example, the following program prints “0 1”

#include <stdio.h>
void fun()
{
    // static variables get the default value as 0.
    static int x;
    printf("%d ", x);
    x = x + 1;
}
int main()
{
    fun();
    fun();
    return 0;
}
// Output: 0 1

11. Can we declare a variable with the same name in two different functions?

  • Yes, each function has its own local scope.
  • Variable names declared inside different functions don’t conflict with each other.

#include <stdio.h>
void func1() {
    int x = 10;
    printf("In func1: x = %d\n", x);
}
void func2() {
    int x = 20;
    printf("In func2: x = %d\n", x);
}
int main() {
    func1();  // x = 10
    func2();  // x = 20
    return 0;
}

12. What are main characteristics of C language? 

C is a procedural language. The main features of C language include low-level access to memory, simple set of keywords, and clean style. These features make it suitable for system programming like operating system or compiler development. 

13. What is difference between i++ and ++i? 

1) The expression ‘i++’ returns the old value and then increments i. The expression ++i increments the value and returns new value. 
2) Precedence of postfix ++ is higher than that of prefix ++. 
3) Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left. 
4) In C++, ++i can be used as l-value, but i++ cannot be. In C, they both cannot be used as l-value. 

See Difference between ++*p, *p++ and *++p for more details.

14. What is l-value? 

l-value or location value refers to an expression that can be used on left side of assignment operator. For example in expression “a = 3”, a is l-value and 3 is r-value. 

l-values are of two types: 

  • “nonmodifiable l-value” represent a l-value that can not be modified. const variables are “nonmodifiable l-value”. 
  • “modifiable l-value” represent a l-value that can be modified.

15. How will you print numbers from 1 to 100 without using loop? 

We can use recursion for this purpose.

/* Prints numbers from 1 to n */
void printNos(unsigned int n)
{
  if(n > 0)
  {
    printNos(n-1);
    printf("%d ",  n);
  } 
}

16. What is volatile keyword? 

The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. 
Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.

17. Can a variable be both const and volatile? 

Yes, the const means that the variable cannot be assigned a new value. The value can be changed by other code or pointer. For example the following program works fine. 

#include <stdio.h>
int main(void)
{
    const volatile int local = 10;
    int* ptr = (int*)&local;
    printf("Initial value of local : %d \n", local);
    *ptr = 100;
    printf("Modified value of local: %d \n", local);
    return 0;
}

18. What happens if you use an uninitialized variable in C?

  • Global and static variables are initialized to 0 (or NULL for pointers) automatically if not explicitly initialized.
  • Local (automatic) variables are not initialized and contain indeterminate (garbage) values.
  • Using local non-static variables without initialization leads to undefined behavior and should never done. Their value can be random, depending on leftover data in memory.
#include <stdio.h>
int main() {
    int x;  // uninitialized local variable
    printf("Uninitialized value: %d\n", x); // Output: garbage (undefined behavior)
    return 0;
}

19. What is while(1) and how does it work?

  • 1 is treated as a true condition in C.
  • So while(1) runs forever unless interrupted using break, return, or external control.
  • An example use case is servers (for example web server) and local services that run forever and wait for a request to process inside a loop.

#include <stdio.h>
int main() {
    while (1) {
        printf("Looping...\n");
        break;  // Stops the loop after one iteration
    }
    return 0;
}

20. What is the difference between the local and global variables in C and What happens when a local variable has the same name as a global variable?

Local variables are declared inside a block or function but global variables are declared outside the block or function to be accessed globally.

Local VariablesGlobal Variables
Declared inside a block or a function.Variables that are declared outside the block or a function.
By default, variables store a garbage value.By default value of the global value is zero.
The life of the local variables is destroyed after the block or a function.The life of the global variable exists until the program is executed.
Variables are stored inside the stack unless they are specified by the programmer.The storage location of the global variable is decided by the compiler.
To access the local variables in other functions parameter passing is required.No parameter passing is required. They are globally visible throughout the program.

when a local variable has the same name as a global variable, then:

  • The local variable takes priority inside the function; this is called shadowing.
  • The global variable is still in memory but is ignored unless accessed using pointers or special tricks.

#include <stdio.h>
// Global variable (default initialized 
// to 0 if not explicitly assigned)
int globalVar = 100; 
void showGlobal() {
    printf("In showGlobal: globalVar = %d\n", globalVar);
}
int main() {
    
    // Local variable (garbage if uninitialized)
    int localVar = 50; 
    printf("In main: localVar = %d\n", localVar);
    printf("In main: globalVar = %d\n", globalVar);
    showGlobal();
    return 0;
}

21. How can we create an infinite loop in C?

There can be only two things when there is an infinite loop in the program.

  • No termination condition or a termination condition that never becomes false.
  • No break statement or unsatisfied break conditions.
Types of Loops
Types of Loops

Below is the program for infinite loop in C:

#include <stdio.h>
// Driver code
int main()
{
    for (;;) {
        printf("Infinite-loop\n");
    }
    while (1) {
        printf("Infinite-loop\n");
    }
    do {
        printf("Infinite-loop\n");
    } while (1);
    return 0;
}

Here, In for loop there is no break statement & In while and do-while loop there is no statement that is making the condition false.

Such loops will keep running forever unless externally stopped (e.g., program termination). In competitive programming environments, this often results in a “Time Limit Exceeded (TLE)” error, but in normal C execution, the program just hangs.

22.  What is type conversion, and difference between implicit conversion and explicit type casting?

In C programming, type conversion refers to converting a value from one data type to another. This can be done either implicitly by the compiler or explicitly by the programmer.

Implicit Type Conversion occurs automatically when a value of a smaller data type is assigned to a larger data type (e.g., int to float).

Explicit Type Casting is done manually by the programmer to convert a value from one data type to another using the cast operator.

23.  What’s the difference between = and == in C?

  • = is the assignment operator; it sets a value: a = 5;.
  • == is the equality operator; it checks if two values are equal: if (a == 5), it will either return true or false.

Mixing them up can cause unexpected results and is a common beginner mistake.

#include <stdio.h>
int main() {
    int a = 5;     // '=' assigns 5 to a
    if (a == 5) {  // '==' checks if a equals 5
        printf("a is equal to 5\n");
    }
    return 0;
}

24. Why can’t we use reserved keywords as variable names?

  • Keywords like int, return, if, etc., are part of C’s syntax.
  • Using them for variables would confuse the compiler and cause syntax errors.

For Example: Let’s look at a code that uses “goto” (it’s a keyword) as a variable

#include <stdio.h>
int main() {
    int goto=5;
    printf("%d",goto);
    return 0;
}

Output: This code will throw the following error:

ERROR!
Traceback (most recent call last):
File “<main.py>”, line 3
int main() {
^^^^
SyntaxError: invalid syntax

The code is throwing “SyntaxError” because a keyword is used as variable.

25. What happens if we do not use break in switch statement?

Switch statement compares the value inside switch() to each case value. Execution starts from the first matching case and continues until a break is found. Without break, fall-through occurs (following cases also run).

For example, the following program executes all cases (including default) after case 2.

// There is no break in all cases
#include <stdio.h>
int main()
{
   int x = 2;
   switch (x)
   {
       case 1: printf("Choice is 1\n");
       case 2: printf("Choice is 2\n");
       case 3: printf("Choice is 3\n");
       default: printf("Choice other than 1, 2 and 3\n");
   }
   return 0;
}

26. What will this code print?

#include <stdio.h>
int main() {
    int a=0;
    if(a=1) printf("zero\n");
    else printf("non zero\n");
    return 0;
}
  • a = 1 is assignment, not comparison.
  • So condition becomes true (since a is now 1, no matter what value of a is before if condition), and hence it prints: zero.

27. What does this loop print?

#include <stdio.h>
int main() {
    int i=0;
    while(i++ <3) printf("%d ",i);
    return 0;
}
  • i++ returns the current value, then increments.
  • It prints: 1 2 3

28. What is the result of this code?

#include <stdio.h>
int main() {
    int x = 3;
    int y = ++x*2;
    printf("x: %d, y: %d",x,y);
    return 0;
}
  • ++x increments x to 4 first and then used in the expression.
  • y = 4 * 2 -> y = 12

29. What is the final value of b?

#include <stdio.h>
int main()
{
    int a = 0;
    int b = 0;
    if (a && b++)
        printf("hello\n");
    printf("%d", b);
    return 0;
}
  • a is 0 (false), & b is 0 (false), here b++ is not evaluated due to short-circuiting, as for any expression a && b, b will not be evaluated if a already turns out to be false because if a is false then, irrespective of any value of b the complete expression will always return to false.
  • Output: 0, and “Hello” is not printed.

30. How are variables stored in memory and what is the role of data types?

  • A variable is a name linked to a memory location.
  • Data types define:

Size of the memory block (e.g., int = 4 bytes)
Type of value (integer, float, char, etc.)

  • This helps the compiler know how to process and interpret values.

31. How does the if-else conditional work in C, and when is it better than switch?

  • The if-else structure is used when decisions are based on logical or relational conditions.
  • Syntax allows multiple paths to be handled using cascading else if.
  • Use if-else when:

Conditions involve ranges (if (x > 10 && x < 20))
Conditions involve logical operations

Switch-case is preferred when checking against multiple constant values (e.g., menu options).

32. Can you use else without if in C?

  • No, else is always tied to an if.
  • Using else without if will result in a compile-time error.

#include <stdio.h>
int main()
{
    int i = 0;
    else i++;
    printf("%d", i); // this will give error
    return 0;
}

33. What are the different types of operators in C?

C provides several categories of operators:

  • Arithmetic Operators: +, -, *, /, %
  • Relational Operators: ==, !=, >, <, >=, <=
  • Logical Operators: &&, ||, !
  • Assignment Operators: =, +=, -=, etc.
  • Bitwise Operators: &, |, ^, ~, <<, >>
  • Unary Operators: ++, –, sizeof
  • Ternary Operator: ? :
  • Comma Operator: ,

34. How does the switch statement work in C, and how is it different from if-else?

  • switch lets you test a variable against multiple constant values.
  • More readable than long if-else chains when dealing with discrete cases.

Example:

#include <stdio.h>
int main() {
    char op='*';
    int a=10, b=6;
    switch (op) {
        case '+':
            printf("%d + %d = %d\n", a, b, a + b);
            break;
        case '-':
            printf("%d - %d = %d\n", a, b, a - b);
            break;
        case '*':
            printf("%d * %d = %d\n", a, b, a * b);
            break;
        case '/':
            if (b != 0)
                printf("%d / %d = %d\n", a, b, a / b);
            else
                printf("Error: Division by zero!\n");
            break;
        default:
            printf("Invalid operator!\n");
    }
    return 0;
}

Differences from if-else:

  • switch works with discrete values only (int, char).
  • if-else works with expressions and ranges.
  • switch is faster and cleaner for multiple values.

35. What is the difference between break and continue statements in C?

Both break and continue are used to control the flow inside loops, but they do so differently:

  • break: Immediately exits the loop or switch block.
  • continue: Skips the current iteration and jumps to the next one.

Example with break:

#include <stdio.h>
int main() {
    int ans=0;
    for(int i=0;i<=10;i++){
        if(i%2==0) continue;
        ans+=i;
    }
    printf("%d",ans);
    return 0;
}

Example with continue:

#include <stdio.h>
int main() {
    int ans=0;
    for(int i=0;i<=10;i++){
        if(i==4) break;
        ans+=i;
    }
    printf("%d",ans);
    return 0;
}

36. What is the size of different data types in C and why does it matter?

The size of a data type tells how much memory it occupies. It affects performance, memory usage, and data range.

Common sizes (may vary by system):

  • char: 1 byte
  • int: 4 bytes
  • float: 4 bytes
  • double: 8 byte
#include <stdio.h>
int main() {
    printf("Size of char: %lu byte\n", sizeof(char));
    printf("Size of int: %lu bytes\n", sizeof(int));
    printf("Size of float: %lu bytes\n", sizeof(float));
    printf("Size of double: %lu bytes\n", sizeof(double));
    return 0;
}

37. What is the role of the size of operator in C?

The sizeof operator returns the size (in bytes) of a variable or data type at compile time.

Why it’s important:

  • Helps with dynamic memory allocation.
  • Useful for writing portable code that works across platforms.

Notes:

sizeof is evaluated at compile time, not run time.
Can also be used with arrays to determine their total memory usage

#include <stdio.h>
int main() {
    int arr[10];
    int total_size = sizeof(arr);
    int element_size = sizeof(arr[0]);
    printf("Total array size: %d bytes\n", total_size);
    printf("Size of one element: %d bytes\n", element_size);
    return 0;
}

38. Can we use a char variable to store small numbers?

  • Yes, char is essentially a 1-byte integer (8 bits).
  • It Can hold numbers from -28-1 to 28-1-1 (i.e., -128 to 127) for signed or 0 to 255 for unsigned)

#include <stdio.h>
int main() {
    char a = 100;         // valid signed char
    unsigned char b = 250; // valid unsigned char
    printf("Signed char a = %d\n", a);
    printf("Unsigned char b = %u\n", b);
    return 0;
}

CATEGORIES:

Career Advice

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Comments

No comments to show.