#C First Lecture
#C First Lecture
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;
}
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;
}
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
code
/* This is a multi-line comment.
It can span multiple lines. */
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;
}
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;
}
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.
code
#include <stdio.h>
int main(void) {
int number1, number2, sum;
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;
6. Arithmetic in C
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
code
if (condition) {
// Code to execute if condition is true
}
Example:
code
int number1 = 5, number2 = 10;
5 is less than 10