📢 CDAC C-CAT Feb 2027 — Dates Announced | 25 Nov 2026 to 29 Dec 2026 Form Filling View Details →
← All Free Notes

C Programming

Section B: C, C++, DSA, OS, Networking & AI

Introduction to C Programming; C Program Structure; Data Types and Variables

Introduction to C Programming; C Program Structure; Data Types and Variables

Introduction to C Programming

History of C

Why Learn C?

1. Foundation language — understanding C helps you learn any language

2. **System

programming** — OS kernels, device drivers, embedded systems written in C

3. Performance

— C is one of the fastest languages (close to hardware)

4. Portability — C programs can

run on any platform with minor changes

5. Control — direct memory management through

pointers

6. Widely used — Linux kernel, PostgreSQL, Python interpreter, Git are written

in C

Where is C Used?

C Program Structure

Basic Structure

```c

/* Preprocessor directives */

#include // Standard Input/Output header

#include // Standard Library header

/* Global declarations (optional) */

int global_var = 10;

/* Main function - entry point of every C program */

int main(int argc, char *argv[]) {

/* Local variable declarations */

int x = 5;

float pi = 3.14;

/* Statements */

printf("Hello, World!\n");

printf("x = %d, pi = %.2f\n", x, pi);

/* Return 0 to indicate successful execution */

return 0;

}

```

Compilation Process

```

Source Code Pre-processing Compilation Assembly Linking

(.c file) (.i file) (.s file) (.o file) (executable)

hello.c → [Preprocessor] → [Compiler] → [Assembler] → [Linker] → a.out / hello

(expand macros) (type check) (machine code) (link libs)

(replace headers)

```

Compiling a C program:

```bash

gcc hello.c -o hello # Compile and link

gcc -S hello.c # Compile to assembly only

gcc -c hello.c # Compile to object file only

gcc hello.c -Wall -Wextra # Enable all warnings

./hello # Run the program

```

Essential Headers

Data Types and Variables

Fundamental Data Types

```c

#include

#include

int main() {

char ch = 'A';

int i = 42;

float f = 3.14f;

double d = 3.141592653589793;

printf("char: %c (%d bytes, value=%d)\n", ch, (int)sizeof(char), ch);

printf("int: %d (%d bytes)\n", i, (int)sizeof(int));

printf("float: %.2f (%d bytes)\n", f, (int)sizeof(float));

printf("double: %.15lf (%d bytes)\n", d, (int)sizeof(double));

printf("INT_MAX = %d\n", INT_MAX); // 2147483647

printf("INT_MIN = %d\n", INT_MIN); // -2147483648

return 0;

}

```

Variable Declaration

```c

// Declaration: type variable_name;

int x;

float salary;

char grade;

// Declaration with initialization

int a = 10;

float pi = 3.14159;

char letter = 'C';

// Multiple declarations

int p, q, r;

int m = 1, n = 2, k = 3;

// Constants (cannot be modified)

const int MAX_SIZE = 100;

const float PI = 3.14159;

```

Type Casting

```c

int a = 5, b = 2;

int result = a / b; // 2 (integer division)

float fresult = (float)a / b; // 2.5 (explicit cast)

double dresult = (double)a / b; // 2.5

// Implicit (automatic) conversion

int x = 3.7; // float → int: x = 3 (truncated, not rounded)

float y = 5; // int → float: y = 5.0 (widening conversion)

```

sizeof Operator

```c

printf("int: %zu bytes\n", sizeof(int)); // 4

printf("double: %zu bytes\n", sizeof(double)); // 8

printf("char: %zu bytes\n", sizeof(char)); // 1

printf("int array[5]: %zu bytes\n", sizeof(int[5])); // 20

```

Continue full topic on App →

Practice MCQs (20)

1. Which data type is most suitable to store the number 65000 on a 32-bit system?

  • A. `long`
  • B. `signed short`
  • C. `int`
  • D. `unsigned short`

2. What is printed? ```c signed char a = 322; unsigned char b = -1; printf("%d %d %c", b-a, sizeof(a=1), a); ```

  • A. 127, 4, -124
  • B. -323, 1, 322
  • C. 189, 1, B
  • D. 127, 4, A

3. Which of the following is NOT a valid escape sequence?

  • A. `\q`
  • B. `\f`
  • C. `\a`
  • D. `\v`

4. Which of the following does `\r` do?

  • A. Moves cursor to next line
  • B. Moves cursor to beginning of same line
  • C. Moves cursor one char back
  • D. Inserts vertical tab

5. Copy one structure to another:

  • A. All of these
  • B. `memcpy`
  • C. `s1 = s2;`
  • D. Field-by-field

6. What is the range of a `signed short` on most 32-bit systems?

  • A. -2³¹ to 2³¹-1
  • B. -128 to 127
  • C. -32,768 to 32,767
  • D. 0 to 65,535

7. Bit-fields: which is FALSE?

  • A. `#pragma pack(1)` removes padding
  • B. Inside struct/union
  • C. Can exceed specified bits safely
  • D. `&` not allowed

8. Which of the following is an implicit typecast?

  • A. `int x = 45.67;`
  • B. `float f = (float)5/3;`
  • C. `int *p = (int*)malloc(4);`
  • D. `char c = (char)65;`

9. `sizeof` a structure:

  • A. Sum of members
  • B. At least sum (with padding)
  • C. Always 8
  • D. Largest member

10. What is printed? ```c signed char ch = 128; printf("%d", ch); ```

  • A. 128
  • B. garbage
  • C. -128
  • D. compiler dependent

11. Output? ```c typedef struct { int a1; char ch; } demo_t; demo_t s1={1}, s2={1}; if (s1 == s2) ... else ... ```

  • A. Runtime
  • B. Sunbeam Pune
  • C. sunbeam Karad
  • D. Compile error

12. A nested structure:

  • A. Stacked unions
  • B. Struct containing another struct member
  • C. Not allowed
  • D. Array of arrays

13. Output? ```c union demo { int a1; unsigned k1:2; unsigned char s1:6; } st; st.s1=127; st.k1=8; st.a1=15; int *p = st.a1; ++*p; // Treating int as pointer … ```

  • A. `63,0,8,4,4`
  • B. Error
  • C. all 16
  • D. all 15

14. What does the following code print? ```c short x = 32767; x = x + 1; printf("%d\n", x); ```

  • A. 32768
  • B. 0
  • C. -32768
  • D. 32767

15. Which is NOT a type modifier in C?

  • A. `signed`
  • B. `unsigned`
  • C. `short`
  • D. `big`

16. Output? `printf("%d", sizeof('A'));` (C compiler, not C++)

  • A. 2
  • B. 1
  • C. 8
  • D. 4

17. Struct member can be pointer to same struct type?

  • A. Yes (self-referential → linked list)
  • B. No
  • C. Only C++
  • D. Only with typedef

18. Which keyword allows a variable's address to be taken?

  • A. `register` variables
  • B. All variables by default
  • C. Only pointers
  • D. `volatile` only

19. Short alias for a struct type:

  • A. `typedef struct { … } Name;`
  • B. `alias struct X Y;`
  • C. `rename struct X Y`
  • D. `#define struct ALIAS`

20. Which of the following is an automatic variable?

  • A. `extern int`
  • B. Global `int`
  • C. `static int` local
  • D. Local `int` without storage class
View App Pricing →