0% found this document useful (0 votes)
7 views

C programming 1St Lesson Notes

The document provides an overview of programming in C, covering character sets, keywords, identifiers, variables, data types, and operators. It explains the importance of variable declaration, initialization, and naming rules, as well as the classification of data types into primitive, derived, and user-defined. Additionally, it details various operators used in C, including arithmetic, relational, logical, and special operators, along with common mistakes and mathematical functions.

Uploaded by

Pat Rick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C programming 1St Lesson Notes

The document provides an overview of programming in C, covering character sets, keywords, identifiers, variables, data types, and operators. It explains the importance of variable declaration, initialization, and naming rules, as well as the classification of data types into primitive, derived, and user-defined. Additionally, it details various operators used in C, including arithmetic, relational, logical, and special operators, along with common mistakes and mathematical functions.

Uploaded by

Pat Rick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PROGRAMMING IN C

LESSON

Unit – 1
Character Set:

A character set refers to the set of characters that can be used in


the source code for writing programs. Types of 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

● Escape sequences represent special characters that can't be directly typed


into the code.
● Examples:
o \n (newline)
o \t (tab)
o \\ (backslash)
o \' (single quote)

6. Other Characters

● These include structural symbols like parentheses, braces, and brackets.


● Examples: (), {}, [], <>.

Together, these character sets are used to create tokens such as


variables, constants, and operators in C programs.

Key-words:

▪ Keywords are predefined or reserved words that have special meanings to


the compiler.
▪ These are part of the syntax and cannot be used as identifiers in the
program
A list of keywords in C or reserved words in the C programming language
are mentioned below:

Identifiers:

An identifier is simply a name given to elements such as variables,


functions, or arrays in the program. It must follow the naming rules
Identifier is the name of the variable.
Example: X = 10, Age = 10
In the above example both X and Age are identifier.

1. Variables:
Introduction:

• Objective: To understand what variables are in C programming, how


they are declared, and their role in storing data.
• Definition:

A variable is a named memory location that holds a value that can


be modified during program execution. It is used to store data such as
numbers, characters, or strings.
• In general words - Think of a variable as a box that can hold one
specific type of item (data). You label the box (give the variable a name)
so that you can refer to it later.

Example: int age = 25;


(In this example, age is the name of the variable, and it stores the value 25)

2. Variable Declaration:

What is 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.

Syntax for Declaration → data_type variable_name;

data_type→ The type of data the variable will store (e.g., int)

variable_name→ The name (identifier) for the variable.

3. Variable Initialization:
• Definition:

Initialization means assigning an initial value to a variable at


the time of its declaration.

• Syntax for Initialization: data_type variable_name = value;

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.

Common Data Types:

int: Used to store integers (whole numbers).


• Example: int count = 100;

float: Used to store floating-point numbers (numbers with decimal


points).
• Example: float price = 99.99;

char: Used to store a single character.


• Example: char grade = 'A';

double: Used to store large floating-point numbers with more


precision.
• Example: double distance = 12345.678;

5. Variable Naming Rules:


• Variable names must begin with a letter (a-z, A-Z) or an underscore (_).
• The name can contain letters, digits (0-9), and underscores.
• No special characters ($, @, %) are allowed.
• Variable names are case-sensitive. For example, age and Age are
different.
• Variable names must not be C keywords (like int, return, char).
Example:

o Valid→ age, price, _totalAmount


o Invalid→ 2count (cannot start with a digit),
my-price (contains a hyphen),
float (reserved keyword).

6. Types of Variables:
There are different types of variables based on their scope and lifetime:

Local Variables:

Declared inside a function and can only be used within that


function.
Example:
void example() {
int age = 30; // Local variable
}

Global Variables:

Declared outside of all functions and can be accessed by


any function within the program.

Example:
int count = 100; // Global variable
void example() {
count = 200; } // Accessing global variable
Static Variables:

Retain their value between function calls.


Example:

void example() {
static int num = 0; // Static variable
num++;
}
Constant Variables:

Declared using the const keyword and their value cannot be


changed after initialization.
Example: const int MAX_SIZE = 100;

7. Memory Allocation for Variables:


When a variable is declared, the system allocates a specific block of
memory to store its value. The size of the memory block depends on the
variable's data type.
Example:
• An int usually takes 4 bytes of memory.
• A char takes 1 byte of memory.

8. Using Variables in Expressions:


