Overview
C is a general-purpose programming language developed by Dennis Ritchie in the early 1970s. It was initially created to write the Unix operating system but quickly became one of the most popular and widely used programming languages. Today, C is known for its efficiency, low-level memory access, and minimalistic syntax, which makes it an excellent choice for developing operating systems, embedded systems, and high-performance applications
Why Learn C?
- System-level programming: C is ideal for writing operating systems, hardware drivers, and embedded systems where you need full control over memory and system resources.
- Portability: C code can run on a wide range of platforms with little modification.
- Foundation for other languages: Many modern languages (like C++, Java, and C#) derive from C, making it an essential foundation for anyone serious about programming.
Structure of a C Program
Every C program follows a specific structure. Understanding this structure is crucial as it forms the backbone of how C programs work. Here’s a breakdown of the key components:
Preprocessor Directives
Preprocessor directives instruct the compiler to include or modify code before the compilation starts. The most common directive you’ll encounter is #include, which tells the compiler to include external libraries or header files.
#include <stdio.h>
The main() Function
The main() function is the starting point of any C program. When you run a C program, the execution always begins from main(). Without it, the program won’t compile or execute.
int main() {
// Code goes here
return 0;
}
The function returns an integer (hence the int) and typically returns 0 to indicate successful execution.
Statements and Expressions
Statements are instructions that the program executes sequentially. These can be variable assignments, function calls, or even control structures like loops and conditionals.
int a = 10; // Assignment statement
a = a + 5; // Expression with operators
Comments
Comments are non-executable parts of the code used to document or explain code. C supports single-line comments (//) and multi-line comments (/* */).
// This is a single-line comment
/* This is a
multi-line comment */
Your First C Program: “Hello, World!”
Let’s write your first C program. One of the most common ways to get started with any programming language is by writing a “Hello, World!” program. This program simply prints the text “Hello, World!” to the screen.
Here’s the code:
#include <stdio.h> // Preprocessor directive to include the standard input/output library
int main() { // The main function where execution begins
printf("Hello, World!\n"); // Print statement
return 0; // Return statement indicating successful program execution
}
Code Breakdown
- return 0: This statement indicates that the program ended successfully. Returning 0 is a way of telling the operating system that the program ran without errors.
- #include <stdio.h>: This directive includes the Standard Input Output library, which provides functions like printf() to handle input and output.
- int main() { … }: This is the main function where the program execution starts. The code inside {} is what will be executed when the program runs.
- printf(“Hello, World!\n”): This is a function call to printf(), which prints the string “Hello, World!” followed by a newline (\n).
Output
When you compile and run this program, it will display “Hello, World!” in the console. Congratulations! You’ve just written your first C program.
Hello, World!
Exercises For You
To reinforce what you’ve learned so far, here’s a simple exercise to try out on your own.
Question 1: Print Your Name and Age
Write a C program that prints your name and age. This is similar to the “Hello, World!” program but requires you to change the printed message.
Task: Create a program that outputs your name and age.
Expected Output:
Name: Alice
Age: 30
Question 2: Basic Math Operations
Write a C program to perform basic arithmetic operations (addition, subtraction, multiplication, division) on two numbers. You can choose any two numbers of your choice.
Expected Output:
Enter first number: 15
Enter second number: 5
Addition: 20
Subtraction: 10
Multiplication: 75
Division: 3
Question 3: Print a Pattern
Write a C program that prints the following pattern:
Enter a number: 4
Square of 4 is 16
Question 4: Square of a Number
Write a C program that takes a number as input and prints its square.
Expected Output:
Enter a number: 4
Square of 4 is 16
Exercise 5: Print a Quote
Write a C program that prints your favorite quote using printf(). Include quotation marks in the output.
Expected Output:
"My favorite quote is: 'The only limit to our realization of tomorrow is our doubts of today.' - Franklin D. Roosevelt"
Summary
In this first tutorial, we covered:
- The history and purpose of C programming.
- The basic structure of a C program, including preprocessor directives, the main() function, and the use of statements and comments.
- Writing and understanding a simple “Hello, World!” program.
- A simple exercise for you to practice printing your own name and age.
Now that you’ve grasped these fundamentals, you’re well on your way to mastering C programming! In the next video, we’ll dive deeper into Variables and Data Types, where you’ll learn how to store and manipulate different kinds of data in C.