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:

C Characters

The C character set is the set of valid characters that can be used to write C programs.

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:

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:

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;

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.

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, ...);

CRITICAL: You must use the ampersand & (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