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

Chapter 2 - Basic of c - Variable Data Types

Chapter 2 covers the basics of C/C++ programming, focusing on variables, data types, and operators. It explains variable declaration, types (local, global, static), and data types (primitive and user-defined), including structures, unions, and enums. Additionally, it introduces operators in C, categorizing them into arithmetic, assignment, relational, logical, and bitwise operators.

Uploaded by

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

Chapter 2 - Basic of c - Variable Data Types

Chapter 2 covers the basics of C/C++ programming, focusing on variables, data types, and operators. It explains variable declaration, types (local, global, static), and data types (primitive and user-defined), including structures, unions, and enums. Additionally, it introduces operators in C, categorizing them into arithmetic, assignment, relational, logical, and bitwise operators.

Uploaded by

24134068
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

CHAPTER 2: BASIC OF C/C++

D ATA S T R U C T U R E a n d A L G O R I T H M S

M.E. LE THANH TUNG


2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.1 Variables in C:
⚬ A variable in C is a memory location with some name that helps
store some form of data and retrieves it when required.
⚬ The syntax to declare a variable in C:

data_type Name;
or data_type Name = Value;

■ data_type: Type of data that a variable can store.


■ Name: Name of the variable.
■ Value: value assigned to the variable.
2 . 1 VA R I A B L E S a n d D ATA T Y P E S ?

• 2.1.1 Variables in C:
⚬ Example:
// C Program
#include <stdio.h>
int main()
{
// variable declaration:
int x1; // integer variable
fl oat x2; // fl oat variable

// variable declaration & initialization:


int a1 = 5; // integer variable
fl oat a2 = 5.5; // fl oat variable

return 0;
}
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.1 Variables in C:
⚬ C Variable Types: The C variables can be classified into the
following types:
⚬ Local Variables: is a variable that is declared inside a function
or a block of code. Its scope is limited to the block or
function in which it is declared.
⚬ Global Variables: is a variable that is declared outside the
function or a block of code. We can access the global
variable anywhere in the C program after it is declared.
⚬ Static Variables: is a variable that is defined using
the static keyword. It can be defined only once in a C program
2 . 1 VA R I A B L E S a n d D ATA T Y P E S ?

• 2.1.1 Variables in C:
⚬ Example:
// C Program
#include <stdio.h>
int x1; // global variable

int main()
{
int x1; // local variable
fl oat x2; // fl oat variable

// variable declaration & initialization:


int a1 = 5; // integer variable
fl oat a2 = 5.5; // fl oat variable

return 0;
}
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.2 Data types in C:


⚬ Data type is a type of information transmitted between the
programmer and the compiler
⚬ It informs the compiler about what type of data is to be stored and
also tells how much space it requires in the memory.
⚬ The data types in C can be classified as follows:
■ Primitive Data Types: the most basic data types that are used for
representing simple values such as integers, float, characters, etc.
■ User Defined Data Types: are defined by the user himself.
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.2 Data types in C:


⚬ Primitive data types:
⚬ Interger types:
Data types Size (bytes) Range of Value
char 1 byte -128 to 127

unsigned char 1 byte 0 to 255

short 2 bytes -32,768 to 32,767

unsigned 2 bytes 0 to 65,535


short
int 4 bytes -2,147,483,648 to 2,147,483,647

unsigned int 4 bytes 0 to 4,294,967,295

long 8 bytes -9223372036854775808 to


9223372036854775807
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.2 Data types in C:


⚬ Primitive data types:
⚬ Floating-point types, boolean and chacracter:

Data types Size (bytes) Description


float 4 byte 6 decimal places

double 8 byte 15 decimal places

long double 10 bytes 19 decimal places

bool 1 bit (or 1 byte) Store values as true-false, or 0-1.

char 1 byte Represents a single character; are encoded by the


ASCII table.
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.2 Data types in C:


⚬ User-define data types:
⚬ The data types defined by the user themself are referred to as
user-defined data types. These data types are derived from the
existing datatypes.
⚬ There are 4 types of user-defined data types in C:
■ Structure: are used to group items of different types into a
single type.
■ Union: Unions are similar to structures in many ways.
■ Enumeration (Enums): custom data types with a set of named
integer constants.
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.2 Data types in C:


