Every C program, from the shortest exercise to an operating system, is built on the same framework. Learning that framework, together with the basic data types, input and output functions and operators, is enough to begin writing useful programs.
The Empty C Program
The smallest complete C program does nothing, but it shows the essential structure.
int main(void)
{
return 0;
}
Execution of every C program begins at the function main. The braces enclose the body of the function. The statement return 0; ends the program and reports successful completion to the operating system. Every statement ends with a semicolon.
Structure of a Typical Program
A preprocessor directive such as #include <stdio.h> makes the standard input and output functions available. Declarations introduce variables and their types. Statements then perform the work.
Data Types
Table 2.11 lists the basic data types. The sizes given are typical; the exact size depends on the compiler.
Table 2.11: Basic data types of C
| Type | Stores | Typical size | Format specifier |
|---|---|---|---|
char |
A single character | 1 byte | %c |
int |
A whole number | 4 bytes | %d |
float |
A real number | 4 bytes | %f |
double |
A real number, more precise | 8 bytes | %lf |
Definition: Variable. A named location in memory that holds a value of a declared type, which may change while the program runs.
Output with printf() and Input with scanf()
The function printf() displays text. A format specifier in the text, such as %d, is replaced by the value of the corresponding variable. The sequence \n moves the output to a new line.
The function scanf() reads a value typed by the user. It needs the address of the variable, which is written by placing & before the variable name, as in scanf("%d", &a);. The format specifier must match the type of the variable.
Operators
C provides several groups of operators.
- Assignment:
=stores a value in a variable. - Arithmetic:
+,-,*,/and%(remainder). When both operands of/are integers, the result is an integer:7 / 2gives 3. - Relational:
<,<=,>,>=,==,!=compare two values and give true or false. - Logical:
&&(and),||(or) and!(not) combine conditions. The conditionmark >= 0 && mark <= 100is true only when both parts are true.
In C, false is represented by 0 and true by 1.
Example 2.11: Arithmetic on two integers
Problem: Read two integers and display their sum, quotient, remainder and average.
Solution:
- Declare the variables with suitable types.
- Read the two integers with scanf().
- Use
/and%for the integer quotient and remainder, and convert tofloatfor the average.
#include <stdio.h>
int main(void)
{
int a, b;
float avg;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
avg = (a + b) / 2.0f;
printf("\nSum = %d\n", a + b);
printf("Quotient = %d\n", a / b);
printf("Remainder = %d\n", a % b);
printf("Average = %.2f\n", avg);
return 0;
}
Input:
17 5
Output:
Enter two integers:
Sum = 22
Quotient = 3
Remainder = 2
Average = 11.00
Result: 17 / 5 gives the integer quotient 3 and 17 % 5 gives the remainder 2. Dividing by 2.0f instead of 2 makes the average a real number, 11.00.
Common mistakes
- Omitting
&before the variable name in scanf(). - Using a format specifier that does not match the type, such as
%dfor a float. - Expecting
7 / 2to give 3.5; integer division discards the fractional part.
Key takeaways
- Every C program starts executing at
main, and every statement ends with a semicolon. - The basic data types are
char,int,floatanddouble. printf()displays output andscanf()reads input, both using format specifiers.- Operators are grouped as assignment, arithmetic, relational and logical.
Practice questions
- At which function does the execution of a C program begin?
- What is the value of 17 % 5 in C?
- Why is & written before the variable name in scanf()?
Answers to the practice questions
- At the function main.
- 2, the remainder when 17 is divided by 5.
- scanf() needs the address of the variable so that it can store the value read there.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II
