LearnFrenzy
Sign in Take a free test

C Programming interview · Set 1 of 12

The C Language Basics

  • 5 follow-up questions
  • 4 key points
  • Code examples
Test yourself

C Language Basics: Fundamentals of Programming

C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in 1972. It's one of the most influential programming languages, forming the foundation for many modern languages like C++, Java, Python, and JavaScript. Understanding C basics is essential for systems programming, embedded systems, and understanding how computers work at a low level.

  • Procedural LanguageC follows a procedural paradigm where programs are structured as functions that call each other in a top-down approach.
  • Middle-Level LanguageC combines high-level language features with low-level capabilities like direct memory access through pointers.
  • Structured ProgrammingC supports functions, loops, conditionals, and blocks for organized, readable code.
  • PortabilityC programs can run on different machines with minimal changes, making it ideal for cross-platform development.
  • Rich Operator SetC provides a wide range of operators including arithmetic, logical, bitwise, and relational operators.
  • Standard LibrariesC comes with extensive standard libraries for I/O, string handling, math, and more.

The C Language Basics

This section covers foundational concepts of C programming, from its history and features to basic syntax and structure .

1. What is C language, and why is it called a mid-level programming language?

Answer: C is a versatile, procedural programming language created by Dennis Ritchie at Bell Labs in 1972. It's extensively used in system programming, embedded systems, and applications that demand high performance. C is regarded as a mid-level programming language because it blends elements of both high-level and low-level languages. It enables direct manipulation of hardware resources, like assembly language, while also offering high-level programming constructs such as functions and structures .


2. What are the basic data types supported in C?

Answer: The basic data types in C include [citation 2]:

  • int: for integers (whole numbers).
  • float: for single-precision floating-point numbers (decimals).
  • double: for double-precision floating-point numbers (more precision).
  • char: for characters (single letters, digits, or symbols).

3. What are tokens in C?

Answer: Tokens are the smallest individual units in a C program. The compiler breaks a program into various tokens. The types of tokens are :

  • Keywords: Predefined words like int, if, return.
  • Identifiers: Names given to variables, functions, etc.
  • Constants: Fixed values like 10, 3.14, 'A'.
  • String Literals: Text enclosed in double quotes, e.g., "Hello".
  • Operators: Symbols that perform operations, e.g., +, -, *.

4. What is the use of the printf() and scanf() functions?

Answer: These are standard input/output functions defined in stdio.h. printf() is used for outputting data to the console, while scanf() is used for reading formatted input from the console. They are essential for interacting with users, allowing us to display messages and capture user input .

#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\n", num);
    return 0;
}

5. Explain the difference between == and = in C.

Answer: = is an assignment operator used to assign a value to a variable. == is a relational operator used to compare two values for equality. Using = inside a conditional statement like if is a common mistake that leads to logical errors.

int a = 5;
if (a = 10) { // Assignment, not comparison. This block will always execute because a is now 10 (non-zero).
    printf("This is a common bug!\n");
}
if (a == 10) { // Correct comparison. This block will execute only if a equals 10.
    printf("This is correct.\n");
}

Remember

Key points

  • Program Structure

    Every C program has a main() function - the entry point. Programs consist of preprocessor directives, global declarations, and functions.

  • Basic Data Types

    C provides int, float, double, char for storing different kinds of data. Size varies by system (typically 1-8 bytes).

  • Syntax Rules

    Statements end with semicolons, blocks are enclosed in {}, comments use // or /* */. C is case-sensitive.

  • Identifiers & Keywords

    Names for variables, functions must start with letter or underscore. 32 keywords are reserved (if, while, return, etc.).

Interview drill

Follow-up questions

