Before you start programming in C, you will need to learn the basics of C data types. In many ways, programming simply involves using logic to record, access, and manipulate data. But not all data is made equal. 

Consider that you have “words,” and you have “numbers” in grammar — programming works much like this, but the distinctions between data types are much more specific and much clearer. If you want to write in the appropriate c syntax, you must first understand all these data types.

C Programming For Beginners

Last Updated August 2019

  • 76 lectures
  • All Levels
4.3 (3,759)

Learn C in ten easy steps on Windows, Mac OS X or Linux | By Huw Collingbourne

Explore Course

Why are data types important in C?

C is a strictly typed language. What does this mean? In some other languages, such as PHP, you simply declare a variable, and the program itself determines what type of variable it is. But in C, not only do you need to tell the program what type of variable you’re declaring, but you need to adhere to that. For instance, you cannot tell C that something is a number and then later change it to a string. Types are defined upon creation.

Because data types are so important in that way, many programmers will name their variables related to the type of variable it is. So, for instance, when declaring an int, they might declare it:

int intCount = 1;

But there are many naming conventions for variables, and that’s not always strictly the case. You could just as easily declare an integer as:

int i;
	int count;
	int count_int;

The name of a variable does not necessarily denote its type, though some programmers prefer to denote type in the name of a variable. What’s important is the declaration itself. All data types are included in the declaration of a variable. 

So, you would declare a string like so:

char stringHello = ‘Hello’;

You will get a syntax error if you try to disrupt these types. The following code will not work:

int i = ‘Hello’;

Neither will the following code:

char stringHello = 1;

This is because this is a strictly typed language. To know what to do with each piece of data, C needs to ensure that each piece of data matches its type. Luckily, if you do mix up your typing (which is one of the most common issues people experience with their code), it’s a very simple compiler error to fix. 

Basic C data types

Let’s take a look at the most basic C data types: int, char, float, and double. These aren’t everything you will need to start programming in C, but they are the most commonly used types. More importantly, they are the most simplistic.

Int

Int is short for “integer,” and it’s essentially just “a number.” It is a number without any decimal points; in other words, a whole number. The number 9 can be stored in an int, as can the number 999,999. But the number 99.99 cannot be stored as an int, as it has a decimal point, and it is not a whole number.

Examples of ints: 1, 123, 1234, 12345, -1, -123, -1234, -12345.

In some other languages, you can declare an int “signed” or “unsigned.” In other words, you can declare whether it may be positive or negative (signed) or whether it will always be positive (unsigned). In C, an int is signed.

Integers are frequently used in mathematical equations and operations. In C, you can add integers, divide integers, multiply integers, and so forth. You can even send them into functions that will perform even more complex mathematical operations. Strings, of course, cannot be subject to these mathematical operations.

Ints use very little memory and are extremely efficient to process, but they also don’t hold as much information as a char, float, or double. If you have whole numbers that you want to perform arithmetic on, integers are the best solution. Most programmers today don’t need to worry about storage size.

Char

A “char” in C is also called a “string” variable in other programming languages. It’s essentially a way of storing text. A common “char” declaration is:

char helloWorld = “Hello World.”;

As you can see, the “char” is now holding a whole string. But a char is actually an array, or at least a type of array. In reality, it’s a 13-slot array holding the following:

	H
	e
	l
	l
	o

	W
	o
	r
	l
	d
	.
	\0

When you call the string “helloWorld,” what C is really doing is iterating through every part of its array, from 0 to 12 (which is 13 slots). And it’s 13 slots rather than 12 slots because it needs a final “\0” to terminate the sequence.

Understanding that a “char” is really an array is important for more advanced programming because it explains how char works, how it can be iterated through, and how certain text functions could be performed. As an example, having a char as an array means that you could technically order the array if you wanted — you could sort all those letters in alphabetical order, even though that would be fairly useless.

