C Programming

C is a powerful general-purpose programming language. It is widely used for system programming, game development, embedded systems, and application development. In this chapter, we will learn about basic syntax, variables, data types, operators, control structures, functions, arrays, pointers, and a final example of a Collatz sequence in C.
Introduction to C
C programs are saved with the .c extension. A C program typically starts with #include <stdio.h> and a main() function. The printf() function is used for output, and scanf() is used for input.
Variables and Data Types
C supports several data types: int, float, double, char. Variables must be declared before use.
#include <stdio.h>
int main() {
int a = 10;
float b = 5.5;
char c = 'A';
printf("Integer: %d\\n", a);
printf("Float: %.2f\\n", b);
printf("Char: %c\\n", c);
return 0;
}
>Operators
C supports arithmetic, relational, logical, assignment, and bitwise operators. Use parentheses to define precedence.
int a = 5, b = 3;
int sum = a + b;
int diff = a - b;
int prod = a * b;
float div = (float)a / b;
printf("Sum: %d\\n", sum);
printf("Difference: %d\\n", diff);
printf("Product: %d\\n", prod);
printf("Division: %.2f\\n", div);
>Control Structures
Use if, else, switch, for, while, and do-while for control flow.
int num = 7;
if(num % 2 == 0) {
printf("Even\\n");
} else {
printf("Odd\\n");
}
for(int i=1; i<=5; i++) {
printf("%d\\n", i);
}
>Functions
Functions in C allow modular programming. Define using return type, name, and parameters.
int add(int x, int y) {
return x + y;
}
int main() {
int result = add(5, 3);
printf("Sum: %d\\n", result);
return 0;
}
>Arrays
Arrays store multiple values of the same type in contiguous memory. Use index starting from 0.
int numbers[5] = {1, 2, 3, 4, 5};
for(int i=0; i<5; i++) {
printf("Number[%d] = %d\\n", i, numbers[i]);
}
>Pointers
Pointers store the memory address of variables. Use * for dereferencing and & to get the address.
int a = 10;
int *ptr = &a;
printf("Address of a: %p\\n", ptr);
printf("Value of a using pointer: %d\\n", *ptr);
>Structures
Structures allow grouping different types under a single name.
struct Person {
char name[20];
int age;
float height;
};
struct Person p1 = {"Alice", 20, 5.5};
printf("Name: %s\\nAge: %d\\nHeight: %.2f\\n", p1.name, p1.age, p1.height);
>Final Example — Collatz Sequence
This program generates the Collatz sequence for a given number. Save as collatz.c.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Collatz sequence: ");
while(n != 1) {
printf("%d ", n);
if(n % 2 == 0)
n = n / 2;
else
n = 3 * n + 1;
}
printf("1\\n");
return 0;
}
>MCQs
1. Which header file is required for input/output in C?
(a) <stdlib.h>
(b) <stdio.h>
(c) <math.h>
(d) <string.h>
► (b) <stdio.h>
2. Which function is the entry point of a C program?
(a) start()
(b) main()
(c) init()
(d) program()
► (b) main()
3. Which format specifier is used for float in printf?
(a) %f
(b) %d
(c) %c
(d) %s
► (a) %f
4. Which symbol is used for comments in C?
(a) // for single line, /* */ for multi-line
(b) #
(c) <!-- -->
(d) <!--
► (a) // and /* */
5. Which data type is used to store characters?
(a) char
(b) int
(c) float
(d) double
► (a) char
6. What is the index of the first element in an array?
(a) 1
(b) 0
(c) -1
(d) 10
► (b) 0
7. Which keyword is used to define a constant?
(a) constant
(b) const
(c) #define
(d) final
► (b) const
8. Which operator is used for multiplication?
(a) *
(b) /
(c) +
(d) %
► (a) *
9. How do you access the value pointed by a pointer?
(a) &ptr
(b) *ptr
(c) ptr
(d) ->ptr
► (b) *ptr
10. Which keyword is used to define a structure?
(a) struct
(b) class
(c) object
(d) typedef
► (a) struct
11. Which loop executes at least once?
(a) for
(b) while
(c) do-while
(d) if
► (c) do-while
12. Which function is used to read input from the user?
(a) printf()
(b) scanf()
(c) gets()
(d) input()
► (b) scanf()
13. What does <stdio.h> stand for?
(a) Standard Input Output
(b) Standard Internal Output
(c) Simple I/O
(d) System I/O
► (a) Standard Input Output
14. Which is the correct way to declare an integer variable?
(a) int num;
(b) integer num;
(c) var num;
(d) num int;
► (a) int num;
15. Which operator is used for modulus?
(a) %
(b) *
(c) /
(d) +
► (a) %
16. How do you end a statement in C?
(a) .
(b) ;
(c) :
(d) ,
► (b) ;
17. Which of these is a valid variable name?
(a) 2ndVar
(b) var_2
(c) my-var
(d) #var
► (b) var_2
18. In the Collatz sequence program, what happens if the number is odd?
(a) n = n / 2
(b) n = n + 1
(c) n = 3 * n + 1
(d) n = n - 1
► (c) n = 3 * n + 1