C

C is a foundational programming language known for speed and direct control over memory. It is widely used in system programming, operating systems, and embedded devices. C programs are written as text files with a .c extension and compiled into machine code using a compiler (like GCC). Clear code in C means careful variable naming, consistent formatting, and commenting logic rather than obvious syntax. This chapter introduces core elements: printf, variables, arithmetic, conditions, loops, functions, arrays, and finishes with a working subtraction calculator as the final example.
Hello world
The classic starter program includes stdio.h for input/output and uses printf.
#include <stdio.h>
int main() {
printf("Hello, world!\\n");
return 0;
}
Variables and types
C uses types like int, float, and char. Declaring them clearly avoids confusion. Use scanf to read input but always validate for safety in real programs.
int age = 12;
float height = 4.5;
char initial = 'A';
Conditions
Conditions guide program flow. The braces { } group statements together.
int age = 18;
if (age >= 18) {
printf("Adult\\n");
} else {
printf("Minor\\n");
}
Loops
Loops repeat actions. for is commonly used for counting, while for conditions. Keep body short and simple.
for (int i = 0; i < 5; i++) {
printf("i = %d\\n", i);
}
Functions
Functions group logic for reuse. Declare them before use or place them above main.
int add(int a, int b) {
return a + b;
}
Final example — calculator for subtraction
The following program reads two numbers, subtracts them, and prints the result. Save this as program.c and compile with gcc program.c -o program before running.
program.c
#include <stdio.h>
int main() {
double minuend, subtrahend, difference;
printf("Enter minuend: ");
scanf("%lf", &minuend);
printf("Enter subtrahend: ");
scanf("%lf", &subtrahend);
difference = minuend - subtrahend;
printf("Difference: %lf\\n", difference);
return 0;
}
MCQs
1. Which header file is required to use printf() in C?
(a) stdlib.h
(b) string.h
(c) stdio.h
(d) math.h
► (c) stdio.h
2. What is the correct file extension for a C source file?
(a) .cpp
(b) .c
(c) .cs
(d) .java
► (b) .c
3. Which function reads input from the user?
(a) get()
(b) scanf()
(c) cin()
(d) input()
► (b) scanf()
4. Which format specifier prints a floating-point value in printf?
(a) %d
(b) %c
(c) %f
(d) %s
► (c) %f
5. What does the return value 0 in main() usually indicate?
(a) Successful execution
(b) Error in program
(c) Infinite loop
(d) Nothing special
► (a) Successful execution
6. Which keyword is used to declare a constant value in C?
(a) define
(b) const
(c) static
(d) fixed
► (b) const
7. What will the condition (5 > 3) evaluate to in C?
(a) 5
(b) 3
(c) 1
(d) true
► (c) 1
8. Which loop is guaranteed to run at least once?
(a) for
(b) while
(c) do...while
(d) foreach
► (c) do...while
9. Which operator is used for equality comparison in C?
(a) =
(b) ==
(c) !=
(d) :=
► (b) ==
10. Which array declaration is correct in C?
(a) int arr[5];
(b) arr int[5];
(c) int arr = [5];
(d) array arr(5);
► (a) int arr[5];
11. Which statement about functions in C is true?
(a) They cannot return values
(b) They must always be void
(c) They can return any single value type
(d) They cannot take parameters
► (c) They can return any single value type
12. Which operator increments a variable by 1?
(a) +
(b) ++
(c) +=
(d) inc
► (b) ++
13. Which of these is a valid comment in C?
(a) # comment
(b) // comment
(c) -- comment
(d) <!-- comment -->
► (b) // comment
14. Which keyword is used to exit from a loop immediately?
(a) exit
(b) stop
(c) break
(d) quit
► (c) break
15. Which function is used to find the length of a string in C?
(a) length()
(b) strlen()
(c) size()
(d) strlength()
► (b) strlen()
16. What is the index of the first element in a C array?
(a) 1
(b) 0
(c) -1
(d) Depends on compiler
► (b) 0
17. Which operator is used to access the value stored at a memory address?
(a) &
(b) *
(c) ->
(d) %
► (b) *
18. In the subtraction calculator, which variables represent the numbers?
(a) alpha, beta
(b) minuend, subtrahend
(c) first, second
(d) num1, num2
► (b) minuend, subtrahend