How C Programming Works

Data Types and Operations in C

From your computer's point of view, your program is all just a series of ones and zeros. Data types in C tell the computer how to use some of those bits.
From your computer's point of view, your program is all just a series of ones and zeros. Data types in C tell the computer how to use some of those bits.

From your computer's perspective, data is nothing but a series of ones and zeros representing on and off states for the electronic bits on your hard drive or in your computer's processor or memory. It's the software you're running on a computer that determines how to make sense of those billions of binary digits. C is one of few high-level languages that can easily manipulate data at the bit level in addition to interpreting the data based on a given data type.

A data type is a small set of rules that indicate how to make sense of a series of bits. The data type has a specific size plus its own way of performing operations (such as adding and multiplying) on data of that type. In C, the size of the data type is related to the processor you're using. For example, in C99, a piece of data of the integer data type (int) is 16 bits long in a 16-bit processor while for 32-bit and 64-bit processors it's 32 bits long.

Another important thing for C programmers to know is how the language handles signed and unsigned data types. A signed type means that one of its bits is reserved as the indicator for whether it's a positive or negative number. So, while an unsigned int on a 16-bit system can handle numbers between 0 and 65,535, a signed in on the same system can handle numbers between -32,768 and 32,767. If an operation causes an int variable to go beyond its range, the programmer has to handle the overflow with additional code.

Given these constraints and system-specific peculiarities in C data types and operations, C programmers must choose their data types based on the needs of their programs. Some of the data types they can choose are the primitive data types in C, meaning those built in to the C programming language. Look to your favorite C programming guide for a complete list of the data types in C and important information about how to convert data from one type to another.

C programmers can also create data structures, which combine primitive data types and a set of functions that define how the data can be organized and manipulated. Though the use of data structures is an advanced programming topic and beyond the scope of this article, we will take a look at one of the most common structures: arrays. An array is a virtual list containing pieces of data that are all the same data type. An array's size can't be changed, though its contents can be copied to other larger or smaller arrays.

Though programmers often use arrays of numbers, character arrays, called strings, have the most unique features. A string allows you to save something you might say (like "hello") into a series of characters, which your C program can read in from the user or print out on the screen. String manipulation has such a unique set of operations, it has its own dedicated C library (string.h) with your typical string functions.

The built-in operations in C are the typical operations you'd find in most programming languages. When you're combining several operations into a single statement, be sure to know the operator precedence, or the order in which the program will perform each operation in a mathematical expression. For example, (2+5)*3 equals 21 while 2+5*3 equals 17, because C will perform multiplication before addition unless there are parentheses indicating otherwise.

If you're learning C, make it a priority to familiarize yourself with all of its primitive data types and operations and the precedence for operations in the same expression. Also, experiment with different operations on variables and numbers of different data types.

At this point, you've scratched the surface of some important C basics. Next, though, let's look at how C enables you to write programs without starting from scratch every time.