BCA Resources - C Mod 2
BCA Resources - C Mod 2
BCA (Honours)
Module 2
C Character Set
In C, a character set refers to the valid characters recognized by the compiler. These are
the building blocks for writing programs.
Delimiters in C
Delimiters in C are symbols that help separate different parts of the code. Common
delimiters include:
Tokens in C
Tokens are the smallest units in a C program and can be classified into several
categories:
1. Keywords: Reserved words with special meanings (e.g., int, return, if)
2. Identifiers: User-defined names for variables, functions, and other entities
Keywords
C has a set of reserved keywords, which have predefined meanings and cannot be used
as identifiers (variable or function names). Here are some common C keywords:
Identifiers
Identifiers are names given by the programmer to elements like variables, functions,
arrays, etc. Rules for defining identifiers include:
Example: Valid identifiers include count, _total, sum1, while invalid ones are 1sum
(begins with a digit) and float (keyword).
Constants
Constants are values that do not change throughout the program. Types of constants in C
include:
● Integer Constants: Whole numbers without decimal points (e.g., 10, -25)
● Floating-point Constants: Numbers with decimal points (e.g., 3.14, -0.001)
● Single Character Constants: Single characters enclosed in single quotes (e.g., 'A',
'$')
● String Constants: Sequence of characters enclosed in double quotes (e.g.,
"Hello")
Variables
Variables are containers for storing data values in a program. In C, variables must be
declared before use, specifying their type (e.g., int, float, char).
Declaration Example:
int count; // Integer variable
Data Types
Data types specify the type of data that a variable can store. They are essential as they
determine how much memory is allocated for the variable and how the data is
interpreted.
Integer (int):
○ Used to store whole numbers without decimal points (e.g., 5, -23).
○ Size: Typically 2 or 4 bytes, depending on the system.
○ Example: int age = 25;
Character (char):
● Used to store single characters (e.g., 'A', 'b').
● Size: 1 byte.
● Holds ASCII values, so each character internally maps to an integer.
● Example: char grade = 'A';
Floating-point (float):
● Used for decimal values or fractional numbers (e.g., 3.14, -0.001).
● Size: 4 bytes.
● Precision: Up to 6-7 decimal places.
● Example: float price = 19.99;
Double (double):
● Used for high-precision decimal numbers.
● Size: 8 bytes.
● Precision: Up to 15-16 decimal places, making it suitable for scientific
calculations.
● Example: double largeNumber = 12345.6789;
Void (void):
● Represents the absence of a value.
● Primarily used with functions that do not return any value or with pointers.
● Example:
void displayMessage()
printf("Hello, World!");
Arrays:
● A collection of elements of the same data type stored in contiguous memory
locations.
● Allows handling of large amounts of data efficiently.
● Syntax: data_type array_name[array_size];
● Example: int scores[5] = {90, 85, 88, 92, 80}; // Integer array with 5 elements
Pointers:
● Stores the memory address of another variable.
● Useful for dynamic memory allocation, arrays, and function calls.
● Syntax: data_type *pointer_name;
● Example:
Strings:
Functions:
● Encapsulate reusable code blocks.
● A function can take inputs, process them, and return outputs.
● Example:
return a + b;
Structures (struct):
● Used to group different data types under a single unit.
● Allows storing related information together in a more organized way.
● Syntax:
struct structure_name {
data_type member1;
data_type member2;
// more members...
};
Unions (union):
● Similar to structures, but members share the same memory location.
● Only one member can hold a value at any given time.
● Useful for memory-saving when only one of several variables is needed.
● Syntax:
union union_name {
data_type member1;
data_type member2;
// more members...
};
Enumerations (enum):
● Allows defining a set of named integer constants, improving code readability.
● Syntax:
Variables can also be initialized later in the code after they have been declared.
Type Modifiers
Type modifiers in C allow you to alter the properties of basic data types, affecting their
size or range. Common type modifiers include:
● signed: Allows a variable to hold both positive and negative values (e.g., signed
int x = -5;).
● unsigned: Restricts a variable to only positive values (e.g., unsigned int x =
5;).
● short: Reduces the storage size of an integer (e.g., short int x = 10;).
● long: Increases the storage size of an integer (e.g., long int x = 100000;).
Type Conversion
Type conversion refers to changing one data type to another. There are two main types:
Operators
Operators in C are symbols used to perform operations on variables and values.
Operators are used in programs to manipulate data and variables. They are used with
operands to build expressions.
Relational Operators: used to compare two values. They evaluate either true (1) or
false (0).
○ Example: a < b
● Greater than or equal to (>=): Checks if the first operand is greater than or equal to
the second.
○ Example: a >= b
● Less than or equal to (<=): Checks if the first operand is less than or equal to the
second.
○ Example: a <= b
Logical Operators: used to combine multiple conditions or expressions. They are mostly
used in decision-making statements.
● Modulus and Assign (%=): Takes modulus using the two operands and assigns the
result to the left operand.
○ Example: a %= 5; (equivalent to a = a % 5)
Increment and Decrement Operators: These operators increase or decrease the value of
a variable by one.
Special Operators: unique operators that perform specific tasks not covered by standard
arithmetic, logical, or bitwise operators. Here’s a detailed explanation of the special
operators available in C:
Sizeof Operator
● Description: The sizeof operator determines the size (in bytes) of a data type or
variable in memory.
● Syntax: sizeof(data_type or variable)
Example:
int a;
● Use Cases:
○ Checking data type sizes for portability.
○ Dynamic memory allocation using functions like malloc.
Example:
● Use Cases:
○ Combining multiple expressions in a single statement.
○ Used in loops for multiple initialization or increment steps
Expressions in C
An expression is any valid combination of operators, constants, variables, and functions
that produce a result. Expressions are evaluated to yield a value.
Types of Expressions
1. Constant Expressions: Contain only constant values and are evaluated at compile
time.
○ Example: 10 + 5
2. Variable Expressions: Involve variables and yield a result when evaluated.
○ Example: a + b
3. Arithmetic Expressions: Combine variables and constants using arithmetic
operators.
○ Example: a + b - c * d / e
4. Relational Expressions: Use relational operators to compare values, returning true
or false.
○ Example: a > b
5. Logical Expressions: Combine multiple relational expressions using logical
operators, returning true or false.
○ Example: (a > b) && (b < c)
6. Bitwise Expressions: Use bitwise operators on integer values.
○ Example: a & b
Evaluation of Expressions
The evaluation of expressions follows the operator precedence and associativity rules in
C:
An arithmetic expression without parentheses will be evaluated from left to right using
the rules of precedence of operators. There are two distinct priority levels of arithmetic
operators in C:
● High priority: * / %
● Low Priority: + -
The basic evaluation procedure includes ‘’two” left-to-right passes through the
expression. During the first pass, the high priority operators are applied as they are
encountered. During the second pass, the low priority operators are applied as they are
encountered.
int result = 10 + 5 * 2;
● Explanation:
1. Multiplication (*) is evaluated first: 5 * 2 = 10.
2. Addition (+) is next: 10 + 10 = 20.
● Result: 20.