
Data Types in C
There are several different ways to store data in C, and they are all unique from each other. The types of data that information can be stored as are called data types. C is much less forgiving about data types than other languages. As a result, it’s important to make sure that you understand the existing data types, their abilities, and their limitations.
One quirk of C’s data types is that they depend entirely on the hardware that you’re running your code on. An int
on your laptop will be smaller than an int
on a supercomputer, so knowing the limitations of the hardware you’re working on is important. This is also why the data types are defined as being minimums- an int
value, as you will learn, is at minimum -32767 to 32767: on certain machines, it will be able to store even more values that this.
There are two categories that we can break this into: integers, and floating point numbers. Integers are whole numbers. They can be positive, negative, or zero. Numbers like -321, 497, 19345, and -976812 are all perfectly valid integers, but 4.5 is not because 4.5 is not a whole number.
Floating point numbers are numbers with a decimal. Like integers, -321, 497, 19345, and -976812 are all valid, but now 4.5, 0.0004, -324.984, and other non-whole numbers are valid too.
C allows us to choose between several different options with our data types because they are all stored in different ways on the computer. As a result, it is important to be aware of the abilities and limitations of each data type to choose the most appropriate one.
Integer data types
Characters: char
char
holds characters- things like letters, punctuation, and spaces. In a computer, characters are stored as numbers, so char
holds integer values that represent characters. The actual translation is described by the ASCII standard. Here’s a handy table for looking up that.
The actual size, like all other data types in C, depends on the hardware you’re working on. By minimum, it is at least 8 bits, so you will have at least 0 to 127. Alternatively, you can use signed char
to get at least -128 to 127.
Standard Integers: int
The amount of memory that a single int
takes depends on the hardware. However, you can expect an int
to be at least 16 bits in size. This means that it can store values from -32,768 to 32,767, or more depending on hardware.
Like all of these other data types, there is an unsigned
variant that can be used. The unsigned int
can be positive and zero but not negative, so it can store values from 0 to 65,535, or more depending on hardware.
Short integers: short
This doesn’t get used often, but it’s good to know that it exists. Like int, it can store -32768 to 32767. Unlike int, however, this is the extent of its ability. Anywhere you can use short
, you can use int
.
Longer integers: long
The long
data type stores integers like int
, but gives a wider range of values at the cost of taking more memory. Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647. Alternatively, use unsigned long
for a range of 0 to 4,294,967,295.
Even longer integers: long long
The long long
data type is overkill for just about every application, but C will let you use it anyway. It’s capable of storing at least −9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. Alternatively, get even more overkill with unsigned long long
, which will give you at least 0 to 18,446,744,073,709,551,615.
Floating point number data types
Basic Floating point numbers: float
float
takes at least 32 bits to store, but gives us 6 decimal places from 1.2E-38 to 3.4E+38.
Doubles: double
double
takes double the memory of float (so at least 64 bits). In return, double can provide 15 decimal place from 2.3E-308 to 1.7E+308.
Getting a wider range of doubles: long double
long double
takes at least 80 bits. As a result, we can get 19 decimal places from 3.4E-4932 to 1.1E+4932.
Picking the right data type
C makes pick the data type, and makes us be very specific and intentional about the way that we do this. This gives you a lot of power over your code, but it’s important to pick the right one.
In general, you should pick the minimum for your task. If you know you’ll be counting from integer 1 to 10, you don’t need a long and you don’t need a double. If you know that you will never have negative values, look into using the unsigned
variants of the data types. By providing this functionality rather than doing it automatically, C is able to produce very light and efficient code. However, it’s up to you as the programmer to understand the abilities and limitations, and choose accordingly.
We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:
#include <stdio.h>int main(){ int a = 1; char b ='G'; double c = 3.14; printf("Hello World!\n"); //printing the variables defined above along with their sizes printf("Hello! I am a character. My value is %c and " "my size is %lu byte.\n", b,sizeof(char)); //can use sizeof(b) above as well printf("Hello! I am an integer. My value is %d and " "my size is %lu bytes.\n", a,sizeof(int)); //can use sizeof(a) above as well printf("Hello! I am a double floating point variable." " My value is %lf and my size is %lu bytes.\n",c,sizeof(double)); //can use sizeof(c) above as well printf("Bye! See you soon. :)\n"); return 0;}
Output:
Hello World!Hello! I am a character. My value is G and my size is 1 byte.Hello! I am an integer. My value is 1 and my size is 4 bytes.Hello! I am a double floating point variable. My value is 3.140000 and my size is 8 bytes.Bye! See you soon. :)
The Void type
The void type specifies that no value is available. It is used in three kinds of situations:
1. Function returns as void
There are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);
2. Function arguments as void
There are various functions in C which do not accept any parameter. A function with no parameter can accept a void. For example, int rand(void);
3. Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size);
returns a pointer to void which can be casted to any data type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
If this article was helpful, .
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
ADVERTISEMENT
FAQs
Data Types in C - Integer, Floating Point, and Void Explained? ›
The integer data type represents a positive whole number or its negative value. Examples of integers are 0 , 1 , 2 , 3 and 4 . The float data type represents a floating-point or decimal number. Examples of floats are 0.1243 and 12.245 .
What is the data type of int and float in C? ›- int − Used to store an integer value.
- char − Used to store a single character.
- float − Used to store decimal numbers with single precision.
- double − Used to store decimal numbers with double precision.
The integer data type represents a positive whole number or its negative value. Examples of integers are 0 , 1 , 2 , 3 and 4 . The float data type represents a floating-point or decimal number. Examples of floats are 0.1243 and 12.245 .
What is a void data type in C? ›The void data type always represents an empty set of values. The only object that can be declared with the type specifier void is a pointer. You cannot declare a variable of type void , but you can explicitly convert any expression to type void .
What are different data types in C explain? ›Data Type | Example of Data Type |
---|---|
Basic Data Type | Floating-point, integer, double, character. |
Derived Data Type | Union, structure, array, etc. |
Enumerated Data Type | Enums |
Void Data Type | Empty Value |
0.0f is a float
Your second example has an integer initialization value. You should always use an initializer of the same type as the variable that is being initialized.
Double is more precise than float and can store 64 bits; double the number of bits float can store. We prefer double over float if we need to do precision up to 15 or 16 decimal points; otherwise, we can stick to float in most applications, as double is more expensive.
What are the 2 types of floating-point? ›- float.
- double.
- long double.
A "floating-point constant" is a decimal number that represents a signed real number. The representation of a signed real number includes an integer portion, a fractional portion, and an exponent.
What is the difference between float and data type? ›Difference between double and float Java types. The key difference between a float and double in Java is that a double can represent much larger numbers than a float. Both data types represent numbers with decimals, but a float is 32 bits in size while a double is 64 bits.
What is the data type of int in C? ›
Integer Data Type
An integer type variable can store zero, positive, and negative values without any decimal. In C language, the integer data type is represented by the 'int' keyword, and it can be both signed or unsigned. By default, the value assigned to an integer variable is considered positive if it is unsigned.
Of course you can add an int to float . C will convert the int to float before adding. However, in the expression (float)15/2 + 15/2 , the first 15/2 will be calculated as float and the second 15/2 will be an integer division first, and that result converted to float.
Which data type consists of both integer type and floating type? ›"Number" data type is used to store both integer and float values.
What data type is float? ›In computer science, a float is a data type composed of a number that is not an integer, because it includes a fraction represented in decimal format.