Unit 1: Introduction to C Language and Basic Statements
Table of Contents
Introduction to C Language
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:
- Structured: Code can be broken down into blocks and functions.
- Compiled: C code is translated into machine code by a compiler, making it very fast.
- Portable: Code written on one machine can be compiled and run on another with minimal changes.
- Case-Sensitive:
mainandMainare treated as two different names.
C Characters
The C character set is the set of valid characters that can be used to write C programs.
- Alphabets: A-Z, a-z
- Digits: 0-9
- Special Characters: `+ - * / % = ( ) { } [ ] < > . , ; : ' " # ! & | ~ ^ _ \ ? $`
- Whitespace: Blank space, newline, tab, etc.
Constants and Variables
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:
- Literal Constants: The value itself, e.g.,
5,3.14,'A',"Hello". - Symbolic Constants: A name given to a literal constant.
- Using `const` keyword:
const float PI = 3.14159; - Using `#define` preprocessor:
#define PI 3.14159
- Using `const` keyword:
Declaration Statement
A 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;
Arithmetic Expression and Statement
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 / 2is2).%(Modulus) - Gives the remainder of a division (e.g.,5 % 2is1).
Example Expression: (a + b) * 10
Example Statement: area = 0.5 * base * height;
Assignment Statement
The assignment statement uses the assignment operator (=) to store the value of an expression into a variable.
Syntax: variable_name = expression;
- The expression on the right is evaluated first.
- The resulting value is then stored in the variable on the left.
Examples: int x; x = 10; // Assigns 10 to x x = x + 5; // Assigns 15 to x
Input-Output (I/O) Statements
To perform I/O, you must first include the standard input/output library: #include
printf() - Formatted Output
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.
%dor%i: for integers (int)%f: forfloat%lf: fordouble(long float)%c: forchar%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
scanf() - Formatted Input
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);
Example Program (Unit 1)
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; } Unit 1: Exam Quick Tips
- The
&in `scanf`:** This is the most critical point.scanf("%d", &num)is correct.scanf("%d", num)is wrong and will crash your program. - Format Specifiers: You must match the specifier to the data type. Use
%dforint,%fforfloat,%lffordouble, and%cforchar. - Semicolons: Every C statement (declarations, assignments, function calls) must end with a semicolon (`;`).
- Integer Division: Be careful!
5 / 2is2. If you want2.5, you must use floats:5.0 / 2.0.