While integers are usually used for arithmetic, strings are usually just stored as discrete pieces of data that can then be printed out again. They are usually used for login names or encrypted for use as passwords.

Integers can also be further modified. A short int, for instance, is smaller — within the -32,767 and 32,767 range. An unsigned short will be longer because it doesn’t need to consider negative numbers — but an unsigned int cannot contain negative numbers, which can lead to issues if a negative number could potentially occur.

Float

As mentioned, an integer has to be a whole number. But what if you need decimals? A floating point number is used when you need precision in your number. You can declare a float as such:

float floatInt = 12.34;

You can perform the same operations on a float (multiplication, addition, etc.) as you could on an integer. In fact, you can also perform arithmetic between an integer and a float, but if you are saving the data, the data is going to need to go into a floating point number. 

As an example:

	int num = 1;
	float float = 12.34;
	float sum = num * float;

When printing strings, float is printed through an %f.

While floats can be more versatile than ints, they do take up more space. When an integer can be used, it should be used. When a float has to be used, it will consume more memory as well as additional processing. 

Today, with fast computer speeds and virtually unlimited processing power through clouds and virtualized services, the distinction between ints and floats isn’t quite as important from a logistical perspective. But as projects get larger and scale, it can become important, especially in particularly dense applications such as physics or graphics engines. 

Double

A double data type or long double is essentially a float but far more precise because it stores 64 bits. Doubles are frequently used when precision matters or when numbers are particularly large. In other aspects, it works identically to either a float or an int. 

It used to be that the usage of “double” was frowned upon in general because it consumes additional resources in terms of memory. But today, it’s usually the default; most programmers will readily use “double” instead of “float.”

Floats are 32 bits, so a “double” is a double of a float. Floats only enter in when there start to become performance issues relative to using “doubles,” and that is very rare today. For those who are trying to create systems that use very few resources (for instance, systems that are running on weak architecture, such as a small Internet-of-Things device), it may be a general best practice to use floats when possible and doubles elsewhere. For most modern programming needs, the use of floats is somewhat deprecated.

There are some situations in which a double can be used interchangeably with other integer types, and because it is the most forgiving of data sets, it’s often defaulted to.

Derived C data types

A derived type still builds upon the basic C data types. For instance, a “function” is a type of derived C data type; while it does perform a block of code (often a fairly large block of code), it ultimately returns a data type. The three major derived data types are functions, arrays, and pointers.

Functions

A core building block of C programming, functions are discrete blocks of code that receive specific parameters (variables) and return a specific value (such as an integer). Functions perform a specific task in code. Rather than copying and pasting blocks of code repeatedly, a single function can perform a task multiple times. But there are different function types, which also should be understood.

Let’s take a simple look at a function that multiplies two numbers and then returns the result:

	#include<stdio.h>
	void main() {
		int multiplyInts (int one, int two) {
			return one*two;
		}
		printf("%i",multiplyInts(4,5));
		return 0;
	}

The above code declares the function as an int itself. This is what makes it a “derived data type,” because the function itself is basically a way of creating an int. When we call the function (multiplyInts), we send the numbers 4 and 5. The function multiplies those integers and returns those integers. We then print the integers.

Functions can be declared as char, void, etc.; this is what’s important to know from the perspective of a data type. Ultimately, a function is a type of data, even though there is more going on in its code. And the return type can also be void, if necessary.

Arrays

Arrays are one of the most complex data types in C and often take the most practice to understand. The easiest way to think of an array is an ordered list. Let’s say you want to create an array of five numbers. You would create:

int numArray[5] = {1,2,3,4,5};

You’ve now created a list of numbers. And they don’t need to be sequential. It could just as well be 5, 4, 3, 2, 1 or 3, 4, 5, 1, 2. Note that arrays start at 0, so the array actually looks like this:

	0: 1
	1: 2
	2: 3
	3: 4
	4: 5

Not understanding this is the most common way to get fence post errors or off-by-one errors, which are, in turn, some of the most common programming errors — and often the most difficult to catch, as they are a logical error that the compiler often won’t uncover. 

