Data Types in C
Data Types in C
A data type specifies the type of data that a variable can store such
as integer, floating, character, etc.
Let's see the basic data types. Its size is given according to 32-bit architecture.
Int:
Integers are entire numbers without any fractional or decimal parts, and the int data
type is used to represent them.
It is frequently applied to variables that include values, such as counts, indices, or other
numerical numbers. The int data type may represent both positive and negative
numbers because it is signed by default.
An int takes up 4 bytes of memory on most devices, allowing it to store values between
around -2 billion and +2 billion.
Char:
Individual characters are represented by the char data type. Typically used to
hold ASCII or UTF-8 encoding scheme characters, such as letters, numbers, symbols,
or commas. There are 256 characters that can be represented by a single char, which
takes up one byte of memory. Characters such as 'A', 'b', '5', or '$' are enclosed in single
quotes.
Float:
To represent integers, use the floating data type. Floating numbers can be used to
represent fractional units or numbers with decimal places.
The float type is usually used for variables that require very good precision but may not
be very precise. It can store values with an accuracy of about 6 decimal places and a
range of about 3.4 x 1038 in 4 bytes of memory.
Double:
Use two data types to represent two floating integers. When additional precision is
needed, such as in scientific calculations or financial applications, it provides greater
accuracy compared to float.
Double type, which uses 8 bytes of memory and has an accuracy of about 15 decimal
places, yields larger values. C treats floating point numbers as doubles by default if no
explicit type is supplied.
In the Example, we declare four variables: an int variable for the
person's age, a char variable for the student's grade, a float
variable for the temperature reading, and two variables for
the number pi.
Derived Data Type
C also supports derived data types, including arrays, pointers, structures, and unions.
These data types give programmers the ability to handle heterogeneous data, directly
modify memory, and build complicated data structures.
Array:
An array, a derived data type, lets you store a sequence of fixed-size elements of the
same type. It provides a mechanism for joining multiple targets of the same data under
the same name.
The index is used to access the elements of the array, with a 0 index for the first entry.
The size of the array is fixed at declaration time and cannot be changed during program
execution. The array components are placed in adjacent memory regions.
A pointer is a derived data type that keeps track of another data type's
memory address. When a pointer is declared, the data type it refers to
is stated first, and then the variable name is preceded by an asterisk (*).
You can have incorrect access and change the value of variable using pointers
by specifying the memory address of the variable. Pointers are commonly
used in tasks such as function pointers, data structures, and dynamic
memory allocation.
• A structure's members or fields are used to refer to each variable within it.
• Any data type, including different structures, can be a member of a structure.
• A structure's members can be accessed by using the dot (.) operator.
Example:
Function Parameters:
The parameter void can be used to indicate that a function accepts no arguments.
Example:
Pointers:
Any address can be stored in a pointer of type void*, making it a universal pointer. It
offers a method for working with pointers to ambiguous or atypical types.
Example:
Ahmed Mostafa