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:
- Combined the efficiency of assembly language with the simplicity of a high-level language.
- Allowed writing programs that can run on different computers portable.
- Became a foundation for many modern languages, such as C++, Java, and Python.
Versions of C
| Version | Year | Description |
|---|---|---|
| K&R C | 1978 | First official version (by Dennis Ritchie). |
| ANSI C | 1989 | Standardized the language. |
| C89 / C90 | 1989–1990 | International Standard (ANSI: American National Standards Institute) |
| C99 | 1999 | Added new features like inline functions, variable-length arrays |
| C11 | 2011 | Added multi-threading support |
| C17 / C18 | 2017–2018 | Minor updates & fixes |
Why C is Important?
- It's called the “Mother of Programming Languages.”
- Many modern languages like C++, Java, C#, and Python were built on concepts of C.
- It's still used in:
- System programming
- Embedded programming
- Compilers
- Operating system development.
Purpose of Flowcharts
- To understand the logic of a program easily
- To plan a program before writing the actual code
- To debug/explain the process to others
Features of C
- Simple and easy C uses simple syntax Easy to learn commands
- Portable C programs can run on different computers without change
- Structured Languages A program can be divided into functions/blocks.
- Rich Library C has many built-in functions like
printf()andscanf(). - Middle-Level Language Supports both low-level and high-level programming.
- 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
- Keywords
- Identifiers
- Constants
- Operators
- Strings
- Special Symbols
1 Keywords
Keywords are reserved words with special meaning in C.
Rules
- You cannot use them as variable names.
- 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
| Type | Example |
|---|---|
| Integer Const | 10, -5, 2, 3 |
| Floating Const | 5.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:
- Arithmetic Operators (+, -, *, /, %).
- Relational Operators (==, !=, >, <, >=).
- Logical Operators (&&, !).
- Assignment Operators (=, +=, -=, *=, /=)
- Increment & Decrement Operators (i++, --i)
- Ternary Operator (? 😃
1. Arithmetic Operators
| Operator | Meaning |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
2. Relational Operators
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
3. Logical Operators
| Operator | Meaning / Example |
|---|---|
&& | Logical AND → (a > 0 && b > 0) |
! | Logical NOT → !(a > 0) |
4. Assignment Operators
| Operator | Meaning / 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
| Form | Meaning |
|---|---|
i++ | Post-increment → Use first, then add 1 |
++i | Pre-increment → Add 1 first, then use |
i-- | Post-decrement → Use first, then subtract 1 |
--i | Pre-decrement → Subtract 1, then use |
6. Ternary Operator
Symbol: ? : It can be used instead of a simple if–else.
Example:
a = 5;
b = 6;
a > b ? a : b;Output:
65. Strings
A string is a sequence of characters enclosed in double quotes.
Properties:
- Always ends with a null character
\0internally - Stored as an array of characters
Correct Example:
char name[] = "c";
printf("%s", name);Output:
cFixed: strings must use double quotes, not single quotes.*
6. Special Symbols
| Symbol | Meaning |
|---|---|
; | 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:
int a;
float b;
char c;Variable Initialization
Assigning a value to a variable.
Example:
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;→ ❌ InvalidA = 10;→ ✔️ Valid_a = 10;→ ✔️ Valida = 10;→ ✔️ Valid
Examples of valid identifiers:
int age;
int _height;Can contain only:
- letters
- digits
- underscores
_
Examples:
| Valid | Invalid |
|---|---|
int total_marks; | int total-mark; |
int sum1; | int price#; |
No spaces allowed
| Valid | Invalid |
|---|---|
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:
int age = 10;
Age = 25;
printf("%d", age); // Output: 10Here, age and Age are different.
No Special Characters Allowed (Except Underscore _)
You cannot use characters like:
@%-#$
Examples:
| Valid | Invalid |
|---|---|
total_marks | total-marks |
sum1 | sum@ |
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:
int a, b, c; // MeaninglessData Types
Data types define:
- what kind of data a variable can store
- how much memory it occupies
1 Primitive Data Types
| Data Type | Size (bytes) | Format Specifier | Example |
|---|---|---|---|
| int | 2 or 4 | %d | int a = 10; |
| float | 4 | %f | float b = 10.5; |
| double | 8 | %lf | double d = 8.54; |
| char | 1 | %c | char c = 'M'; |
2. Derived Data Types
| Derived Type | Definition | Example |
|---|---|---|
| 1. Array | Group of same-type elements | c int a[5]; a = {1, 2, 3, 4, 5}; |
| 2. Pointer | A variable that stores the address of another variable | c int *p; |
| 3. Structure | Group of different data types | c struct Student { ... }; |
| 4. Union | Shares the same memory location | c union info { ... }; |
| 5. Function | Returns a value after execution | c int add(int a, int b) { ... } |
3. User-Defined Data Types
| Keyword | Definition | Example |
|---|---|---|
| typedef | Gives a new name to an existing type | c typedef int num; |
| enum | Defines a set of named constants | c enum day { Mon, Tue, Wed }; |
Data Type Modifiers Used to change the size or range of basic data types:
| Modifier | Used With | Example |
|---|---|---|
| short | int | short int a; |
| long | int, double | long int b; |
| signed | int, char | signed char c; |
| unsigned | int, char | unsigned char c; |
Structure of a C Program
A C program is divided into 6 sections:
- Documentation Section
- Link Section
- Definition Section
- Global Declaration Section
- main() Function Section
- Sub-program Section (User-defined functions)
main() Function Section
{
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 nDocumentation 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()andscanf().
Syntax
#include <stdio.h> // stdio → standard input/output
#include <math.h> // math.h → mathematical functionsDefinition Section
- Used to define constants or macros before the main program.
#definekeyword is used for constant definitions.
Example:
#define PI 3.14Global Declaration Section
- Variables or functions declared here can be used anywhere in the program.
- They are defined outside the main() function.
Example:
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
int main()
{
// variable declaration
// logic / process
return 0;
}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:
- Formatted I/O
- Unformatted I/O
Formatted I/O
These allow formatted input and output using format specifiers. 1) printf() → OutputSyntax:
printf("%d", a);Common Format Specifiers
| Format Specifier | Meaning |
|---|---|
%d | int |
%f | float |
%s | string |
%c | char |
%lf | double |
Example (double output)
int a = 10;
printf("%d", a);2) scanf() → Input
Syntax
scanf("format", &variable_name);Example
int a;
scanf("%d", &a);