C LANGUAGE NOTES
1. Overview of C
C is a general-purpose, high-level programming language that was developed by Dennis Ritchie at
Bell Labs in 1972. It is one of the most widely used programming languages, particularly for system
and application software development. C allows direct manipulation of hardware and system
resources, making it ideal for system-level programming such as operating systems, device drivers,
and embedded systems.
Key Features of C:
Portability: C programs can be run on different machines with minimal or no modification.
Efficiency: C provides low-level access to memory through pointers, making it highly efficient.
Rich Set of Operators: C provides a wide range of operators that allow for versatile
programming.
Structured Programming: C supports structured programming, which helps in making the
code more readable and maintainable.
Extensibility: You can add new features to C through functions and libraries.
2. Introduction & Features of C
C is a versatile and powerful programming language. It combines the features of high-level
programming languages with the low-level capabilities of assembly language. It was initially
developed to write system software but has since become widely used for application software as
well.
Features of C:
Simple Syntax: C has a simple, clean, and concise syntax, which makes it easy for
programmers to learn and write.
Procedural Language: C is procedural, which means that the code is executed step by step in
a sequential manner.
Rich Library Support: C has a vast collection of built-in functions and libraries that help in
efficient development.
Memory Management: C allows explicit memory management, enabling efficient use of
memory resources.
Pointers: C’s use of pointers enables developers to directly manipulate memory, which is
both powerful and efficient.
3. Structure of a C Program
A C program consists of several components that work together to produce the desired output. The
basic structure is:
c
Copy code
,#include <stdio.h> // Preprocessor directive
// Global declarations
int main() // main function, where program execution begins
{
// Local variables
// Statements
return 0; // Return statement, indicating successful program termination
}
Preprocessor Directives: These are used to include header files, such as #include <stdio.h>,
which gives access to standard input/output functions.
Global Declarations: Variables or functions that can be accessed throughout the program.
Main Function: This is the entry point of any C program. The main() function is mandatory,
and the program begins execution here.
Statements: The actual code or instructions that the program will execute.
4. Variables, Expressions, Identifiers, Keywords, Data Types, Constants
Variables: A variable is a named memory location used to store data. The data type of the
variable defines the type of data it can hold (e.g., int, float, char).
c
Copy code
int a = 10;
float b = 3.14;
Expressions: An expression is a combination of variables, constants, operators, and functions
that are evaluated to produce a value.
c
Copy code
int sum = a + b; // This is an expression
Identifiers: Identifiers are names used to identify variables, functions, arrays, or any other
user-defined item. They must begin with a letter (A-Z or a-z) or an underscore (_) and can
contain letters, digits, and underscores.
,c
Copy code
int myVariable = 5; // "myVariable" is an identifier
Keywords: Keywords are reserved words in C that have special meanings and cannot be used
as identifiers. Examples include int, return, if, while, for, etc.
Data Types: Data types define the type of data a variable can store. C has several basic data
types:
o int: Integer numbers (e.g., 5, -10)
o float: Floating-point numbers (e.g., 3.14)
o char: Single characters (e.g., 'a')
o double: Double precision floating-point numbers
Constants: Constants are fixed values that cannot be changed during the program's
execution. They can be defined using #define or const.
c
Copy code
#define PI 3.14 // Constant using #define
const int MAX = 100; // Constant using const
5. Operators and Expressions
In C, operators are symbols that perform operations on variables and values. They are used to
manipulate data and variables.
Arithmetic Operators: These operators perform mathematical calculations.
o + (Addition)
o - (Subtraction)
o * (Multiplication)
o / (Division)
o % (Modulo)
Example:
c
Copy code
int a = 10, b = 5;
int result = a + b; // result = 15
Logical Operators: Used for logical operations, often in conditions.
, o && (Logical AND)
o || (Logical OR)
o ! (Logical NOT)
Example:
c
Copy code
if (a > 5 && b < 10) {
// This condition is true
}
Relational Operators: Used to compare two values.
o == (Equal to)
o != (Not equal to)
o > (Greater than)
o < (Less than)
o >= (Greater than or equal to)
o <= (Less than or equal to)
Example:
c
Copy code
if (a == 10) {
// This condition is true
}
Conditional Operator (?:): This is a shorthand for if-else conditions.
c
Copy code
int result = (a > b) ? a : b; // result gets the value of a if true, otherwise b
Bitwise Operators: These operate on bits of integer data types.
o & (AND)
o | (OR)
o ^ (XOR)
o ~ (NOT)
1. Overview of C
C is a general-purpose, high-level programming language that was developed by Dennis Ritchie at
Bell Labs in 1972. It is one of the most widely used programming languages, particularly for system
and application software development. C allows direct manipulation of hardware and system
resources, making it ideal for system-level programming such as operating systems, device drivers,
and embedded systems.
Key Features of C:
Portability: C programs can be run on different machines with minimal or no modification.
Efficiency: C provides low-level access to memory through pointers, making it highly efficient.
Rich Set of Operators: C provides a wide range of operators that allow for versatile
programming.
Structured Programming: C supports structured programming, which helps in making the
code more readable and maintainable.
Extensibility: You can add new features to C through functions and libraries.
2. Introduction & Features of C
C is a versatile and powerful programming language. It combines the features of high-level
programming languages with the low-level capabilities of assembly language. It was initially
developed to write system software but has since become widely used for application software as
well.
Features of C:
Simple Syntax: C has a simple, clean, and concise syntax, which makes it easy for
programmers to learn and write.
Procedural Language: C is procedural, which means that the code is executed step by step in
a sequential manner.
Rich Library Support: C has a vast collection of built-in functions and libraries that help in
efficient development.
Memory Management: C allows explicit memory management, enabling efficient use of
memory resources.
Pointers: C’s use of pointers enables developers to directly manipulate memory, which is
both powerful and efficient.
3. Structure of a C Program
A C program consists of several components that work together to produce the desired output. The
basic structure is:
c
Copy code
,#include <stdio.h> // Preprocessor directive
// Global declarations
int main() // main function, where program execution begins
{
// Local variables
// Statements
return 0; // Return statement, indicating successful program termination
}
Preprocessor Directives: These are used to include header files, such as #include <stdio.h>,
which gives access to standard input/output functions.
Global Declarations: Variables or functions that can be accessed throughout the program.
Main Function: This is the entry point of any C program. The main() function is mandatory,
and the program begins execution here.
Statements: The actual code or instructions that the program will execute.
4. Variables, Expressions, Identifiers, Keywords, Data Types, Constants
Variables: A variable is a named memory location used to store data. The data type of the
variable defines the type of data it can hold (e.g., int, float, char).
c
Copy code
int a = 10;
float b = 3.14;
Expressions: An expression is a combination of variables, constants, operators, and functions
that are evaluated to produce a value.
c
Copy code
int sum = a + b; // This is an expression
Identifiers: Identifiers are names used to identify variables, functions, arrays, or any other
user-defined item. They must begin with a letter (A-Z or a-z) or an underscore (_) and can
contain letters, digits, and underscores.
,c
Copy code
int myVariable = 5; // "myVariable" is an identifier
Keywords: Keywords are reserved words in C that have special meanings and cannot be used
as identifiers. Examples include int, return, if, while, for, etc.
Data Types: Data types define the type of data a variable can store. C has several basic data
types:
o int: Integer numbers (e.g., 5, -10)
o float: Floating-point numbers (e.g., 3.14)
o char: Single characters (e.g., 'a')
o double: Double precision floating-point numbers
Constants: Constants are fixed values that cannot be changed during the program's
execution. They can be defined using #define or const.
c
Copy code
#define PI 3.14 // Constant using #define
const int MAX = 100; // Constant using const
5. Operators and Expressions
In C, operators are symbols that perform operations on variables and values. They are used to
manipulate data and variables.
Arithmetic Operators: These operators perform mathematical calculations.
o + (Addition)
o - (Subtraction)
o * (Multiplication)
o / (Division)
o % (Modulo)
Example:
c
Copy code
int a = 10, b = 5;
int result = a + b; // result = 15
Logical Operators: Used for logical operations, often in conditions.
, o && (Logical AND)
o || (Logical OR)
o ! (Logical NOT)
Example:
c
Copy code
if (a > 5 && b < 10) {
// This condition is true
}
Relational Operators: Used to compare two values.
o == (Equal to)
o != (Not equal to)
o > (Greater than)
o < (Less than)
o >= (Greater than or equal to)
o <= (Less than or equal to)
Example:
c
Copy code
if (a == 10) {
// This condition is true
}
Conditional Operator (?:): This is a shorthand for if-else conditions.
c
Copy code
int result = (a > b) ? a : b; // result gets the value of a if true, otherwise b
Bitwise Operators: These operate on bits of integer data types.
o & (AND)
o | (OR)
o ^ (XOR)
o ~ (NOT)