Types in C
In C, types are categorized into several basic categories: integer types, floating-point types, and other types such as void and pointer types. Here's an overview of the types in C, along with sample code snippets and the typical range of values they can represent. Note that the exact range of values can vary by implementation, as the C standard only provides minimum sizes for each data type.
Integer Types:
char
Used to store characters.
Range: Typically -128 to 127 for
signed char
, and 0 to 255 forunsigned char
.Size: At least 8 bits.
char a = 'A';
unsigned char b = 255;
int
The most natural size of integer for the machine.
Range: Typically -32,768 to 32,767 for
signed int
, and 0 to 65,535 forunsigned int
.Size: At least 16 bits.
int x = 123;
unsigned int y = 456U;
short
Short integer.
Range: Typically -32,768 to 32,767 for
signed short
, and 0 to 65,535 forunsigned short
.Size: At least 16 bits.
short s = 1000;
unsigned short t = 2000U;
long
Long integer.
Range: Typically -2,147,483,648 to 2,147,483,647 for
signed long
, and 0 to 4,294,967,295 forunsigned long
.Size: At least 32 bits.
long l = 100000L;
unsigned long m = 100000UL;
long long
Longer integer type.
Range: Typically -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 for
signed long long
, and 0 to 18,446,744,073,709,551,615 forunsigned long long
.Size: At least 64 bits.
long long ll = 123456789LL;
unsigned long long ull = 123456789ULL;
Floating-Point Types:
float
Single-precision floating-point.
Range: Typically 1.2E-38 to 3.4E+38 (6 decimal places).
float f = 123.456f;
double
Double-precision floating-point.
Range: Typically 2.3E-308 to 1.7E+308 (15 decimal places).
double d = 123.456;
long double
Extended precision floating-point.
Range and precision can vary, but it is at least as precise as
double
.
long double ld = 123.456L;
Other Types:
void
Type with no values.
Often used to specify the return type of functions that do not return a value.
void myFunction() {
// Do something
}
Pointers
A variable that stores the address of another variable.
The range depends on the architecture (32-bit or 64-bit).
int z = 10;
int *p = &z;
Sample Code Using Various Types:
#include <stdio.h>
int main() {
char c = 'A';
int i = 1;
short sh = 2;
long l = 1234567890L;
long long ll = 1234567890123456789LL;
float f = 123.456f;
double d = 123.456789;
long double ld = 123.45678910111213L;
printf("char: %c\n", c);
printf("int: %d\n", i);
printf("short: %hd\n", sh);
printf("long: %ld\n", l);
printf("long long: %lld\n", ll);
printf("float: %f\n", f);
printf("double: %lf\n", d);
printf("long double: %Lf\n", ld);
return 0;
}
Remember that the actual size and range of these types can vary depending on the system and compiler implementation. To determine the exact size and range for your system, you can use the limits.h
and float.h
header files which define constants for these properties.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.