Introduction to ‘C’ Language
Background
● Origin:
○ Developed in the early 1970s by Dennis Ritchie at Bell Labs.
○ Derived from the B programming language.
● Features:
○ General-purpose programming language.
○ Procedural and structured.
○ Provides low-level access to memory.
○ Widely used for system programming, embedded systems, and software development.
● Advantages:
○ Portability.
○ Efficient performance.
○ Rich set of built-in functions.
○ Modular programming support via functions.
C Programs
● Definition: A program written in the C language consists of one or more functions, with the main()
function being mandatory.
Structure of a C Program:
Copy code
#include <stdio.h> // Preprocessor directive
int main() { // Main function
printf("Hello, World!\n"); // Output statement
return 0; // Indicates successful execution
}
○ Preprocessor Directives: Begin with # and include libraries like <stdio.h>.
○ Functions: Blocks of code that perform specific tasks.
Identifiers
● Definition: Names used to identify variables, functions, or other user-defined elements.
● Rules:
○ Must start with a letter or underscore (_).
○ Can contain letters, digits, and underscores.
○ Case-sensitive.
○ Reserved keywords (like int, return) cannot be used as identifiers.
Data Types
● Definition: Specify the type of data a variable can hold.
● Categories:
1. Basic Types:
■ int: Integer (e.g., 10, -5).
■ float: Floating-point (e.g., 3.14).
■ char: Single character (e.g., 'A').
■ double: Double-precision floating-point (e.g., 3.141592).
2. Derived Types:
■ Arrays, pointers, functions, structures.
3. Void Type:
■ Represents no value or return type (e.g., void main()).
Variables
● Definition: Named storage locations for data that can be modified during program execution.
Declaration:
Copy code
int age; // Declares an integer variable
float price; // Declares a floating-point variable
Initialization:
Copy code
int age = 19; // Declare and initialize
Constants
● Definition: Values that do not change during program execution.
● Types:
○ Numeric Constants: 10, 3.14.
○ Character Constants: 'A', 'z'.
○ String Constants: "Hello".
Defined Constants: Use #define.
Copy code
#define PI 3.14
Input/Output Statements
Input: Use scanf() to read data.
Copy code
int age;
printf("Enter your age: ");
scanf("%d", &age);
Output: Use printf() to display data.
Copy code
printf("Your age is %d", age);
Arithmetic Operators and Expressions
Arithmetic Operators
● Definition: Operators used for basic mathematical operations.
● List:
○ + (Addition): a + b
○ - (Subtraction): a - b
○ * (Multiplication): a * b
○ / (Division): a / b
○ % (Modulus): a % b (remainder of integer division)
Expressions
● Definition: A combination of variables, constants, and operators that are evaluated to produce a value.
Example:
Copy code
int result = (10 + 5) * 2; // Result is 30
Evaluating Expressions
● Steps:
1. Compute values inside parentheses first.
2. Follow operator precedence and associativity.
Precedence and Associativity of Operators
● Precedence: Determines the order in which operators are evaluated.
○ Higher precedence operators are evaluated before lower precedence ones.
○ Example: * has higher precedence than +.
● Associativity: Determines the direction of evaluation for operators of the same precedence.
○ Most operators are left-to-right associative.
○ Example: a - b + c is evaluated as (a - b) + c.
Type Conversions
● Implicit Conversion (Type Promotion):
○ Automatically performed by the compiler.
Example: int is promoted to float in mixed operations.
Copy code
int x = 5;
float y = x + 2.5; // x is converted to 5.0
● Explicit Conversion (Type Casting):
○ Performed by the programmer using type casting.
Copy code
float result = (float)5 / 2; // Result is 2.5