⚬ Structure:
⚬ The structure can be used to group items of different types into a
single type.
⚬ The struct keyword is used to define the structure
⚬ The items in the structure are called its member.
⚬ The syntax to declare a struct in C:
struct structure_name {
data_type member_name_1;
data_type member_name_2;

2 . 1 VA R I A B L E S a n d D ATA T Y P E S ?

• 2.1.2 Data types in C:


⚬ Example:
#include <stdio.h>
// declaring structure with name student
struct student {
int student_id; // struct member
fl oat score; // struct member
};
int main()
{
struct student sv1; // variable declaration
sv1.student_id = 12345;
struct student sv2 = {12346, 8.5}; // initialization

return 0;
}
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.2 Data types in C:


⚬ Union:
⚬ Union: group items of different types into a single type just like
structure.
⚬ But unlike structures, all the members in the C union are stored in
the same memory location. Due to this, only one member can
store data at the given instance.
union union_name {
data_type member_name_1;
data_type member_name_2;

2 . 1 VA R I A B L E S a n d D ATA T Y P E S ?

• 2.1.2 Data types in C:


⚬ Example:
#include <stdio.h>
// declaring union with name student
union student{
int student_id; // union member
fl oat score; // union member
};
int main()
{
union student sv1; // variable declaration
sv1.student_id = 12345;
sv1.score = 5.5;
return 0;
}
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.2 Data types in C:


⚬ Enumeration (enum):
⚬ Enum: used to assign names to integral constants, the names
make a program easy to read and maintain.
enum enum_name {
member_name1 = value1,
member_name2 = value2,

};
2 . 1 VA R I A B L E S a n d D ATA T Y P E S ?

• 2.1.2 Data types in C:


⚬ Example:
#include <stdio.h>
// declaring enum
enum State{
stop = 0, // enum member
start = 1 // enum member
};
enum Week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum State motor1; // variable declaration
enum Week day; // variable declaration
motor1 = stop;
day = Sat;
return 0;
}
2 . 1 VA R I A B L E S a n d D ATA T Y P E S :

• 2.1.2 Data types in C:


⚬ typedef:
⚬ typedef: is a keyword that is used to provide existing data types
with a new name.
⚬ The C typedef keyword is used to redefine the name of already
existing data types.
typedef existing_name alias_name;
2 . 1 VA R I A B L E S a n d D ATA T Y P E S ?

• 2.1.2 Data types in C:


⚬ Example:
#include <stdio.h>
// declaring enum
typedef enum State{
stop = 0, // enum member
start = 1 // enum member
};
typedef enum Week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
State motor1; // variable declaration
Week day; // variable declaration
motor1 = stop;
day = Sat;
return 0;
}
2 . 2 O P E R AT O R I N C :

• 2.2 Operator:
⚬ An operator in C can be defined as the symbol that helps us to perform
some specific mathematical, or logical computations on values and variables.
⚬ The values and variables used with operators are called operands.
⚬ In C language, operators can be classified into 6 types:
⚬ Arithmetic Operators
⚬ Assignment Operators
⚬ Relational Operators
⚬ Logical Operators
⚬ Bitwise Operators
⚬ Other Operators
2 . 2 O P E R AT O R I N C :

• 2.2.1 Arithmetic Operator:


⚬ The C arithmetic operators are the symbols that are used to perform
mathematical operations on operands.
⚬ There are a total of 7 arithmetic operators in C
2 . 2 O P E R AT O R I N C :

• 2.2.2 Assignment Operator:


⚬ Assignment operators are used for assigning value to a variable.
⚬ The left side operand of the assignment operator is a variable and
right side operand of the assignment operator is a value.
⚬ The value on the right side must be of the same data-type of the
variable on the left side otherwise the compiler will raise an error.
2 . 2 O P E R AT O R I N C :

• 2.2.2 Assignment Operator:


2 . 2 O P E R AT O R I N C :

• 2.2.3 Relational Operator:


⚬ In C, relational operators are the symbols that are used for
comparison between two values to understand the type of
relationship a pair of numbers shares.
⚬ The result that we get after the relational operation is a boolean
value, that tells whether the comparison is true or false.
⚬ Relational operators are mainly used in conditional statements and
loops to check the conditions in C programming.
2 . 2 O P E R AT O R I N C :

• 2.2.3 Relational Operator:


2 . 2 O P E R AT O R I N C :

• 2.2.4 Logical Operator:


⚬ Logical operators in C are used to combine multiple
conditions/constraints.
⚬ Logical Operators returns either 0 or 1, it depends on whether the
expression result is true or false.
⚬ We have 3 logical operators in the C language:
⚬ Logical AND ( && )
⚬ Logical OR ( || )
⚬ Logical NOT ( ! )
2 . 2 O P E R AT O R I N C :

• 2.2.4 Logical Operator:


2 . 2 O P E R AT O R I N C :

• 2.2.5 Bitwise Operator:


⚬ In C, the following 6 operators are bitwise operators (also known as
bit operators as they work at the bit-level). They are used to perform
bitwise operations in C.
⚬ The & (bitwise AND)
⚬ The | (bitwise OR)
⚬ The ^ (bitwise XOR).
⚬ The << (left shift).
⚬ The >> (right shift).
⚬ The ~ (bitwise NOT).
2 . 2 O P E R AT O R I N C :

• 2.2.5 Bitwise Operator:


DATA STRUCTURE &
ALGORITHMS
THANKS YO U

You might also like