What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.

  1. 1 What is the basic structure of a C program? Explain with an example. Basic

    A C program typically consists of the following sections:

    1. Documentation Section: Comments about program
    2. Preprocessor Directives: #include, #define statements
    3. Global Declarations: Global variables, function prototypes
    4. main() Function: Entry point of program
    5. User-defined Functions: Additional functions
    #include <stdio.h>  // Preprocessor directive
    
    #define PI 3.14159  // Macro definition
    
    // Global variable
    int globalCount = 0;
    
    // Function prototype
    float calculateArea(float radius);
    
    int main() {  // main function - entry point
        printf("Hello, World!\n");
        
        int localVar = 10;  // Local variable
        globalCount++;
        
        float area = calculateArea(5.0);
        printf("Area of circle: %.2f\n", area);
        
        return 0;  // Return statement
    }
    
    // User-defined function
    float calculateArea(float radius) {
        return PI * radius * radius;
    }

    Key insight: Every C program must have a main() function. Execution always starts from main().

  2. 2 What are the basic data types in C? Explain their sizes and ranges. Basic

    C provides four basic data types:

    TypeSize (typical)Format SpecifierRange
    char1 byte%c-128 to 127 or 0 to 255
    int4 bytes%d-2,147,483,648 to 2,147,483,647
    float4 bytes%f~3.4E-38 to 3.4E+38
    double8 bytes%lf~1.7E-308 to 1.7E+308

    Modifiers like short, long, signed, unsigned can modify these basic types.

    #include <stdio.h>
    #include <limits.h>
    #include <float.h>
    
    int main() {
        printf("Data Type Sizes (in bytes):\n");
        printf("char: %zu\n", sizeof(char));
        printf("int: %zu\n", sizeof(int));
        printf("float: %zu\n", sizeof(float));
        printf("double: %zu\n", sizeof(double));
        
        printf("\nInteger Ranges:\n");
        printf("INT_MAX: %d\n", INT_MAX);
        printf("INT_MIN: %d\n", INT_MIN);
        
        printf("\nFloat Ranges:\n");
        printf("FLT_MAX: %e\n", FLT_MAX);
        printf("FLT_MIN: %e\n", FLT_MIN);
        
        // Examples
        char ch = 'A';
        int num = 100;
        float pi = 3.14159f;
        double e = 2.718281828459045;
        
        printf("\nValues: char=%c, int=%d, float=%.2f, double=%.15f\n",
               ch, num, pi, e);
        
        return 0;
    }

    Key insight: Type sizes can vary by system. Use sizeof() for portability and limits.h/float.h for range information.

  3. 3 What are variables and constants in C? How do you declare them? Basic

    Variables are named memory locations that can change value during program execution. Constants are fixed values that cannot be changed.

    Variable declaration: data_type variable_name;

    Constant declaration: Two ways - const keyword or #define preprocessor directive.

    #include <stdio.h>
    
    #define MAX_SIZE 100  // Preprocessor constant (no semicolon)
    #define PI 3.14159
    
    int main() {
        // Variable declarations
        int age = 25;           // integer variable
        float salary = 45000.50; // float variable
        char grade = 'A';        // character variable
        
        // const variables
        const int DAYS_IN_WEEK = 7;
        const float TAX_RATE = 0.15;
        
        // Using constants
        int numbers[MAX_SIZE];  // Using #define constant
        
        printf("Age: %d\n", age);
        printf("Salary: %.2f\n", salary);
        printf("Grade: %c\n", grade);
        printf("Days in week: %d (const)\n", DAYS_IN_WEEK);
        printf("PI from #define: %.5f\n", PI);
        
        // Can modify variables
        age = 26;
        printf("Updated age: %d\n", age);
        
        // DAYS_IN_WEEK = 8;  // ERROR! Can't modify const
        
        return 0;
    }

    Key insight: #define creates compile-time constants without type safety. const creates typed constants checked by compiler.

  4. 4 What are the different types of operators in C? Basic

    C provides a rich set of operators:

    1. Arithmetic: +, -, *, /, %
    2. Relational: ==, !=, <, >, <=, >=
    3. Logical: && (AND), || (OR), ! (NOT)
    4. Bitwise: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift)
    5. Assignment: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
    6. Increment/Decrement: ++, --
    7. Conditional (ternary): ? :
    8. sizeof: returns size in bytes
    9. Address-of (&) and Dereference (*): for pointers
    10. Comma: , (evaluates multiple expressions)
    #include <stdio.h>
    
    int main() {
        int a = 10, b = 3;
        
        // Arithmetic
        printf("Arithmetic:\n");
        printf("a + b = %d\n", a + b);
        printf("a - b = %d\n", a - b);
        printf("a * b = %d\n", a * b);
        printf("a / b = %d (integer division)\n", a / b);
        printf("a %% b = %d (modulo)\n", a % b);
        
        // Relational
        printf("\nRelational:\n");
        printf("a == b: %d\n", a == b);
        printf("a > b: %d\n", a > b);
        
        // Logical
        int x = 1, y = 0;
        printf("\nLogical:\n");
        printf("x && y: %d\n", x && y);
        printf("x || y: %d\n", x || y);
        printf("!x: %d\n", !x);
        
        // Bitwise
        printf("\nBitwise:\n");
        printf("a & b = %d\n", a & b);
        printf("a | b = %d\n", a | b);
        printf("a ^ b = %d\n", a ^ b);
        printf("~a = %d\n", ~a);
        printf("a << 1 = %d\n", a << 1);
        
        // Increment/Decrement
        int c = 5;
        printf("\nIncrement/Decrement:\n");
        printf("c++: %d (post-increment)\n", c++);
        printf("Now c: %d\n", c);
        printf("++c: %d (pre-increment)\n", ++c);
        
        // Ternary
        int max = (a > b) ? a : b;
        printf("\nTernary: max = %d\n", max);
        
        // sizeof
        printf("\nsizeof(int) = %zu bytes\n", sizeof(int));
        
        return 0;
    }

    Key insight: Operator precedence matters. Use parentheses for clarity. Bitwise operators work on individual bits, logical on boolean values.

  5. 5 What are control structures in C? Explain with examples. Intermediate

    Control structures determine the flow of program execution. C provides:

    1. Conditional (Selection) Statements:

    • if, if-else, else-if ladder
    • switch-case for multiple selections

    2. Looping (Iteration) Statements:

    • while - entry-controlled loop
    • do-while - exit-controlled loop (executes at least once)
    • for - counter-based loop

    3. Jump Statements:

    • break - exit loop/switch
    • continue - skip to next iteration
    • goto - unconditional jump (avoid)
    • return - exit function
    #include <stdio.h>
    
    int main() {
        int num = 5;
        
        // if-else
        printf("=== if-else ===\n");
        if(num > 0) {
            printf("%d is positive\n", num);
        } else if(num < 0) {
            printf("%d is negative\n", num);
        } else {
            printf("Zero\n");
        }
        
        // switch-case
        printf("\n=== switch-case ===\n");
        int choice = 2;
        switch(choice) {
            case 1:
                printf("Option 1 selected\n");
                break;
            case 2:
                printf("Option 2 selected\n");
                break;
            default:
                printf("Invalid option\n");
        }
        
        // for loop
        printf("\n=== for loop (1 to 5) ===\n");
        for(int i = 1; i <= 5; i++) {
            printf("%d ", i);
        }
        printf("\n");
        
        // while loop
        printf("\n=== while loop (countdown) ===\n");
        int count = 5;
        while(count > 0) {
            printf("%d ", count--);
        }
        printf("\n");
        
        // do-while loop
        printf("\n=== do-while (executes at least once) ===\n");
        int x = 10;
        do {
            printf("x = %d\n", x);
            x++;
        } while(x < 5);  // Condition false, but loop executed once
        
        // break and continue
        printf("\n=== break and continue ===\n");
        for(int i = 1; i <= 10; i++) {
            if(i == 5) continue;  // Skip 5
            if(i == 8) break;     // Stop at 8
            printf("%d ", i);
        }
        printf("\n");
        
        return 0;
    }

    Key insight: Choose the right control structure: for loops when you know iteration count, while for condition-based, do-while when you need at least one execution. break and continue provide fine-grained loop control.

Preparing with friends?

Share this page with your study group or placement batch.

Search LearnFrenzy

Esc

Quick links