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

Variables and Data Types - C

1) A variable is a container that stores a value of a specified data type. Variables are declared with a data type and name, and optionally initialized with a value. 2) Operators are symbols that perform computations on operands. Common operators include arithmetic, relational, logical, bitwise, and assignment. 3) Data types specify the type of data a variable can store, such as integer, floating point, character, etc. Common C/C++ data types include basic types like int and char, and derived types like arrays and structures.

Uploaded by

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

Variables and Data Types - C

1) A variable is a container that stores a value of a specified data type. Variables are declared with a data type and name, and optionally initialized with a value. 2) Operators are symbols that perform computations on operands. Common operators include arithmetic, relational, logical, bitwise, and assignment. 3) Data types specify the type of data a variable can store, such as integer, floating point, character, etc. Common C/C++ data types include basic types like int and char, and derived types like arrays and structures.

Uploaded by

adityakmcs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1

VARIABLES AND DATA TYPES (C/C++)

Q. What is a Variable?
In Simple Language: -

A variable is a Container that stores a value that value can be any type like characters, decimal numbers,
floating numbers etc. And these values can be changed during the execution of the program.

Example:
int var_name = 5 ;

Data Type Variable Name Value Stored in Variable

Ex. 1.1

 A name given to the memory location.


 Declared by writing type variable_name;
 Initialized and declared by type variable_name = value; as given in example.
 Declaration means Reserving a location in memory for a variable.
Example:
int var_name, var_name2, a, s;

 Initialization means assigning a value after declaration of variable as example in Ex. 1.1.

Rules for defining variables: -

1. A variable can have alphabets, digits, and underscore.


2. A variable name can start with the alphabet, and underscore only. It can’t start with a digit.
3. No whitespace is allowed within the variable name.
4. A variable name must not be any reserved word or keyword, e.g., int, goto, etc.
 Valid variable names: int ady, char aditya_shahi, float _aditya1233
 Invalid variables name: int @aditya, int 12aditya, char long

Q. What is Data Types?

Data types simply refers to the type and size of data associated with variables.
OR
A data type specifies the type of data that a variable can store such as integer, floating, character etc. Data
types in C.

DATA TYPES IN C: -
 Basic Data Types: int, char, float, double
 Derived Data Types: array, pointer, structures, union
 Enumeration data Type: enum
 Void Data Type: void (means Empty)

By Aditya Shahi
2

Data Types Memory Size Range Format


Specifier

char 1 byte −128 to 127 %c

signed char 1 byte −128 to 127 %c

unsigned char 1 byte 0 to 255 %c

int 2 bytes −32,768 to 32,767 %d

unsigned int 2 bytes 0 to 65,535 %u

short int 2 bytes −32,768 to 32,767 %hd

signed short int 2 bytes −32,768 to 32,767 %hi

unsigned short int 2 bytes 0 to 65,535 %hu

long int 4 bytes -2,147,483,648 to 2,147,483,647 %ld

Long long int 8 bytes -(2^63) to (2^63)-1 %lld

Unsigned long long int 8 bytes 0 to 18,446,774,073,709,551,615 %llu

unsigned long int 4 bytes 0 to 4,294,967,295 %lu

float 4 bytes %f

double 8 bytes %lf

long double 10 bytes %Lf

By Aditya Shahi
3

OPERATORS IN C/C++

Q. What is Operators?

We can define operators as symbols that help us to perform specific mathematical and logical
computations on operands. In other words, we can say that an operator operates the operands. For
example, ‘+’ is an operator used for addition, For Example:
c = a + b;
a = 3 + 4 ;
+ b;
c = a + b; Operands Operator

Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are operands. The addition
operator tells the compiler to add both of the operands ‘a’ and ‘b’.

C/C++ has many built-in operators and can be classified into 6 types:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators

By Aditya Shahi
4

 Arithmetic Operators:

These operators are used to perform arithmetic/mathematical operations on operands. Examples: (+, -, *,
/, %, ++, –). Arithmetic operators are of two types:

a) Unary Operators: Operators that operate or work with a single operand are unary operators. For
example: Increment (++) and Decrement (--) Operators

b) Binary Operators: Operators that operate or work with two operands are binary operators. For
example: Addition (+), Subtraction (-), multiplication (*), Division (/) operators

 Relational Operators:
