Introduction To C Language
Introduction to c language
๐งพ History of C Language
C Language was developed in the early 1970s by Dennis Ritchie at Bell Laboratories (AT&T). It was created as an evolution of the B language, and it was initially used to develop the UNIX operating system.
Milestones:
- 1972: C was developed
- 1978: "The C Programming Language" book by Brian Kernighan and Dennis Ritchie was published
- 1989: ANSI standardized C as "ANSI C"
- Later: ISO standards followed, leading to C90, C99, C11, and C18
Why It Matters: C is considered the foundation of many modern programming languages like C++, Java, and Python due to its speed, control, and portability.
๐️ Structure of a C Program
A C program follows a specific and well-organized structure. Understanding its parts helps in writing, debugging, and managing code easily.
Typical Structure Includes:
- Header Files Inclusion: Using
#include
to import libraries likestdio.h
,math.h
- Global Declarations: Variables, constants, or functions defined outside
main()
- Main Function: Starting point of execution —
int main()
- Variable Declaration: Inside functions to define local data
- Executable Statements: The actual logic and instructions
- Functions: Optional — reusable blocks of code
Example:
#include <stdio.h> int main() { printf("Hello, World!"); return 0; }
๐งฉ Functions as Building Blocks
Functions are self-contained blocks of code designed to perform a specific task. In C, they help in modularizing the program.
Advantages of Functions:
- Code reusability
- Improved readability and maintainability
- Debugging and testing becomes easier
- Supports top-down programming
Types of Functions:
- Library Functions: Predefined (e.g.,
printf()
,sqrt()
) - User-defined Functions: Created by the programmer
Function Syntax:
return_type function_name(parameters) { // code }
Example:
int add(int a, int b) { return a + b; }
language fundamentals
๐ค Character Set
Character set in C includes all valid characters that can be used in programs. It includes:
- Letters: A–Z, a–z
- Digits: 0–9
- Special Characters: +, -, *, /, %, =, &, |, <,>, ^, etc.
- Whitespace: space, tab, newline
๐น C Tokens
Tokens are the smallest individual units in a C program. These are:
- Keywords
- Identifiers
- Constants
- Strings
- Operators
- Special Symbols
๐ Keywords
Keywords are reserved words that have predefined meanings in C. They cannot be used as identifiers (e.g., variable names).
Examples: int
, return
, if
, while
,
for
, void
, char
C has 32 standard keywords.
๐ Identifiers
Identifiers are the names given to variables, functions, arrays, etc., by the programmer.
Rules:
- Must begin with a letter (A–Z or a–z) or underscore (_)
- Cannot be a keyword
- Can contain letters, digits, and underscores
- Case-sensitive
๐ฆ Variables
Variables are named memory locations used to store data that can change during program execution.
Syntax: data_type variable_name;
Example: int age;
, float salary;
๐ Constants
Constants are fixed values that do not change during program execution.
Types of Constants:
- Integer constants: e.g., 10, -25
- Floating-point constants: e.g., 3.14, -0.5
- Character constants: e.g., 'A', '3'
- String constants: e.g., "Hello", "123"
Defining constant: #define PI 3.14
or
const int x = 10;
๐ Data Types
Data types define the type of data a variable can hold in C.
Primary Data Types:
int
– Integer values (e.g., 5, -3)float
– Floating-point numbers (e.g., 3.14)char
– Single character (e.g., 'A')double
– Double-precision float
Derived Types: Arrays, Pointers, Structures, Unions
๐ฌ Comments
Comments are non-executable statements used to explain code. They help improve code readability.
Types of Comments:
- Single-line:
// This is a comment
- Multi-line:
/* This is a multi-line comment */
Comments
Post a Comment
write your complements and complaints :)