C programming language
SAGAR PRAVEEN
1. Introduction to C Programming
What is C?
History and Evolution of C
Why Learn C?
2. Setting Up the Environment
Installing a C Compiler
Setting Up an IDE
Writing Your First C Program
3. Basics of C Programming
Syntax and Structure
Variables and Data Types
Operators and Expressions
4. Control Structures
Conditional Statements (if, else, switch)
Loops (for, while, do-while)
5. Functions
Defining and Calling Functions
Function Parameters and Return Values
Scope and Lifetime of Variables
6. Arrays and Strings
Introduction to Arrays
String Handling Functions
Multidimensional Arrays
7. Pointers
Understanding Pointers
Pointer Arithmetic
Pointers and Arrays
8. Structures and Unions
Defining Structures
Using Structures
Unions in C
9. File Handling
Reading from and Writing to Files
File Operations
Error Handling in File I/O
10. Advanced Topics
Dynamic Memory Allocation
Preprocessor Directives
Linked Lists
11. Debugging and Optimization
Common Errors and Debugging Techniques
Optimizing C Code
12. Projects and Exercises
Simple Projects to Reinforce Learning
Practice Exercises with Solutions
PAGE 1
Introduction to C Programming
WHAT IS C?
PAGE 2
C is a high-level and general-purpose programming language that was
initially developed by Dennis Ritchie between 1969 and 1973 at Bell Labs. It
was created to develop the Unix operating system.
DEFINITION:
C is a structured, procedural programming language that offers low-
level access to memory, a simple set of keywords, and a clean style. It
is designed for efficiency in mapping constructs to machine
instructions, making it a particularly good choice for system
programming - writing operating systems - and for developing
application software.
HISTORY AND EVOLUTION OF C
BIRTH OF C (1970S)
Creation: C was developed by Dennis Ritchie at Bell Labs in the early 1970s.
It was initially created to implement the Unix operating system.
Influences: C was influenced by earlier programming languages, particularly
B and BCPL. Ritchie’s goal was to improve upon these languages to support
cross-platform system programming.
KEY MILESTONES
1972: The first version of C was completed and used to rewrite Unix, which
was originally written in assembly language.
1978: Brian Kernighan and Dennis Ritchie published "The C Programming
Language," which is also known as “K&R C.” This book became the
definitive guide and greatly influenced the language’s popularity.
MODERN C (2000S AND BEYOND)
1999: The C99 standard was introduced, adding new features such as inline
functions, variable-length arrays, and new data types.
PAGE 3
2011: The C11 standard brought enhancements like improved Unicode
support, multithreading capabilities, and additional standard library
functions.
2018: The C18 standard, a minor revision, included bug fixes and
clarifications to the C11 standard.
IMPACT AND LEGACY
C has profoundly impacted the field of computer science and programming:
Foundation for Other Languages: Many modern programming languages,
such as C++, C#, and Java, are based on or influenced by C.
Wide Usage: C is still widely used in system programming, embedded
systems, and applications requiring high performance.
WHY LEARN C?
Learning C is a wonderful choice for anyone interested in programming.
Being a base language, C provides the best basis for understanding even
more modern languages like C++, C#, and Java, so it will not be too hard to
learn them in the future. Efficiency and control over system resources are
known features of the C language, which allows the programmer to write
high-performance code directly interacting with the hardware. This low-level
access is critical for system programming and development of embedded
systems. Also, the syntax of C is clean and simple, which helps to master the
complex programming concepts like memory management and pointers.
Since it is portable, C code can run on almost any platform, which
makes it suitable for cross-platform development. Also, knowledge of C
provides numerous career opportunities, especially in high-performance
computing and system-level programming industries. It has a great
community and tons of material so learning C is very rewarding as well as
valuable for any budding programmer.
PAGE 4
Basics of C Programming
C programming is a versatile and powerful language that is widely used in
various applications. Here’s a brief overview of its key uses:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
OUTPUT:
Hi! This is a basic C program.
#include <stdio.h>: This line includes the standard input-output library,
which is necessary for using the printf function.
int main(): This is the main function, where the program execution begins.
printf("Hello, World!\n");: This line prints "Hello, World!" to the console.
The \n is a newline character that moves the cursor to the next line.
return 0;: This statement terminates the main function and returns 0 to the
operating system, indicating that the program ended successfully.
PAGE 5
VARIABLES AND DATA TYPES
VARIABLES:
A variable in C is a memory location with some name that helps store some
form of data and retrieves it when required. We can store different types of
data in the variable and reuse the same variable for storing some other data
any number of times.
Syntax:
data_type variable_name;
Example:
int age;
float salary;
char grade;
Data Types in C
Data Types
in C
Primitive User defined
Data Types Derived typs data typs
PAGE 6
Primitive Data Types
Primitive data types in C are the basic building blocks for data manipulation.
They are predefined by the language and serve as the foundation for
creating more complex data types. Here are the primary primitive data types
in C:
1. int: Used to store integer values.
o Example: int age = 25;
2. char: Used to store a single character.
o Example: char grade = 'A';
3. float: Used to store single-precision floating-point numbers.
o Example: float salary = 8500.50;
4. double: Used to store double-precision floating-point numbers for
more precision.
o Example: double pi = 3.14159265359;
5. void: Represents the absence of type. It's used in function
declarations to indicate that a function doesn't return a value.
o Example: void functionName();
These primitive types are integral to the C programming language and form
the basis for more complex data structures like arrays, structures, and
unions. Understanding these types is essential for writing effective and
efficient C programs.
PAGE 7
Derived data types
Derived data types in C are built from the basic primitive data types to
create more complex data structures. They allow programmers to handle
multiple related data items as a single unit. Here are the primary derived
data types in C:
1. Arrays
Description: A collection of elements of the same type stored in
contiguous memory locations.
Example:
int numbers[10]; // An array of 10 integers
char name[50]; // An array of 50 characters (a string)
2. Pointers
Description: Variables that store the memory address of another
variable.
Example:
int *ptr; // A pointer to an integer
ptr = &var; // Storing the address of variable 'var' in pointer 'ptr'
3. Structures
Description: A user-defined data type that groups different data
types into a single unit.
Example:
struct Person {
char name[50];
PAGE 8
int age;
float salary;
};
4. Unions
Description: Similar to structures, but all members share the same
memory location.
Example:
union Data {
int i;
float f;
char str[20];
};
5. Enumerations (enum)
Description: A user-defined data type that consists of integral
constants, making code more readable.
Example:
enum Weekday { Sunday, Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday };
6. Functions
Description: A block of code that performs a specific task and can be
called from other parts of the program.
Example:
int add(int a, int b) {
return a + b;
}
PAGE 9
These derived data types enhance the functionality and flexibility of the C
programming language, allowing for more sophisticated and organized
coding practices.
userdefined data types
User-defined data types in C are types that a programmer creates to
represent more complex data structures. These allow you to group related
data together, making your programs more readable and manageable. Here
are the primary user-defined data types in C:
1. Structures
Description: Structures (struct) allow you to group variables of
different data types under a single name. This is useful for organizing
related data.
Example:
struct Person {
char name[50];
int age;
float height;
};
2. Unions
Description: Unions (union) are similar to structures, but they store
different data types in the same memory location, so they can only
hold one of the defined types at a time.
Example:
union Data {
int i;
float f;
char str[20];
PAGE 10
};
3. Enumerations (enum)
Description: Enumerations (enum) allow you to define a set of named
integer constants, which can make your code more readable and
maintainable.
Example:
enum Weekday { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday };
4. Typedef
Description: The typedef keyword allows you to create new names
(aliases) for existing data types, which can simplify your code and
make it easier to understand.
Example:
typedef unsigned long ulong;
ulong distance;
5. Bit Fields
Description: Bit fields within structures are used to allocate a
specific number of bits to a variable, which is useful in situations
where memory needs to be used efficiently, like in embedded systems.
Example:
struct {
unsigned int age : 3;
} Age;
Usage in Program:
Struct Example:
PAGE 11
#include <stdio.h>
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Person person1;
person1.age = 30;
person1.height = 5.9;
strcpy(person1.name, "John Doe");
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);
return 0;
Enum Example:
#include <stdio.h>
enum Weekday { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday };
PAGE 12
int main() {
enum Weekday today;
today = Wednesday;
printf("Day %d\n", today + 1); // Outputs: Day 4
return 0;
User-defined data types in C provide powerful tools for managing and
organizing complex data.
PAGE 13