Variables can be used in mathematical or logical expressions to
perform operations.
Example:
int a = 10, b = 20;
int sum = a + b; // Using variables in a mathematical expression
(In this example, a and b are variables used to calculate the sum.)

9. Common Mistakes When Using Variables:


➢ Using undeclared variables,
➢ Re-declaring variables,
➢ Type (Data Type) mismatch.
DATA TYPES:

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:

In C, data types are broadly divided into:


1. Primitive Data Types (built-in types)
2. Derived Data Types (arrays, pointers, etc.)
3. User-Defined Data Types (structures, unions, etc.)

❖ Primitive Data Types:


These are the basic data types provided by C

I. Integer Types

Integer types are used to store whole numbers (without decimal


points). These types can be signed (positive and negative) or
unsigned (only positive). Some of these Types are..

i. Int - Used to store whole numbers.


Typically takes 4 bytes of memory.
Example - int age = 25;
ii. Short - A shorter version of int, taking 2 bytes of memory
Example - short smallNumber = 100;

iii. Long - Used when you need larger integer values than int
can handle.
Typically takes 8 bytes.
Examples - long largeNumber = 1000000L;
II. Floating:

Used to store real numbers (numbers with decimal points). Some of


these types are….

a) Float - Takes 4 bytes of memory.


Used for numbers with smaller decimal precision.
Example - float price = 19.99;

b) Double - Takes 8 bytes of memory and provides more


precision for decimal values
Example - double distance = 123456.789;

III. Character Type:

Used to store individual characters. Some of these types are…

1) Char - Takes 1 byte of memory.


Stores a single character enclosed in single quotes
('A', 'b').
Example - char grade = 'A';
2) Void Type - The void type is used to indicate that no value
is available. It’s typically used with functions to indicate they
don’t return a value.

❖ Derived Data Types:


Derived data types are built from the primitive data types and
include arrays, pointers, functions, and structures.

➢ 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 = &num;
➢ 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:

1. signed: Allows a variable to store both positive and negative values.


o Example: signed int num = -100;

2. unsigned: Used to store only positive values.


o Example: unsigned int num = 100;

3. short: Reduces the size of the integer (typically 2 bytes).


o Example: short int smallNum = 100;

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.

2) Operator Precedence Confusion: Always be mindful of the order in


which operators are evaluated; using parentheses can clarify the intended
calculations.

➢ 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.

• Logical AND (&&): Returns true if both operands are true.


• Logical OR (||): Returns true if at least one operand is true.
• Logical NOT (!): Reverses the logical state of its operand.

➢ 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.

+= (addition and assignment)


-= (subtraction and assignment)
*= (multiplication and assignment)
/= (division and assignment)
%= (modulus and assignment)

➢ Increment and Decrement Operators:

Increment (++) and Decrement (--) operators are used to increase or


decrease the value of a variable by 1. Types of Increment and Decrement Operat
Postfix (x++): The value is increased after the expression is
evaluated. Ex - int x = 5; int y = x++; // y is 5, x is now 6
Prefix (++x): The value is increased before the expression is
evaluated. int x = 5; int y = ++x; // y is 6, x is also 6
➢ Conditional (Ternary) Operator:
The conditional operator (also known as the ternary operator) is a
shorthand for the if-else statement. It takes three operands and evaluates to one
of two values based on a condition.
Syntax: condition ? expression_if_true : expression_if_false;
Ex: int max = (a > b) ? a : b;

➢ 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)

OR (|): Compares each bit; returns 1 if at least one bit is 1.


• Example: 5 | 3 results in 7.
0101 →(5)
0011 →(3)
-----
0110 →(6)
XOR (^): Compares each bit; returns 1 if the bits are different.
• Example: 5 ^ 3 results in 6.
NOT (~): Inverts all bits.
• Example: ~5 results in -6 (in two's complement).
➢ Special Operators:

Special operators are specific operators used in C for


particular functionalities.
1. Sizeof Operator:
Returns the size (in bytes) of a data type or variable.

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

3. Pointer Operator (*, &):


o is used to declare a pointer or to access the value at the
address the pointer points to.
o & is used to get the address of a variable.

➢ 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:

o Assignment operators assign values to variables.


o Increment and decrement operators modify values by 1.
o Conditional operators provide shorthand for conditional
statements.
o Bitwise operators manipulate data at the bit level.
o Special operators serve unique purposes in C.
o Mathematical functions facilitate complex calculations.

You might also like