C programming 1St Lesson Notes
C programming 1St Lesson Notes
LESSON
Unit – 1
Character Set:
1. Alphabets
● These include both uppercase (A-Z) and lowercase (a-z) English letters.
● Example: A, b, Z, m.
2. Digits
● These are the numerical digits used in C, ranging from 0 to 9.
● Example: 0, 1, 9.
3. Special Characters
● These include symbols and punctuation marks used for operations,
delimiters, and control in programs.
● Examples:
o Arithmetic: +, -, *, /
o Relational: <, >, =
o Others: @, #, &, ;, :.
4. Whitespace Characters
● These are non-visible characters used to format the code and control text
layout.
● Examples:
o Space:
o Tab: \t
o Newline: \n
5. Escape Sequences
6. Other Characters
Key-words:
Identifiers:
1. Variables:
Introduction:
2. Variable Declaration:
Declaring a variable means telling the compiler that you are going to use
a specific variable and specifying what data type the variable will store.
data_type→ The type of data the variable will store (e.g., int)
3. Variable Initialization:
• Definition:
Example:
int age = 25;
float salary = 50000.50;
char grade = 'A';
4. Data Types in C:
When declaring a variable, you need to specify the type of data it
will hold because C is a strongly typed language. This means that each
variable can only store data of a specific type.
6. Types of Variables:
There are different types of variables based on their scope and lifetime:
Local Variables:
Global Variables:
Example:
int count = 100; // Global variable
void example() {
count = 200; } // Accessing global variable
Static Variables:
void example() {
static int num = 0; // Static variable
num++;
}
Constant Variables:
Definition:
A data type defines the type of data that a variable can store
and the operations that can be performed on that data. It also tells the
compiler how much memory is needed for that variable.
Classification:
I. Integer Types
iii. Long - Used when you need larger integer values than int
can handle.
Typically takes 8 bytes.
Examples - long largeNumber = 1000000L;
II. Floating:
➢ Arrays:
An array is a collection of variables of the same data type.
Example - int numbers[5] = {1, 2, 3, 4, 5};
➢ Pointers:
A pointer is a variable that stores the memory address of another
variable
Example - int num = 10;
int *ptr = #
➢ Functions:
Functions also return values of specific data types, which can be
void, int, float, etc.
❖User-Defined Data:
C also allows users to define their own data types using structures,
unions, and typedef.
❖Type Modifiers:
C provides type modifiers to alter the size and range of data types. These
include:
4. long: Increases the size of the integer or float (typically 8 bytes for long
integers).
o Example: long int largeNum = 123456789;
Constants:
A constant is a fixed value that does not change during the
execution of a program. Unlike variables, which can be modified,
constants are immutable once defined.
Example:
#include <stdio.h>
const float GRAVITY = 9.81;
int main() {
float weight = 10.0; // in kg
float force = weight * GRAVITY;
printf("Weight in Newtons: %.2f N\n", force);
return 0;
}
Expressions:
An expression is a combination of variables, constants, operators, and
functions that evaluates to a value.
Example:3 + 5 is an expression that evaluates to 8
Types of Expressions
Arithmetic Expressions - a + b, x * y, p - q.
Relational Expressions - x > y, a == b
Logical Expressions - (x > y) && (a < b)
Operator’s:
Operators are special symbols that perform operations on
variables and values. They can be classified into several categories:
➢ Arithmetic Operators:
Arithmetic operators are symbols used to perform mathematical
operations on numeric values. Some of the Common Arithmetic Operators are
Addition (+): Adds two operands.
Subtraction (-): Subtracts the second operand from the first.
Multiplication (*): Multiplies two operands
Division (/): Divides the first operand by the second
Modulus (%): Returns the remainder of the division of the first
operand by the second
Common Mistakes:
1) Integer Division: Remember that dividing two integers results in integer
division, truncating any decimal part.
➢ Relational Operators:
These operators are used to compare two values
Equal to (==): Checks if two operands are equal.
Not equal to (!=): Checks if two operands are not equal.
Greater than (>): Checks if the left operand is greater than the
right.
Less than (<): Checks if the left operand is less than the right.
Greater than or equal to (>=): Checks if the left operand is greater
than or equal to the right.
Less than or equal to (<=): Checks if the left operand is less than or
equal to the right.
➢ Logical Operators:
These operators are used to combine conditional statements.
➢ Assignment Operators:
• Assignment operators are used to assign values to variables.
• The most common assignment operator is the “=”operator.
Compound Assignment Operators
These operators combine an arithmetic operation with
assignment. They simplify expressions.
➢ Bitwise Operators:
Bitwise operators perform operations on bits and are often used for
low-level programming and manipulating data at the bit level.
AND (&): Compares each bit; returns 1 if both bits are 1.
• Example: 5 & 3 results in 1.
0101 →(5)
0011 →(3)
-----
0001 →(1)
2. Comma Operator:
Allows two expressions to be evaluated in a single
statement, returning the value of the second expression. Ex:
int a = (5, 10); // a will be assigned 10
➢ Mathematical Functions:
i. abs(): Returns the absolute value of an integer.
ii. sqrt(): Returns the square root of a number.
iii. pow(): Raises a number to the power of another
iv. round(): Rounds a floating-point number to the nearest
integer
Conclusion from the operators: