In C programming, operators are symbols that perform operations on variables and values.
Operators in C can be categorized based on the number of operands they work with and the type of
operations they perform. Here are the primary categories of operators in C:
1. Unary Operators
Description: Operators that operate on a single operand. Or Operators that perform operation
on a single operand.
Examples:
o ++ : Increment
o -- : Decrement
o ! : Logical NOT
o ~ : Bitwise NOT
o - : Unary minus (negation)
o + : Unary plus
o & : Address-of
o * : Dereference
o sizeof : Size of a data type or variable
2. Binary Operators
Description: Operators that operate on two operands.
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, >, <, >=, <=
Logical Operators: &&, ||
Bitwise Operators: &, |, ^, <<, >>
Assignment Operators: =, +=, -=, *=, /=, %=
Examples:
Arithmetic Operators
These operators are used to perform basic arithmetic operations.
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : Modulus (remainder of division)
3. Relational Operators :-
These operators are used to compare two Numberic values (Integer, floating, character).
== : Equal to
!= : Not equal to
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than or equal to
3. Logical Operators
These operators are used to perform logical operations.
&& : Logical AND
|| : Logical OR
! : Logical NOT
4. Bitwise Operators
These operators perform operations on bits.
& : Bitwise AND
| : Bitwise OR
^ : Bitwise XOR (exclusive OR)
~ : Bitwise NOT
<< : Left shift
>> : Right shift
5. Assignment Operators
These operators are used to assign values to variables.
= : Simple assignment
+= : Add and assign
-= : Subtract and assign
*= : Multiply and assign
/= : Divide and assign
%= : Modulus and assign
3. Ternary Operator
Description: The only ternary operator in C, it operates on three operands and is used as a
shorthand for an if-else statement.or the ternary operator is a shorthand way of writing an if-
else statement.
Example:
o ?: : Ternary conditional (e.g., condition ? value_if_true : value_if_false)
4. Special Operators
Description: These operators serve special purposes, often related to memory management,
structure handling, or type conversion.
Examples:
o sizeof : Returns the size of a variable or type
o & : Address-of (also a unary operator)
o * : Dereference (also a unary operator)
o . : Member access for structure or union
o -> : Member access through a pointer to a structure
o [] : Array subscript
o () : Function call
o (type) : Type cast
Operators Precedence and Associativity are two characteristics of operators that determine the
evaluation order of sub-expressions.
1. Precedence: Defines the order in which operators are evaluated in expressions with more than
one operator.
2. Associativity: Defines the order in which operators of the same precedence level are evaluated,
i.e., whether they are evaluated from left to right or right to left.
Operator Precedence and Associativity Table
Below is a table of C operators sorted by their precedence from highest to lowest, along with their
associativity.
**Note** :- 1) Precedence level
Unary > Binary > ternary > assignment
Logical not (!) > Logical and (&&) > Logical or (||)
*,/,% have same and more precedence than +,-
Precedence
Operator Description Associativity
Level
Function call, array subscript, structure
1 (Highest) () [] -> . Left to Right
member access
2 ++ -- + - ! ~ * & sizeof Unary operators, type casting Right to Left
Precedence
Operator Description Associativity
Level
(type)
3 */% Multiplication, division, modulus Left to Right
4 +- Addition, subtraction Left to Right
5 << >> Bitwise shift left, shift right Left to Right
6 < <= > >= Relational operators (less/greater) Left to Right
7 == != Equality, inequality Left to Right
8 & Bitwise AND Left to Right
9 ^ Bitwise XOR Left to Right
10 ‘ ‘ Bitwise OR
11 && Logical AND Left to Right
12 ‘ ‘
13 ?: Ternary conditional Right to Left
= += -= *= /= %= <<= >>= Assignment
14 =‘
&= ^= ‘ operators
15 , Comma operator Left to Right
Key Points:
Highest precedence operators are evaluated first. For example, multiplication (*) and division (/)
have higher precedence than addition (+) and subtraction (-), so in an expression like 3 + 4 * 5,
the multiplication happens first, resulting in 3 + 20 = 23.
C Program to illustrate operator precedence
#include <stdio.h>
int main()
{
// printing the value of same expression
• printf(“10 + 20 * 30 = %d”, 10 + 20 * 30);
• return 0;
}
Output
10 + 20 * 30 = 610
In C programming, the concepts of expressions and statements are fundamental to how programs are
structured and executed.
Expression
Definition: An expression in C is any valid combination of operators, constants, variables, and
function calls that produce a value. Expressions can be as simple as a single variable or constant,
or more complex, involving multiple operations and function calls.
Examples:
o x + y (where x and y are variables)
o 5 * (3 + 2)
o a++
o sqrt(x) (assuming sqrt is a function)
Evaluation: An expression is evaluated to produce a value. For example, in the expression 5 + 3,
the result of evaluation is 8.
2. Statement
Definition: A statement in C is a complete instruction that performs an action. or a statement in
C is a complete instruction to be executed by a compiler. It is often formed by one or more
expressions and ends with a semicolon (;). Statements control the flow of execution in a
program.
Types of Statements:
a. Expression Statements
o Description: These are statements that consist of an expression followed by a semicolon.
They typically involve variable assignments, function calls, or arithmetic operations.
o Examples:
x = 5;
y++;
printf(“Hello, World!”);
a = b + c;
b. Compound Statements (Block)
o Description: A compound statement (also known as a block) groups multiple statements
together within curly braces {}. It allows multiple statements to be executed where a
single statement is expected.
o Syntax:
{
statement1;
statement2;
...
o Example:
int x = 10;
x++;
printf(“%d”, x);
c. Selection Statements/ Conditional Statements
o Description: These statements allow the program to choose different paths of execution
based on certain conditions.
o Types:
if statement:
if (condition) {
// code to execute if condition is true
}
if-else statement:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
else if ladder
if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if none of the above conditions are true
}
switch statement:
switch (expression) {
case constant1:
// code to execute if expression equals constant1
break;
case constant2:
// code to execute if expression equals constant2
break;
default:
// code to execute if expression doesn’t match any case
}
d. Iteration Statements (Loops)
o Description: These statements repeatedly execute a block of code as long as a specified
condition is true.
o Types:
while loop:
while (condition) {
// code to execute while condition is true
}
do-while loop:
do {
// code to execute at least once and then while condition is true
} while (condition);
for loop:
for (initialization; condition; increment) {
// code to execute while condition is true
}
e. Jump Statements
o Description: These statements are used to alter the flow of control by transferring
control to another part of the program.
o Types:
break: Exits the current loop or switch statement
break;
continue: Skips the rest of the loop iteration and proceeds with the next
iteration or it is used to skip current iteration and proceed with next iteration.
continue;
return: Exits the current function and optionally returns a value.
return value;
goto: Transfers control to a labeled statement within the same function.
goto label;
f. Labeled Statements
o Description: These are statements that are preceded by a label, which can be used with
the goto statement for jump operations.
o Example:
label:
statement;
start:
printf(“This is a labeled statement.”);
goto start;
Formatted I/O Functions
Formatted I/O functions are used to take various inputs from the user and display multiple outputs to
the user. These types of I/O functions can help to display the output to the user in different formats
using the format specifiers.
What us firmat specifier ?
Fornat specifier is write in input output function to take various types of inputs or display multiple
types of outputs to the user in different formats.
List of some format specifiers-
S
Format Specifier Type Description
NO.
int/signed
1 %d used for I/O signed integer value
int
2 %c char Used for I/O character value
3 %f float Used for I/O decimal floating-point value
4 %s string Used for I/O string/group of characters
5 %ld long int Used for I/O long signed integer value
6 %u unsigned int Used for I/O unsigned integer value
7 %i or %d unsigned int used for the I/O integer value(+ve,-ve)
8 %lf double Used for I/O fractional or floating data
9 %n prints prints nothing
The following formatted I/O functions are
1. printf()
2. scanf()
3. sprintf()
4. sscanf()
printf()
Purpose: Outputs formatted text to the console.
Syntax:-
printf(“Format Specifier”, var1, var2, …., varn);
example:
int x = 10;
printf(“The value of x is: %d\n”, x);
Syntax 2:
To display any string or a message
printf(“Enter the text which you want to display”);
int main()
// Displays the string written
// inside the double quotes
printf(“This is a string”);
return 0;
scanf():
scanf() function is used in the C program for reading or taking any value from the keyboard by the user,
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
// C program to implement
// scanf() function
#include <stdio.h>
// Driver code
int main()
int num1;
// Printing a message on
// the output screen
printf(“Enter a integer number: “);
// Taking an integer value
// from keyboard
scanf(“%d”, &num1);
// Displaying the entered value
printf(“You have entered %d”, num1);
return 0;
}Unformatted Input/Output functions
Unformatted I/O functions are used only for character data type or character array/string and cannot be
used for any other datatype. These functions are used to read single input from the user at the console
and it allows to display the value at the console.
The following unformatted I/O functions will be discussed in this section-
1. getch()
2. getche()
3. getchar()
4. putchar()
5. gets()
6. puts()
7. putch()
getch()
Purpose: Reads a single character from the keyboard without echoing it to the console.
Syntax:
getch();
or
variable-name = getch();
// C program to implement
// getch() function
#include <conio.h>
#include <stdio.h>
// Driver code
int main()
printf(“Enter any character: “);
// Reads a character but
// not displays
getch();
return 0;
getche()
Purpose: Reads a single character from the keyboard and echoes it to the console.
Syntax:
getche();
or
variable_name = getche();
// C program to implement
// the getche() function
#include <conio.h>
#include <stdio.h>
int main()
printf(“Enter any character: “);
// Reads a character and
// displays immediately
getche();
return 0;
getchar():
The getchar() function is used to read only a first single character from the keyboard whether multiple
characters is typed by the user and this function reads one character at one time until and unless the
enter key is pressed. This function is declared in stdio.h(header file)
Syntax:
Variable-name = getchar();
// C program to implement
// the getchar() function
#include <conio.h>
#include <stdio.h>
// Driver code
int main()
// Declaring a char type variable
char ch;
printf(“Enter the character: “);
// Taking a character from keyboard
ch = getchar();
// Displays the value of ch
printf(“%c”, ch);
return 0;
putchar():
The putchar() function is used to display a single character at a time by passing that character directly
to it or by passing a variable that has already stored a character. This function is declared in
stdio.h(header file)
Syntax:
putchar(variable_name);
// C program to implement
// the putchar() function
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf(“Enter any character: “);
// Reads a character
ch = getchar();
// Displays that character
putchar(ch);
return 0;
gets():
gets() function reads a group of characters or strings from the keyboard by the user and these
characters get stored in a character array.
Syntax:
char str[length of string in number]; //Declare a char type variable of any length
gets(str);
now days gets has deprecated due to security purpose.
Detail Explaination on input unformatted functions :-
1. getch
Purpose: Reads a single character from the keyboard without displaying it on the screen.
Behavior:
Does not require pressing Enter.
Part of <conio.h> (non-standard library, specific to some compilers like Turbo C).
Mostly used in DOS-based programming.
Use Case: Used for password input, menu navigation, or pausing programs where input
visibility is not needed.
2. getchar
Purpose: Reads a single character from the standard input (stdin) and requires pressing Enter.
Behavior:
Displays the character on the screen as you type.
Part of the standard C library (<stdio.h>), so it is portable.
Use Case: Suitable for reading single-character input in standard C programs.
3. Gets
Purpose: Reads a string (line of text) from the standard input.
Behavior:
Reads characters until a newline (\n) is encountered.
Part of the standard C library (<stdio.h>).
It is echoing the character on the screen
Does not perform bounds checking, which can cause buffer overflow issues.
Deprecated in modern C standards (C11 and later); use fgets instead.
Use Case: Historically used for string input but now avoided due to security risks.
4. Getche
Purpose: Reads a single character from the keyboard and displays it on the screen as you type.
Behavior:
Does not require pressing Enter.
Part of <conio.h> (non-standard librar getch).
Use Case: Used in cases where character input needs to be displayed immediately (e.g.,
interactive menus).
Detail Explaination on output unformatted functions :-
1. putch
Purpose: Displays a single character to the screen.
Behavior:
Belongs to the <conio.h> library (non-standard, DOS-specific).
Do not Automatically appends a newline at the end of the string.
Often used in older compilers like Turbo C.
Use Case: Useful for simple output in console-based applications (rarely used in modern
programming).
2. puts
Purpose: Outputs a string followed by a newline (\n) to the screen.
Behavior:
Part of the standard C library (<stdio.h>), making it portable.
Automatically appends a newline at the end of the string.
Use Case: Preferred for printing strings when a newline is desired.
Putchar
Purpose: Outputs a single character to the screen.
Behavior:
Part of the standard C library (<stdio.h>), making it portable.
Do not Automatically appends a newline at the end of the string.
Can be used in loops to print multiple characters (e.g., printing a string character by character).
Use Case: Used for single-character output in standard C programs.
Heder files
header files in C are essential for organizing and managing code in larger programs.
What are Header Files?
Header files are files with a .h extension that typically contain function prototypes, macro
definitions, type definitions, and global variables. They do not contain the actual code for
functions but provide declarations that are shared between multiple source files.
Benefits of Using a Header File
Modularity: The code is organized into separate files or In large program we can break into
separate files, making it easier to manage and maintain.
Reusability: The factorial function can be reused in other programs by simply including the
header file or once header file is defined we can used repeatedly without written the same code.
Collaboration: In larger projects, different team members can work on different parts of the
program simultaneously.
Ease of maintenance : If the header file needs to modify then the changes is made in one place
Abstraction : It hide the implementation details from the user eg:-only expose / available
function prototype (return type, function name, parameters).
// myheader.h
// Function prototype
void myFunction();
// Macro definition
#define MAX 100
// Global variable
extern int globalVariable;
PREPROCESSOR
Preprocessor directives are commands that are processed by the preprocessor before the actual
compilation of the code begins. Two commonly used preprocessor directives in C are #include and
#define.
1. #include Directive
The #include directive is used to include the contents of a file (usually a header file) into another file.
This is often used to include standard libraries or user-defined headers in a C program.
Types of #include
Including Standard Library Headers: When including standard library headers, the file name is
enclosed in angle brackets (<>). For example:
#include <stdio.h>
Including User-Defined Headers: When including user-defined header files, the file name is enclosed in
double quotes (“”). For example:
#include “myheader.h”
This tells the preprocessor to look for the myheader.h file in the current directory or the specified
directory.
#include “myheader.h”
int main() {
printf(“Hello, World!\n”);
return 0;
2. #define Directive
The #define directive is used to define macros, which are constant values or expressions that can be
reused throughout the program. Macros are substituted by their values before the compilation of the
code.
Basic Usage of #define
Defining Constants: You can define a constant value using #define. For example:
#define PI 3.14159
This tells the preprocessor to replace every instance of PI in the code with 3.14159.
Defining Macros with Parameters: You can also define macros that take parameters, similar to functions.
For example:
#include <stdio.h>
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
int main() {
float radius = 5.0;
float area = PI * SQUARE(radius);
printf(“Area of the circle: %.2f\n”, area);
return 0;
Important Points
No Semicolon: When defining a macro using #define, do not use a semicolon (;). The directive
itself is not a statement but an instruction to the preprocessor.
Preprocessing Only: These directives are processed before the actual compilation of the code.
The compiler never sees the macros; it only sees the code after the macros have been replaced
by their values.
Caution with Macros: Macros do not have type checking or scope like functions, so they should
be used carefully to avoid errors or unintended consequences.