In an array, you determine the types of the derived data types the same way as basic data types; you simply declare it. If you declare an int array, every item of that array must be an int. 

Pointers

In addition to the above basic data types, keep in mind that you can also have pointers to these data types. You would declare a pointer with an asterisk (*) before the variable, and you can print it with an ampersand (&) before the variable. 

Points are complex, and many of their functions are beyond the purview of this article, but they are important to know about. C stores every variable in a specific, allocated memory space — pointers are the way that you directly access and reference this memory space and, ultimately, the way that you allocate and deallocate variables to memory.

Enumerated C data types

An enumerated C data type is also known as a user-defined data type. It is a data type that the user themselves has created. It’s referred to as an “enum” and is used as something quite advanced.

Let’s consider that an employee could be “Onboarding,” “Hired,” or “Terminated.” We want an employee to have only those three statuses; we don’t want someone to enter in a status that we can’t control. And while we could use an array for this, that’s hardly readable; we want to actually be able to say that an employee is onboarded, not that an employee is “0.” 

enum employeeStatus {Onboarding, Hired, Terminated};

Once you have created an enumerated C data type, you can only use those three variables; you will never be allowed to use another variable type. 

Enums are tricky to use, but they should be used whenever this type of strict variable is required because by using enums, you can internally validate your own data. At the same time, it can lead to run-time issues if you aren’t validating your data correctly; if a user tries to enter something other than an acceptable value, for instance, that is going to get rejected. 

Before using enums, make sure that you know exactly which values can and should be stored.

Void data types

Void data types are often included as a type of “basic c data type,” which makes sense, as “null” is almost the most basic type there is. But void is actually the absence of a data type. 

Void is generally used for functions. You would create a function that is:

void myFunction();

But you cannot create a variable this way. You will get an error if you try to do:

void voidVariable;

A variable, by definition, needs a data type and a value. Otherwise, it doesn’t exist. 

Void types are frequently used for functions that are not going to return a data type. They have limited utility beyond this, but they’re important to note because not all functions need or will have a data type.

Using C data types

C data types are important chiefly for two reasons. First, you need to know what data type to use. Not only is this based on the type of information you’re storing, but also processing power and memory usage. In terms of integers, for instance, you might want to use an integer to keep memory usage small, but a float or a double if you’re not concerned about resource usage — and need flexibility more than efficiency.

Second, you need to keep in mind the types of data you’re storing to avoid potential compiler errors. C needs to know the type of variable you’re storing when you first declare it, and it will throw up a compiler error if you use the wrong type. You can never feed a char into an int or an int into a char; while it seems like an innocent error, it’s a quite substantial one.

Luckily, basic C data types are pretty simple — most people are going to be able to use them intuitively very quickly. And while functions, arrays, pointers, and enum types are more advanced, they are also very powerful.

Top courses in C (programming language)

C Programming the Basics
Frank Anemaet
4.4 (43)
New
C Programming For Beginners - Master the C Language
Tim Buchalka's Learn Programming Academy, Jason Fedin
4.4 (21,214)
Bestseller
Advanced C Programming Course
Tim Buchalka's Learn Programming Academy, Jason Fedin
4.5 (1,610)
Learn C++ Programming by Making Games
Serge Lansiquot
4.5 (426)
Build Undetectable Malware Using C Language: Ethical Hacking
Aleksa Tamburkovski, Joe Parys
4.7 (828)
C Programming Language: The Ultimate Guide for Beginners
Aptuts Online Coaching, Prashant Nayak
4.5 (154)
C Programming from scratch- Master C Programming
Priyanka Yadav Desai
4.6 (32)
Bestseller
C Programming For Beginners
Huw Collingbourne
4.3 (3,759)
Advanced C Programming: Pointers
Huw Collingbourne
4.7 (2,325)

More C (programming language) Courses

C (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo