Introduction To C Programming
Introduction To C Programming
C is a powerful general-purpose programming language developed in the early 1970s by Dennis Ritchie at
Bell Laboratories. Known for its efficiency, simplicity, and flexibility, C is widely used in various fields,
from operating systems to embedded systems. It is considered a foundational language in computer science,
providing a stepping stone for learning other programming languages and understanding computer
architecture.
C allows for low-level memory manipulation, making it suitable for developing operating systems and
complex software. Its portability enables programs written in C to be compiled and run on different types of
computer systems with little to no modification.
Efficiency: C is known for its speed and efficiency, making it ideal for performance-intensive applications.
Modularity: C supports modular programming through functions, which helps in organizing code and
reusing functionality.
Flexibility: C allows for both high-level programming constructs and low-level access to memory.
Rich Library: The C Standard Library offers numerous built-in functions for common tasks.
History of C Language:
The C language was developed as an enhancement to the B programming language. Dennis Ritchie and
Brian Kernighan improved on B by adding structures, data types, and richer functionality. This resulted in
the C language, which became popular with the development of the Unix operating system, as C was used to
implement much of Unix. The language was later standardized by ANSI in 1989, creating ANSI C, which
became widely adopted.
Over the years, various standards for C have been introduced, such as C90, C99, C11, and C18, each
bringing improvements in language features, performance, and support for new programming paradigms.
A C program consists of one or more functions, with the main() function serving as the entry point. The
structure of a simple C program includes:
#include <stdio.h>
int main() {
printf(“Hello, World!”);
return 0;
}
Explanation:
#include <stdio.h>: This line includes the standard input-output library, which provides the printf function.
return 0;: This line indicates that the program has executed successfully.
Variables in C are placeholders for storing data. Each variable has a data type that defines the kind of data it
can hold, such as integers, floating-point numbers, or characters.
Float: Used for single-precision floating-point numbers (e.g., float price = 9.99;).
Variables must be declared before they are used. Initialization assigns an initial value to a variable. E.g.
Operators in C:
C supports various operators for performing arithmetic, logical, relational, and assignment operations.
Operators are essential for manipulating data and performing calculations in a program.
Types of Operators:
1. Arithmetic Operators: +, -, *, /, %
2. Relational Operators: ==, !=, >, <, >=, <=
3. Logical Operators: && (AND), || (OR), ! (NOT)
4. Assignment Operators: =, +=, -=, *=, /=
Example:
int a = 10;
int b = 20;
int sum = a + b;
Control Structures:
Control structures allow for the flow of execution to change based on certain conditions. The main types of
control structures are if statements, for loops, while loops, and do-while loops.
Example: If Statement
} else {
int i = 0;
While (i< 5) {
i++;
Functions in C:
Functions are blocks of code designed to perform specific tasks. They help break down complex problems
into smaller, manageable parts, improve code readability, and promote reusability.
#include <stdio.h>
// Function declaration
int main() {
int sum = add(5, 3);
return 0;
// Function definition
return a + b;
Arrays are used to store collections of data, while pointers are variables that store the memory address of
another variable.
Declaring an Array:
Declaring a Pointer:
Memory Management:
C provides functions for dynamic memory management, which allows allocating and deallocating memory
at runtime using malloc, calloc, and free.
Example-
#include <stdlib.h>
if (arr == NULL) {
// Use memory
Keywords:
In C, keywords are reserved words that have special meaning to the compiler. These words cannot be used
as variable names, function names, or any other identifiers in a C program. They are predefined by the
language and perform specific functions. Here are the keywords in C:
These keywords form the backbone of C syntax and are essential for writing C programs.
Strings:
In C, a string is a sequence of characters terminated by a special character called the null character (‘\0’).
This null character marks the end of the string, allowing functions to determine the length of the string by
looking for ‘\0’. Unlike some other programming languages, C does not have a specific data type for strings;
instead, strings are represented as arrays of characters.
In this case, C automatically adds the null character at the end of the string.
Since strings are arrays of characters, you can manipulate them using array notation and standard C library
functions, which are defined in <string.h>. Here are some commonly used string functions:
Example-
#include <stdio.h>
#include <string.h>
int main() {
return 0;
In C programming, input and output (I/O) operations allow interaction with the user or other systems, such
as reading data from the keyboard or displaying results on the screen. The most common methods for input
and output in C are through functions in the Standard Input/Output library, <stdio.h>.
The printf() function is used to display text and variables on the screen. It takes a format string as its first
argument, which can include text as well as format specifiers to indicate where variable values should be
displayed.
Syntax-
%d or %i – for integers
%c – for characters
%s – for strings
Example-
#include <stdio.h>
int main() {
return 0;
The scanf() function is used to read user input from the keyboard. Similar to printf(), scanf() uses format
specifiers to determine the type of data being read.
Important: When using scanf() with variables, you must use the address-of operator (&) to pass the memory
address of the variable, except for strings.
Syntax-
Example-
#include <stdio.h>
int main() {
int age;
float height;
char grade;
printf(“Enter your age: “);
scanf(“%d”, &age);
scanf(“%f”, &height);
return 0;
Important Points:
Whitespace Handling: scanf() stops reading when it encounters whitespace (like spaces, tabs, newlines), so
be careful with multiple inputs.
Buffering: The newline (‘\n’) from previous inputs can sometimes cause issues, especially with char input.
Adding a space before %c can help avoid this.
Error Handling: scanf() returns the number of items successfully read, so you can use it to check if the input
was correct.
Here’s a simple program that takes user input and displays it back to them.
#include <stdio.h>
int main() {
char name[50];
int age;
float salary;
scanf(“%d”, &age);
scanf(“%f”, &salary);
return 0;
}
Constants:
In C programming, constants are fixed values that do not change during the execution of a program. They
are defined with specific keywords or syntax, allowing programmers to use values that remain constant
throughout the code.
Types of Constants in C-
1. Literal Constants
2. Const Keyword Constants
3. #define Macro Constants
4. Enumerated Constants (using enum)
1. Literal Constants
Literal constants are direct values assigned in the code without using any variables or symbols. Examples
include:
Example:
The const keyword creates a read-only variable, meaning the value cannot be changed once it’s assigned.
This is useful when you need a variable to hold a fixed value that should not be modified.
Syntax-
Example-
In this example, maxStudents and pi are constants, so attempting to modify them later in the code will result
in a compiler error.
The #define preprocessor directive allows you to create symbolic constants, which are replaced by their
values during preprocessing before the actual compilation. #define does not consume memory for storage, as
it performs a text substitution.
Syntax-
Example-
#define MAX_STUDENTS 50
#define PI 3.14159
In this example, every occurrence of MAX_STUDENTS or PI in the code is replaced with 50 and 3.14159,
respectively, before the code is compiled.
An enum (enumeration) is a user-defined data type in C that consists of a set of named integer constants.
Enumerations make code more readable and manageable by using names instead of numbers.
Syntax-
Example-
In this example, each day is assigned an integer value starting from 0 (i.e., MONDAY = 0, TUESDAY = 1,
etc.).