1.
Data Types
C has several data types that can be categorized into four main groups:
1. Basic Data Types: int, float, double, char
- int: Integer data type (e.g., int age = 25;)
- float: Single-precision floating-point (e.g., float height = 5.7;)
- double: Double-precision floating-point (e.g., double pi = 3.14159;)
- char: Character data type (e.g., char initial = 'A';)
2. Derived Data Types: arrays, pointers, structures, unions
- Arrays: Collection of elements of the same type (e.g., int nums[10];)
- Pointers: Variables that store memory addresses
- Structures: Custom data types grouping variables of different types
3. Enumeration Data Types
- User-defined types with named integer constants (e.g., enum Day {Mon, Tue, Wed};)
4. Void Type
- Represents absence of data, used in functions returning no value
2. Variables
Variables in C are used to store data, and each variable must be declared with a specific data type.
- Declaration: Specifies the data type and variable name (e.g., int num;)
- Initialization: Assigns a value at the time of declaration (e.g., int num = 10;)
Rules for naming variables:
- Must start with a letter or underscore (_)
- Can contain letters, digits, and underscores
- Case-sensitive (num and Num are different)
Types of variables:
- Local Variables: Declared inside a function, accessible only within that function
- Global Variables: Declared outside all functions, accessible throughout the program
- Static Variables: Retain their value between function calls
- Extern Variables: Declared with the 'extern' keyword, used to refer to a global variable
3. Operations, Expressions, and Statements
Operators in C perform various operations on variables and values.
1. Arithmetic Operators: +, -, *, /, %
2. Relational Operators: ==, !=, >, <, >=, <=
3. Logical Operators: &&, ||, !
4. Bitwise Operators: &, |, ^, ~, <<, >>
5. Assignment Operators: =, +=, -=, *=, /=
6. Miscellaneous Operators: sizeof, &, *, ?:
Expressions are combinations of variables, constants, and operators.
Statements can be classified as:
- Expression Statements (e.g., a = b + c;)
- Compound Statements (block of statements enclosed in braces)
- Selection Statements (if, if-else, switch)
- Iteration Statements (while, for, do-while)
4. Conditional Statements
Conditional statements control the flow of execution based on conditions.
1. if Statement
- Syntax: if (condition) { // code }
2. if-else Statement
- Syntax: if (condition) { // code } else { // code }
3. else-if Ladder
- Used for multiple conditions
4. switch Case
- Used for selecting among multiple options
- Syntax: switch (expression) { case constant1: // code; break; default: // code; }
5. Functions
Functions are blocks of code that perform specific tasks.
- Function Declaration: Specifies the function's name, return type, and parameters
- Function Definition: Contains the code for the function
- Function Call: Executes the function
Types of Functions:
- Predefined Functions (e.g., printf, scanf)
- User-Defined Functions
Example:
int add(int a, int b) {
return a + b;
}
6. Recursive Functions
A recursive function is a function that calls itself.
- Useful for tasks like factorial calculation, Fibonacci series, etc.
Example:
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
7. Arrays (Single-Dimensional and Multi-Dimensional)
Arrays are collections of data of the same type.
1. Single-Dimensional Arrays
- Declaration: data_type array_name[size];
- Example: int numbers[5] = {1, 2, 3, 4, 5};
2. Multi-Dimensional Arrays
- Example: int matrix[3][3];
Accessing elements:
- Single-dimensional: array[index]
- Multi-dimensional: array[row][column]