Introduction to C – Data Types

Following Kochan’s Programming in C, i’ve summarized the data types encountered in C (chapter 4)

Integer int
Integers consist of 1 or more digits, and can have a minus sign for negative numbers. Integers can be expressed in octal and hexadecimal number systems by appending 0 and 0x respectively to an integer.
For example, consider the integer 350. Its octal equivalent is 536 and hex equivalent is 15E (I use Coder’s Toolbox to convert between systems. It has a minimal design, but works quickly).
Usually if we initialize a integer in C it would go like this:

int decimalNum = 350;

And then we prefix 0 and 0x to initialize our octal and hexadecimal numbers:

int octalNum = 0536;
int hexaNum = 0x15E

were we to remove the 0 and 0x prefix, C would treat octalNum and hexaNum as base 10 numbers.
To print octals, the conversion characters are %o and %#o.
To print hexadecimals, the conversion characters are %x and %#x. You can also use %X and %#X. Try them out using printf to see what happens!

Float float
Used to store floating numbers, or numbers with decimal points. As long as there is a number before and after the decimal point, it’s a valid float. (.53, 3., 12.39, and -.23 are valid floats). To print floats, the conversion character is %f. By default, floats have 6 digits after the decimal point. To edit this, you can use, where n is the number of digits you want.
You can also express floats with scientific notation using the mantissa, or the e. For example, 2.3e3 is equivalent to 2.3 x 103, or 2,300. To print floats, use %e, and format the 6 trailing digits with, same as with floats.
To evaluate the scientific notation, you can print it using %f, converting it into a float.
Here’s a small program to differentiate between floats and scientific notation floats, and how different conversion characters can affect them.


int main (void) {

	// Initialize an integer
	int intNum = 20;

	// Initialize a float
	float floatNum = 20.5;

	// Initiialize a float using scientific notation
	// The digits before e are the values of the mantissa
	// The digits after e are the value of the exponent
	float sfNum = 2.05e1;
	// Output our variables to see how they look
	printf ("The integer is %i\n", intNum);
	printf ("The float with default trailing zeroes is %f\n", floatNum);	
	printf ("The float with formatted trailing zeroes is %.2f\n", floatNum);

	// Output the sf float
	printf ("The scientific notation float is %e\n", sfNum);
	printf ("The scientific notation float with formatted trailing zeroes is %.2e\n", sfNum);
	printf ("The sf with float conversion is %f", sfNum);

	return 0;

Would output:

The integer is 20
The float with default trailing zeroes is 20.500000
The float with formatted trailing zeroes is 20.50
The scientific notation float is 2.050000e+01
The scientific notation float with formatted trailing zeroes is 2.05e+01
The sf with float conversion is 20.500000

Doubles double
A more precise float, it can store twice as much significant digits as a float can. The C compiler takes in floats as doubles, unless you specify them to be floats by placing f after the float, for example 20.50f.

Single character char
Used for single characters. They are specified using single quotations, so ‘a’, ‘b’, ‘2’, and ‘;’ are valid chars.
Use %c to display chars in print.

Booleans _Bool
Used for storing 0 and 1, and only has enough memory to do that. The 0 and 1 correspond to False and True, respectively.
Use %i to display booleans in print.

And that’s it! (Aside from strings, which haven’t been mentioned yet). In the next post, I might go over type specifiers.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s