Skip to content

Purpose of C

Dennis Ritchie developed C to make system software (like operating systems). UNIX is one of the first operating systems written in C, which made it portable and efficient.

Features of C

C became popular because it:

  1. Combined the efficiency of assembly language with the simplicity of a high-level language.
  2. Allowed writing programs that can run on different computers portable.
  3. Became a foundation for many modern languages, such as C++, Java, and Python.

Versions of C

VersionYearDescription
K&R C1978First official version (by Dennis Ritchie).
ANSI C1989Standardized the language.
C89 / C901989–1990International Standard (ANSI: American National Standards Institute)
C991999Added new features like inline functions, variable-length arrays
C112011Added multi-threading support
C17 / C182017–2018Minor updates & fixes

Why C is Important?

  1. It's called the “Mother of Programming Languages.
  2. Many modern languages like C++, Java, C#, and Python were built on concepts of C.
  3. It's still used in:
  • System programming
    • Embedded programming
    • Compilers
    • Operating system development.
Summary
**Developed by:** Dennis Ritchie **Year:** 1972 **Place:** Bell Laboratories (A&T) **Purpose:** To develop the UNIX OS **Derived from:** B & BCPL **Known as:** ==Mother of all modern programming languages==

Purpose of Flowcharts

  1. To understand the logic of a program easily
  2. To plan a program before writing the actual code
  3. To debug/explain the process to others

Features of C

  1. Simple and easy C uses simple syntax Easy to learn commands
  2. Portable C programs can run on different computers without change
  3. Structured Languages A program can be divided into functions/blocks.
  4. Rich Library C has many built-in functions like printf() and scanf().
  5. Middle-Level Language Supports both low-level and high-level programming.
  6. Modular Large programs can be broken into smaller functions.

Tokens In C

Tokens are the smallest building blocks of a C program. They are like words in a language.

Types of Tokens

  1. Keywords
  2. Identifiers
  3. Constants
  4. Operators
  5. Strings
  6. Special Symbols

1 Keywords

Keywords are reserved words with special meaning in C.

Rules

  1. You cannot use them as variable names.
  2. They tell the compiler what to do.

Examples: int, float, if, else, for, while, return, char, const, void, switch.

2 Identifiers

Identifiers are names given by the programmer to things like:

  • variables
  • functions
  • arrays
  • structures, etc...

Purpose: They help identify data and functions in a program.

3 Constants

Constants are values that do not change during program execution.

Types of Constants

TypeExample
Integer Const10, -5, 2, 3
Floating Const5.55, 7.25, 9.24
Char Const'A', 'B', 'C'
String Const"My Name"

4. Operators in C

An operator is a symbol that tells the compiler to perform an operation such as addition, comparison, assignment (+, <, =.) etc...

Types of Operators:

  1. Arithmetic Operators (+, -, *, /, %).
  2. Relational Operators (==, !=, >, <, >=).
  3. Logical Operators (&&, !).
  4. Assignment Operators (=, +=, -=, *=, /=)
  5. Increment & Decrement Operators (i++, --i)
  6. Ternary Operator (? 😃

1. Arithmetic Operators

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)

2. Relational Operators

OperatorMeaning
==Equal to
!=Not equal
>Greater than
<Less than
>=Greater than or equal

3. Logical Operators

OperatorMeaning / Example
&&Logical AND → (a > 0 && b > 0)
!Logical NOT → !(a > 0)

4. Assignment Operators

OperatorMeaning / Example
=Assign value → a = 10
+=Add & assign → a += 5
-=Subtract & assign → a -= 5
*=Multiply & assign → a *= 5
/=Divide & assign → a /= 5

5. Increment & Decrement Operators

FormMeaning
i++Post-increment → Use first, then add 1
++iPre-increment → Add 1 first, then use
i--Post-decrement → Use first, then subtract 1
--iPre-decrement → Subtract 1, then use

6. Ternary Operator

Symbol: ? : It can be used instead of a simple if–else.

Example:

c
a = 5;
b = 6;
a > b ? a : b;

Output:

6

5. Strings

A string is a sequence of characters enclosed in double quotes.

Properties:

  • Always ends with a null character \0 internally
  • Stored as an array of characters

Correct Example:

c
char name[] = "c";
printf("%s", name);

Output:

c

Fixed: strings must use double quotes, not single quotes.*

6. Special Symbols

SymbolMeaning
;End of statement
{ }Start & end of block / function
( )Used in functions & conditions
[ ]Arrays
,Separator between variables
#Preprocessor directive (#include)
' 'Character constant
" "String constant
\Escape character

Variables in C

A variable is a named memory location used to store data that can change.

Why Variables Are Needed? Variables store data during program execution. A variable is a container that stores a value.

Variables are needed to:

  • Store input data
  • Perform calculations
  • Store program results
  • Make code readable and reusable

Variable Declaration

