The C language is a powerful, general-purpose, procedural programming language developed in the 1970s by Dennis Ritchie at Bell Labs. It is called a "middle-level" language because it combines the features of high-level languages (like Python or Java) with the low-level control of assembly language.
Key Features:
main and Main are treated as two different names.The C character set is the set of valid characters that can be used to write C programs.
Variables: A variable is a named location in memory used to store data. In C, every variable must be declared with a specific data type before it can be used.
Constants: Constants are fixed values that do not change during program execution.
There are two main types of constants:
5, 3.14, 'A', "Hello".const float PI = 3.14159;#define PI 3.14159A declaration statement introduces a variable to the compiler. It specifies the variable's data type and its name.
Syntax: data_type variable_name;
Common Data Types:
| Data Type | Keyword | Description | Example |
|---|---|---|---|
| Integer | int |
Whole numbers | int age = 25; |
| Floating-point | float |
Single-precision decimal numbers | float price = 19.99; |
| Double | double |
Double-precision decimal numbers | double pi = 3.14159265; |
| Character | char |
A single character | char grade = 'A'; |
You can declare multiple variables: int a, b, c;
You can also initialize at declaration: int count = 0;
An **expression** is a combination of variables, constants, and operators that evaluates to a single value.
A **statement** is a complete instruction to the computer, usually ending in a semicolon (`;`).
Arithmetic Operators:
+ (Addition)- (Subtraction)* (Multiplication)/ (Division) - Note: Integer division truncates the decimal (e.g., 5 / 2 is 2).% (Modulus) - Gives the remainder of a division (e.g., 5 % 2 is 1).Example Expression: (a + b) * 10
Example Statement: area = 0.5 * base * height;
The assignment statement uses the assignment operator (=) to store the value of an expression into a variable.
Syntax: variable_name = expression;
Examples:
int x;
x = 10; // Assigns 10 to x
x = x + 5; // Assigns 15 to x
To perform I/O, you must first include the standard input/output library: #include
The printf() function is used to print formatted text and variable values to the console.
Syntax: printf("format_string", argument1, argument2, ...);
The format string contains plain text and format specifiers. Each specifier is a placeholder for an argument.
%d or %i: for integers (int)%f: for float%lf: for double (long float)%c: for char%s: for strings (character arrays)\n: special character for a newline.Example:
int age = 20; float height = 5.8;
printf("Age: %d, Height: %.1f\n", age, height);
Output: Age: 20, Height: 5.8
The scanf() function is used to read formatted data from the user via the keyboard.
Syntax: scanf("format_string", &variable1, &variable2, ...);
& (the "address-of" operator) before each variable name (except for strings). This tells scanf() *where in memory* to store the input value. Forgetting the & is the most common beginner error.
Example:
int number;
printf("Enter a number: ");
scanf("%d", &number);
This program demonstrates all concepts from Unit 1 by implementing Practical #1b (Area of a rectangle).
#include <stdio.h> // For printf() and scanf()
int main() {
// Declaration Statements
float length, width;
float area;
// Input Statement (printf for prompt)
printf("Enter the length of the rectangle: ");
// Input Statement (scanf to read data)
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
// Arithmetic Expression and Assignment Statement
area = length * width;
// Output Statement
printf("The area of the rectangle is: %.2f\n", area);
return 0;
}
& in `scanf`:** This is the most critical point. scanf("%d", &num) is correct. scanf("%d", num) is wrong and will crash your program.%d for int, %f for float, %lf for double, and %c for char.5 / 2 is 2. If you want 2.5, you must use floats: 5.0 / 2.0.