Essential C Programming Concepts and Syntax

C Programming Fundamentals

Q-1: Structure of a C Program

A C program follows a specific hierarchical structure to ensure the compiler understands how to process the code.

  • Documentation Section: Contains comments (e.g., /* author name */) explaining the program’s purpose.
  • Link Section: Includes header files using #include (e.g., <stdio.h>) to use built-in functions like printf.
  • Definition Section: Where symbolic constants are defined using #define.
  • Global Declaration Section: Variables or functions declared here can be accessed throughout the entire program.
  • Main Function Section: Every C program must have a main() function. This is where execution begins.
    • Declaration part: Variables used in the function are defined.
    • Executable part: The actual logic/statements.
  • Sub-program Section: User-defined functions are placed here.

Q-2: Data Types in C

Data types specify the type of data that a variable can store. This tells the compiler how much space to reserve in memory.

Types of Data Types

  • Basic/Primary: int (integers), float (decimal numbers), char (single characters), double (large decimals).
  • Derived: Array, Pointer, Structure.
  • Enumeration: enum.
  • Void: void (represents no value).

Example Program

#include <stdio.h>
int main() {
    int age = 20;          // Integer type
    float price = 99.50;   // Floating point type
    char grade = 'A';      // Character type

    printf("Age: %d, Price: %.2f, Grade: %c", age, price, grade);
    return 0;
}

Q-3 & Q-4: Operators in C Language

Operators are symbols that tell the compiler to perform specific mathematical or logical manipulations.

1. Arithmetic Operators (Q-4 Focus)

Used for mathematical calculations.

  • + (Addition), - (Subtraction), * (Multiplication), / (Division).
  • % (Modulus): Returns the remainder (e.g., 5 % 2 = 1).

2. Relational Operators

Used to compare two values. They return true (1) or false (0).

  • == (Equal to), != (Not equal to), > (Greater than), < (Less than).

3. Logical Operators

Used to combine two or more conditions.

  • && (Logical AND), || (Logical OR), ! (Logical NOT).

4. Assignment Operators

Used to assign values to variables.

  • =, +=, -=, *= (e.g., a += 5 is the same as a = a + 5).

5. Increment and Decrement Operators

  • ++ (Increments value by 1), -- (Decrements value by 1).

6. Conditional (Ternary) Operator

  • ? : (A shorthand for if-else).

Q-5: The switch Statement

The switch statement is a multi-way branch statement. It allows a variable to be tested for equality against a list of values (cases).

Example:

int day = 2;
switch (day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break; // This will execute
    default: printf("Invalid day");
}
  • Note: The break keyword is essential to stop the execution from “falling through” to the next case.

Q-6: The while Loop

A while loop repeatedly executes a block of code as long as a given condition remains true. It is an entry-controlled loop because the condition is checked before entering the loop body.

Example:

int i = 1;
while (i <= 5) {
    printf("%d ", i);
    i++; // Incrementing to avoid infinite loop
}
// Output: 1 2 3 4 5

Q-11: Difference between while and do-while loop

This is a very common exam question. The main difference lies in when the condition is checked.

Featurewhile Loopdo-while Loop
TypeEntry-controlled loop.Exit-controlled loop.
Condition CheckCondition is checked before execution.Condition is checked after execution.
ExecutionsIf the condition is false, it executes 0 times.Even if the condition is false, it executes at least once.
Syntaxwhile(condition) { ... }do { ... } while(condition);

Q-10: Nested if Statement

A nested if is simply an if statement placed inside another if statement. This is used when a second condition needs to be checked only if the first condition is true.

Example:

int x = 10, y = 20;
if (x == 10) {
    if (y == 20) {
        printf("Both conditions are true.");
    }
}