1.
A Simple C Program: Printing a Line of Text
The first program we’ll look at is one that prints a line of text to the screen. This helps us understand the
structure of a C program.
Code:
#include <stdio.h>
int main(void) {
printf("Welcome to C!\n");
return 0;
}
Let’s break it down:
Explanation:
1. #include <stdio.h>: This is a preprocessor directive.
o It tells the C compiler to include the Standard Input/Output library, which provides
functions for input and output, like printf.
o Think of this like importing tools that let you do things like print text or read user input.
2. int main(void): This is the main function where the program execution begins.
o Every C program must have a main function. The parentheses () indicate that this is a
function.
o The keyword int tells the computer that this function returns an integer, a whole number.
This is a C convention, and even if you don’t need to return anything, for now, we use it to
follow best practices.
3. printf("Welcome to C!\n");: This is an output statement.
o printf is a function from stdio.h that allows you to display text to the screen.
o The text inside the quotation marks ("Welcome to C!") is called a string literal, meaning it’s
a fixed piece of text that will be shown exactly as written.
o "\n" is an escape sequence that tells the program to move to a new line after printing the
text. Without it, the cursor would stay on the same line after the text is printed.
4. return 0;: This statement indicates that the program ends successfully. It’s a good practice to
return 0 from main, signaling to the operating system that the program finished without errors.
Example:
Let’s modify the program slightly:
code
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
If you run this program, the output will be:
Hello, World!
The structure remains the same, but we can change the text inside printf to display any message we
want.
2. Comments
Comments in C are essential for making your code understandable to humans. Comments do not aVect
the program’s execution.
• Single-line comments start with //.
code
// This is a single-line comment
• Multi-line comments are enclosed in /* */.
code
/* This is a multi-line comment.
It can span multiple lines. */
Why use comments?
• They help explain what your code is doing.
• They make it easier for others (or yourself) to understand your code later.
• They do not aVect the performance of the program because the compiler ignores them.
Example:
code
#include <stdio.h>
int main(void) {
// Print a welcome message
printf("Welcome to C programming!\n"); // Print text to the console
return 0;
}
3. Using printf and Escape Sequences
The function printf is used for displaying output. The format is:
code
printf("text\n");
But it can do more than just print text. You can use escape sequences to format your output:
• \n: Newline. Moves the cursor to the next line.
• \t: Tab. Moves the cursor to the next tab stop.
• \\: Prints a backslash (\).
• \": Prints a double-quote (").
Example:
code
#include <stdio.h>
int main(void) {
printf("Line one\nLine two\n");
printf("Tab\tseparated\n");
printf("Printing backslash: \\\n");
return 0;
}
The output will be:
mathematica
code
Line one
Line two
Tab separated
Printing backslash: \
In this example:
• The first \n moves the text to a new line.
• The \t inserts a tab between "Tab" and "separated".
• The \\ prints a backslash.
4. Adding Two Integers: Introducing Variables
Let’s move on to a program where the user inputs two numbers, and the program adds them. This
introduces the concept of variables.
code
#include <stdio.h>
int main(void) {
int number1, number2, sum;
printf("Enter first number: ");
scanf("%d", &number1); // Read first integer
printf("Enter second number: ");
scanf("%d", &number2); // Read second integer
sum = number1 + number2; // Calculate sum
printf("The sum is: %d\n", sum); // Display result
return 0;
}
Explanation:
1. Variables: int number1, number2, sum;
o These are variables of type int (integer). They store whole numbers.
o Think of variables as boxes that hold values. In this case, number1, number2, and sum are
boxes that will hold numbers.
2. scanf("%d", &number1);:
o scanf is a function that reads user input.
o %d is a format specifier for integers.
o The & before the variable name is necessary because scanf needs the memory address of
the variable to store the user input.
3. sum = number1 + number2;:
o This line performs arithmetic. It adds the values of number1 and number2, then stores the
result in sum.
4. printf("The sum is: %d\n", sum);:
o The %d in printf is a placeholder for an integer. It’s replaced by the value of sum when the
program runs.
Example Output:
If the user inputs 10 and 20, the output will be:
code output
Enter first number: 10
Enter second number: 20
The sum is: 30
5. Memory Concepts
Each variable in C has:
• A Name: The identifier you use in your program (like number1).
• A Type: The kind of data it stores (in this case, int for integers).
• A Value: The actual data it holds (like 10 or 20).
• A Memory Location: The location in the computer’s memory where the value is stored.
When you declare a variable like this:
code
int number1;
C reserves a spot in memory to store the value of number1.
6. Arithmetic in C
You can perform various mathematical operations using operators:
• Addition: +
• Subtraction: -
• Multiplication: *
• Division: /
• Remainder (Modulus): %
Example:
code
int a = 10, b = 3;
int sum = a + b; // 13
int diVerence = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3 (integer division)
int remainder = a % b; // 1
7. Decision Making: if Statements
C allows you to make decisions in your code using if statements. An if statement checks a condition and
executes the code inside the braces {} only if the condition is true.
code
if (condition) {
// Code to execute if condition is true
}
Example:
code
int number1 = 5, number2 = 10;
if (number1 < number2) {
printf("%d is less than %d\n", number1, number2);
}
In this case, since 5 is less than 10, the program will output:
csharp
5 is less than 10