Tells the compiler what type of data the variable will store.

Example:

c
int a;
float b;
char c;

Variable Initialization

Assigning a value to a variable.

Example:

c
a = 10;
b = 10.5;
c = 'M';
stockerid = "SSzxxc1";

Rules for Naming Variables

Must start with a letter (a–z or A–Z) or underscore (_)

  • 1a = 10; → ❌ Invalid
  • A = 10; → ✔️ Valid
  • _a = 10; → ✔️ Valid
  • a = 10; → ✔️ Valid

Examples of valid identifiers:

c
int age;
int _height;

Can contain only:

  • letters
  • digits
  • underscores _

Examples:

ValidInvalid
int total_marks;int total-mark;
int sum1;int price#;

No spaces allowed

ValidInvalid
int total_marks;int total marks;

Cannot use keywords: Examples: int, float, if, else cannot be used as variable names

Case Sensitive C is a case-sensitive language, meaning uppercase and lowercase letters are treated as different variables.

Example:

c
int age = 10;
Age = 25;
printf("%d", age);   // Output: 10
Note

Here, age and Age are different.

No Special Characters Allowed (Except Underscore _)

You cannot use characters like:

  • @
  • %
  • -
  • #
  • $

Examples:

ValidInvalid
total_markstotal-marks
sum1sum@

Length of Variable Name There is no strict limit, but most compilers recognize only the first 31 characters.

Meaningful and Readable Names Use meaningful names. Avoid:

c
int a, b, c;   // Meaningless

Data Types

Data types define:

  • what kind of data a variable can store
  • how much memory it occupies

1 Primitive Data Types

Data TypeSize (bytes)Format SpecifierExample
int2 or 4%dint a = 10;
float4%ffloat b = 10.5;
double8%lfdouble d = 8.54;
char1%cchar c = 'M';

2. Derived Data Types

Derived TypeDefinitionExample
1. ArrayGroup of same-type elementsc int a[5]; a = {1, 2, 3, 4, 5};
2. PointerA variable that stores the address of another variablec int *p;
3. StructureGroup of different data typesc struct Student { ... };
4. UnionShares the same memory locationc union info { ... };
5. FunctionReturns a value after executionc int add(int a, int b) { ... }

3. User-Defined Data Types

KeywordDefinitionExample
typedefGives a new name to an existing typec typedef int num;
enumDefines a set of named constantsc enum day { Mon, Tue, Wed };

Data Type Modifiers Used to change the size or range of basic data types:

ModifierUsed WithExample
shortintshort int a;
longint, doublelong int b;
signedint, charsigned char c;
unsignedint, charunsigned char c;

Structure of a C Program

A C program is divided into 6 sections:

  1. Documentation Section
  2. Link Section
  3. Definition Section
  4. Global Declaration Section
  5. main() Function Section
  6. Sub-program Section (User-defined functions)

main() Function Section

c
{
    Declaration Part
    Executable Part
}


// * The **declaration part** contains variable declarations
// * The **executable part** contains statements to be executed



// Subprogram Section
// Contains user-defined functions:


Function 1
Function 2

Function n

Documentation Section

  • This part contains information about the program such as name, purpose, author, and date.
  • It is written as comments, and the compiler ignores it.

Preprocessor Section

  • This section includes header files using the #include statement.
  • Header files contain predefined functions like printf() and scanf().

Syntax

c#
#include <stdio.h>   // stdio → standard input/output
#include <math.h>    // math.h → mathematical functions

Definition Section

  • Used to define constants or macros before the main program.
  • #define keyword is used for constant definitions.

Example:

c
#define PI 3.14
Note
Whenever the compiler finds `PI`, it replaces it with `3.14`.

Global Declaration Section

  • Variables or functions declared here can be used anywhere in the program.
  • They are defined outside the main() function.

Example:

c
int num = 10;

main() Function Section

  • This is the starting point of every C program.
  • The compiler starts execution from the main() function.
  • The main function can return a value (usually int).

Syntax

c
int main()
{
    // variable declaration
    // logic / process
    
    return 0;
}
Note
Everything that happens in a C program runs inside the `{}` of the **main()** function.

Subprograms (User-Defined Functions)

  • These are functions created by the programmer to perform specific tasks.
  • They make programs modular and easier to reuse.

I/O Statements in C

In C, input and output are done using certain functions. There are 2 types:

  1. Formatted I/O
  2. Unformatted I/O

Formatted I/O

These allow formatted input and output using format specifiers. 1) printf() → OutputSyntax:

c
printf("%d", a);

Common Format Specifiers

Format SpecifierMeaning
%dint
%ffloat
%sstring
%cchar
%lfdouble

Example (double output)

c
int a = 10;
printf("%d", a);

2) scanf() → Input

Syntax

c
scanf("format", &variable_name);

Example

c
int a;
scanf("%d", &a);

© 2025 Notes.Tamim's.Space