0% found this document useful (0 votes)
10 views6 pages

UNIT-II Data Types & Operators

Unit 2 Data type and operator

Uploaded by

kevalkalal22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

UNIT-II Data Types & Operators

Unit 2 Data type and operator

Uploaded by

kevalkalal22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT II

Data Types & Operators

Data Types: A data-type in C programming is a set of values and is determined to act on


those values. C provides various types of data-types which allow the programmer to select
the appropriate type for the variable to set its value.

C Data Types are used to:

 Identify the type of a variable when it declared.


 Identify the type of the return value of a function.
 Identify the type of a parameter expected by a function.

ANSI C provides three types of data types:

1. Primary(Built-in) Data Types:


void, int, char, double and float.
2. Derived Data Types:
Array, References, and Pointers.
3. User Defined Data Types:
Structure, Union, and Enumeration.

Primary Data Types:

void As the name suggests, it holds no value and is generally used for
specifying the type of function or what it returns. If the function has a
void type, it means that the function will not return any value.

int Used to denote an integer type.

char Used to denote a character type.

float, double Used to denote a floating point type.

int *, float *, char Used to denote a pointer type.


*
Derived Data Types:

Arrays Arrays are sequences of data items having homogeneous values. They
have adjacent memory locations to store values.

References Function pointers allow referencing functions with a particular signature.

Pointers These are powerful C features which are used to access the memory and
deal with their addresses.

User Defined Data Types:

Structure It is a package of variables of different types under a single name. This is


done to handle data efficiently. "struct" keyword is used to define a
structure.

Union These allow storing various data types in the same memory location.
Programmers can define a union with different members, but only a single
member can contain a value at a given time. It is used for

Enum Enumeration is a special data type that consists of integral constants, and
each of them is assigned with a specific name. "enum" keyword is used to
define the enumerated data type.

Enumerated Data Type: Enumeration (or enum) is a user defined data type in C. It is
mainly used to assign names to integral constants, the names make a program easy to read
and maintain.
Example:
#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}

Output: 2
Operators
C programming language offers various types of operators having different functioning
capabilities.

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operator
7. Bitwise Operators
8. Special Operators

Arithmetic Operators:
Arithmetic Operators are used to performing mathematical calculations like addition (+),
subtraction (-), multiplication (*), division (/) and modulus (%).

Relational Operators:
Relational operators are used to comparing two quantities or values.

== Is equal to

!= Is not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

Logical Operators:
C provides three logical operators when we test more than one condition to make decisions.
These are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical
NOT).
&& And operator. It performs logical conjunction of two expressions. (if both expressions
evaluate to True, result is True. If either expression evaluates to False, the result is False)

|| Or operator. It performs a logical disjunction on two expressions. (if either or both


expressions evaluate to True, the result is True)

! Not operator. It performs logical negation on an expression.

Assignment Operators:
Assignment operators applied to assign the result of an expression to a variable. C has a
collection of shorthand assignment operators.

= Assign

+= Increments then assign

-= Decrements then assign

*= Multiplies then assign

/= Divides then assign

%= Modulus then assign

<<= Left shift and assign

>>= Right shift and assign

&= Bitwise AND assign

^= Bitwise exclusive OR and assign

|= Bitwise inclusive OR and assign

Increment and Decrement Operators:

Increment and Decrement Operators are useful operators generally used to minimize the
calculation, i.e. ++x and x++ means x=x+1 or -x and x−−means x=x-1. But there is a slight
difference between ++ or −− written before or after the operand. Applying the pre-increment
first add one to the operand and then the result is assigned to the variable on the left whereas
post-increment first assigns the value to the variable on the left and then increment the operand.

Conditional Operator
C offers a ternary operator which is the conditional operator (? : in combination) to construct
conditional expressions.
Example: x = (5>2? True: False)

Bitwise Operators
C provides a special operator for bit operation between two variables.

<< Binary Left Shift Operator

>> Binary Right Shift Operator

~ Binary Ones Complement Operator

& Binary AND Operator

^ Binary XOR Operator

| Binary OR Operator

Special Operator
C supports some special operators:

sizeof() Returns the size of a memory location.

& Returns the address of a memory location.

* Pointer to a variable.

Storage Classes
Storage Classes are associated with variables for describing the features of any variable or
function in the C program. These storage classes deal with features such as scope, lifetime and
visibility which helps programmers to define a particular variable during the program's
runtime. These storage classes are preceded by the data type which they had to modify
There are four storage classes types in C:

 Auto- auto comes by default with all local variables as its storage class. The keyword
auto is used to define this storage class explicitly
 Register- This storage class is implemented for classifying local variables whose value
needs to be saved in a register in place of RAM (Random Access Memory). This is
implemented when you want your variable the maximum size equivalent to the size
of the register. It uses the keyword register.
 Static- This storage class uses static variables that are used popularly for writing
programs in C language. Static variables preserve the value of a variable even when
the scope limit exceeds. Static storage class has its scope local to the function in which
it is defined
 Extern- The extern storage class is used to feature a variable to be used from within
different blocks of the same program. Mainly, a value is set to that variable which is
in a different block or function and can be overwritten or altered from within another
block as well. Hence it can be said that an extern variable is a global variable which
is assigned with a value that can be accessed and used within the entire program.
Moreover, a global variable can be explicitly made an extern variable by
implementing the keyword 'extern' preceded the variable name.

You might also like