These are used for the comparison of the values of two operands. For example, checking if one operand is
equal to the other operand or not, whether an operand is greater than the other operand or not, etc.
Some of the relational operators are (==, >= , <= )

int main () {

int a= 4;
int b = 2;
a>b; // operator to check a is smaller than b

return 0;
}

 3. Logical Operators:
Logical Operators are used to combine two or more conditions/constraints or to complement the
evaluation of the original condition in consideration. The result of the operation of a logical operator is a
Boolean value either true or false.

For example, the logical AND represented as ‘&&’ operator in C or C++ returns true when both the
conditions are true. Otherwise, it returns false. Therefore, a && b returns true when both a and b are true
(i.e., non-zero)
x; // true
 Bitwise Operators:
The Bitwise operators are used to perform bit-level operations on the operands. The operators are first
converted to bit-level and then the calculation is performed on the operands. Mathematical operations
such as addition, subtraction, multiplication, etc. can be performed at the bit-level for faster processing.
For example, the bitwise AND represented as & operator in C or C++ takes two numbers as operands and
does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
cout << (a ^ b); // 00001100
cout <<(~a); // 11111010

By Aditya Shahi
5

 Assignment Operators:
Assignment operators are used to assign value to a variable. The left side operand of the assignment
operator is a variable and the right-side operand of the assignment operator is a value. The value on the
right side must be of the same data type as the variable on the left side otherwise the compiler will raise an
error.

1. “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to
the variable on the left.
For example:
a = 10;
b = 20;
char ch = 'y';

2. “+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of
the variable on left to the value on the right and then assigns the result to the variable on the left.
For example:

(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11.

3. “-=”: This operator is a combination of ‘-‘ and ‘=’ operators. This operator first subtracts the value on
the right from the current value of the variable on left and then assigns the result to the variable on the
left.
For example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) = 2.

4. “*=”: This operator is a combination of ‘*’ and ‘=’ operators. This operator first multiplies the current
value of the variable on left to the value on the right and then assigns the result to the variable on the left .
For example:
(a *= b) can be written as (a = a * b)
If initially, the value stored in a is 5. Then (a *= 6) = 30.

5. “/=”: This operator is a combination of ‘/’ and ‘=’ operators. This operator first divides the current
value of the variable on left by the value on the right and then assigns the result to the variable on
the left.
For example:
6.
(a /= b) can be written as (a = a / b)
If initially, the value stored in a is 6. Then (a /= 2) = 3.

By Aditya Shahi
6

 Other Operators:

Apart from the above operators, there are some other operators available in C or C++ used to perform
some specific tasks. Some of them are discussed here:

a. sizeof operator:
 sizeof is much used in the C/C++ programming language.
 It is a compile-time unary operator which can be used to compute the size of its operand.
 The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
 Basically, the sizeof the operator is used to compute the size of the variable.

b. Comma Operator:
 The comma operator (represented by the token) is a binary operator that evaluates its first operand
and discards the result, it then evaluates the second operand and returns this value (and type).
 The comma operator has the lowest precedence of any C operator.
 Comma acts as both operator and separator.

c. Conditional Operator (ternary operator):


 The conditional operator is of the form Expression1? Expression2: Expression3.
 Here, Expression1 is the condition to be evaluated. If the condition (Expression1) is True then we
will execute and return the result of Expression2 otherwise if the condition (Expression1)
is false then we will execute and return the result of Expression3.
 We may replace the use of if..else statements with conditional operators.

d. dot (.) and arrow (->) Operators:


 Dot/Arrow operators are used to reference individual members of classes, structures, and unions.
 The dot operator is applied to the actual object.
 The arrow operator is used with a pointer to an object.

e. Cast Operator:
 Casting operators convert one data type to another. For example, int(2.2000) would return 2.
 A cast is a special operator that forces one data type to be converted into another.
 The most general cast supported by most of the C++ compilers is as follows − [ (type) expression ].

f. &, * Operator:
 Pointer operator & returns the address of a variable. For example, &a; will give the actual address
of the variable.
 Pointer operator * is a pointer to a variable. For example, *var; will pointer to a variable var.

By Aditya Shahi

You might also like