M2 Introduction to ‘C’ Language
M2 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
}
Identifiers
Data Types
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
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);
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.
Type Conversions