Introduction
C Data types define the nature of variables, dictating the kind of values they can hold and the operations that can be performed on them. From the basic integers and characters to the floating-point numbers and user-defined structures, the right data types serve as the very foundation to build a working C program. C programming is a low level programming language, it means that you, as a developer, have to be on top of every details. One particular detail is the size
of each data type, which determines the range of values a data type can hold. The concept of size
is very important in C programming, misusing and abusing data size normally results in crashes or random behavior of a program.
Common C Data Types
Data Type | Storage Size (byte(s)) | Value Range |
---|---|---|
int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 | 0 to 4,294,967,295 |
short | 2 | -32,768 to 32,767 |
unsigned short | 2 | 0 to 65,535 |
char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
long | 8 | -9223372036854775808 to 9223372036854775807 |
unsigned long | 8 | 0 to 18446744073709551615 |
float | 4 | 1.2E-38 to 3.4E+38 (6 decimal places) |
double | 8 | 2.3E-308 to 1.7E+308 (15 decimal places) |
The Void Data Type and Keyword
void can be a special keyword or a special data type depending on how its use case. It could mean take no arguments
or return no value
when used in a function declaration. When it is used with a pointer *
, it actually means a special data type that could be cast
into any data types. For example.
void myfunction (void * mydata) { /* assuming mydata is allocated some heap memory... */ /* print mydata as a string */ printf("mydata as a string is %s", (char *) mydata); /* print mydata as a number */ printf("mydata as a number is %d", *((int *) mydata)); }
Variable Declaration of a C Data Type
A variable is a named location in computer memory that holds a value. It allows a program to store and manipulate data during its execution. Variables have a specific data type, which determines the size
and type of data
that can be stored. A variable can be declared by specifying a data type followed by a unique name and we have an option to initialize it to a initial value. For example:
void datatypeExample() { /* declare variables without initialization*/ int32 myint; /* declare other variables and initialize them to some values */ uint32 myuint = 1; int16 myshort = -1; unsigned short myushort = 0; char mychar = 'c'; unsigned char myuchar = 0; long mylong = 0; unsigned long myulong = 0; float myfloat = 3.5; double mydouble = 0.0; printf("myint = %d\n"); printf("myuint = %u\n"); }
myint
above is declared without an initial value. What happens if we try to print its value out? printf will print whatever value it has on the stack memory
occupied by the variable myint
. This means, the value could be very random and printf may not print consistent results. This is generally not a good practice. myunit
, on the other hand, is initialized to a value, so it will always print 1 every time.
Remember, always initialize your variable to an initial value. It is generally a good practice and can avoid many debugging troubles
Size is Important in C!
A data type is closely related to the size it occupies in memory. As a good C programmer, you must
be aware of the sizes that your variables occupy, otherwise, you may introduce bugs that are hard to debug. The sizeof() is a convenient routine in C programming that returns the size of an object.
- The size here refers to
the number of bytes the object occupies in memory
. It does not necessarily mean the size of an array, or the length of a string. This is absolutely crucial for you to understand this distinction. - The object here can refers to a
variable
, adata type
, apointer
or astruct
,
For example:
struct user { int userid; char username[64]; unsigned int age; char occupation[64]; }; void datatypeExample() { unsigned char myuchar = 0; unsigned char * myuchar_ptr = NULL; /* it is a good practice to initialize pointer type data to NULL */ struct user myuser = {0}; /* a convenient way to initialize a struct to all zeroes */ int myint_array[8] = {1, 2, 3, 4, 5, 6, 7, 8}; /* anient way to initialize a struct to all zeroes */ printf("sizeof(myuchar) = %ld\n", sizeof(myuchar)); /* print size of myuchar variable */ printf("sizeof(myuchar_ptr) = %ld\n", sizeof(myuchar_ptr)); /* print size of a pointer */ printf("sizeof(struct user) = %ld\n", sizeof(struct user)); /* print size of a struct data type */ printf("sizeof(myint_array) = %ld\n", sizeof(myint_array)); /* print size of an array */ }
The above example will output:
sizeof(myuchar) = 1
sizeof(myuchar_ptr) = 8
sizeof(struct user) = 136
sizeof(myint_array) = 32
ASCII
ASCII stands for American Standard Code for Information Interchange. It’s a character encoding standard used in computers and communication equipment to represent characters and other symbols. In ASCII, a unique numeric code ranging from 0 to 127 represents each character.
C programming also builds on ASCII, and you have an option to print characters either as a number or as a text. For example:
void asciiExample(void) { char mychar = 'C'; int myint = 67; printf("mychar as a character = %c, as a number = %d\n", mychar, mychar); printf("myint as a character = %c, as a number = %d\n", myint, myint); }
The above example will output:
mychar as a character = C, as a number = 67
myint as a character = C, as a number = 67
which indicates that the character C
is equivalent to the number 67
, which is indeed the case according to ASCII. You can find the list of ASCII character tables below. Refer to here for extended ASCII codes.

Summary
Now you know the basis of data types and variable declarations. The next important thing is to understand a variable’s life cycle and scope. This is to say, to understand when a variable is destroyed after it has been declared. Find out in the next chapter.
Recommanded Reads

Hi, this is Cary, your friendly tech enthusiast, educator and author. Currently working as a software architect at Highgo Software Canada. I enjoy simplifying complex concepts, diving into coding challenges, unraveling the mysteries of software. Most importantly, I like sharing and teaching others about all things tech. Find more blogs from me at highgo.ca
Pingback: Explore the Binary System Fundamentals - the Bits and Bytes - techbuddies.io
Pingback: The C Scope is More Important than a Variable - techbuddies.io
Pingback: Create Custom Data Types With the C Struct - techbuddies.io
Pingback: Pointer in C - All Mysteries Revealed Now - techbuddies.io
Pingback: A Deeper Look at Computer Memory - techbuddies.io