0% found this document useful (0 votes)
40 views7 pages

3151 - Data Types in C

3151_Data Types In C

Uploaded by

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

3151 - Data Types in C

3151_Data Types In C

Uploaded by

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

Data Types In C

In C, every variable has a data type. The data type determines the kind of data the variable can hold,
such as an integer, character, floating point, or double. Each data type uses a different amount of
memory. It also has specific operations that can be performed on it. A data type is a collection of
data with fixed values, a defined meaning, and particular characteristics.

The data types in C can be classified as follows:

Types Description

Primitive Data Primitive data types are the most basic data types that are used for
Types representing simple values such as integers, float, characters, etc.

User Defined
The user-defined data types are defined by the user himself.
Data Types

The data types that are derived from the primitive or built-in
Derived Types
datatypes are referred to as Derived Data Types.

Different data types can store numbers within specific ranges. These ranges can vary depending on
the compiler. Below is a list showing the ranges, memory requirements, and format specifiers for
each data type on a 32-bit GCC compiler.
Size Format
Data Type (bytes) Range Specifier

short int 2
-32,768 to 32,767 %hd

unsigned short
2 0 to 65,535 %hu
int

unsigned int 4 0 to 4,294,967,295 %u

-2,147,483,648 to
int 4 %d
2,147,483,647

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

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

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

unsigned long 0 to
8 %llu
long int 18,446,744,073,709,551,615

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

float 4 %f
1.2E-38 to 3.4E+38

double 8 %lf
1.7E-308 to 1.7E+308
Size Format
Data Type (bytes) Range Specifier

long double 16 %Lf


3.4E-4932 to 1.1E+4932

Note: The long, short, signed and unsigned are datatype modifier that can be used with
some primitive data types to change the size or length of the datatype.

Int (Integers)

Integer can be signed and unsigned. Signed integer allows positive and negative values in
variables whereas unsigned int allows only positive values.

#include <stdio.h>
#include <limits.h>

int main() {
// short int
short int si = 32767;
printf("short int: %hd\n", si);

// unsigned short int


unsigned short int usi = 65535;
printf("unsigned short int: %hu\n", usi);
// unsigned int
unsigned int ui = 4294967295;
printf("unsigned int: %u\n", ui);

// int
int i = 2147483647;
printf("int: %d\n", i);

// long int
long int li = 2147483647;
printf("long int: %ld\n", li);

// unsigned long int


unsigned long int uli = 4294967295;
printf("unsigned long int: %lu\n", uli);

// long long int


long long int lli = 9223372036854775807;
printf("long long int: %lld\n", lli);

// unsigned long long int


unsigned long long int ulli = 18446744073709551615U;
printf("unsigned long long int: %llu\n", ulli);

return 0;
}
Char
#include <stdio.h>
int main() {
// signed char
signed char sc = 127;
printf("signed char: %c (as char), %d (as int)\n", sc, sc);

// unsigned char
unsigned char uc = 255;
printf("unsigned char: %c (as char), %u (as int)\n", uc, uc);

return 0;
}
Character can be signed and unsigned. Signed characters allows negative values whereas
unsigned character allows only positive values within the range.

Float
 float: Declares a floating-point variable, which can hold single-precision values. %f
is the format specifier for float, and it prints the value with six decimal places by
default.
 double: Declares a double-precision floating-point variable. %lf is the format
specifier for double, though in printf, %f can also be used interchangeably due to
implicit type promotion.
 long double: Declares an extended-precision floating-point variable. %Lf is the
format specifier for long double.
#include <stdio.h>
int main() {
// float
float f = 3.4E+38;
printf("float: %f\n", f);
// double
double d = 1.7E+308;
printf("double: %lf\n", d);
// long double
long double ld = 1.1E+4932;
printf("long double: %Lf\n", ld);

return 0;
}
Important: how size determines the data range of the signed and unsigned variable?
The size of a data type in memory determines the range of values it can store because it
dictates how many bits are available to represent the values. Here's how size affects the
range for both signed and unsigned variables:
Unsigned Variables
 Unsigned variables can only represent non-negative values (0 and positive
numbers).
 If a variable is N bits in size, the range of values it can store is from 0 to 2^N - 1.
 Example: An unsigned 8-bit variable (unsigned char) can store values from 0 to 255:
o Range: 0 to 2^8 - 1 = 0 to 255.
Signed Variables
 Signed variables can represent both positive and negative values.
 In a signed variable, the most significant bit (MSB) is used to represent the sign:
o 0 for positive numbers.
o 1 for negative numbers.
 If a variable is N bits in size, the range of values it can store is from -2^(N-1) to
2^(N-1) - 1.
 Example: A signed 8-bit variable (signed char) can store values from -128 to 127:
o Range: -2^7 to 2^7 - 1 = -128 to 127.
Why This Happens:
 Unsigned Variables: All bits are used to represent the magnitude of the number,
allowing for a full range from 0 to the maximum possible value.
 Signed Variables: One bit is reserved for indicating the sign, which reduces the
range of positive numbers and allows for representing negative values as well.
Summary of Ranges Based on Size:
 1 byte (8 bits):
o Unsigned: 0 to 255
o Signed: -128 to 127
 2 bytes (16 bits):
o Unsigned: 0 to 65,535
o Signed: -32,768 to 32,767
 4 bytes (32 bits):
o Unsigned: 0 to 4,294,967,295
o Signed: -2,147,483,648 to 2,147,483,647
 8 bytes (64 bits):
o Unsigned: 0 to 18,446,744,073,709,551,615
o Signed: -(2^63) to (2^63) - 1
The number of bits available directly determines the number of possible values, hence
determining the range of the data type.

You might also like