Basics of Values in C Programming - dummies

By Dan Gookin

Computers deal with both numbers and text. In C programming, text comes in the form of individual characters or a parade of characters all lumped together into a string. Numbers are pretty much numbers until you get into huge values and fractions. The computer understands everything, as long you properly inform your program of which values are which.

You’ve probably dealt with numbers all your life, virtually tortured by them throughout your schooling. You may recall terms such as whole number, fraction, real number, and imaginary number. Ignore them! These terms mean nothing in computer programming.

When it comes to programming, you use only two types of numbers:

  • Integer

  • Float

An integer is a whole number — no fractional part. It can be positive. It can be negative. It can be a single digit, zero, or humongous value such as the amount of money the U.S. government spends in a week (no cents). All these numbers are integers in computer programming jargon.

A float is a number that has a fractional part — a decimal place. It can be a very, very small number, like the width of a proton. It can be a very, very large number, like the hat size of the planet Jupiter.

  • Examples of integers: –13, 0, 4, and 234792.

  • In programming, you don’t type commas in large values.

  • Examples of floats are 3.14, 0.1, and 6.023e23. That last number is written in scientific notation, which means that it’s the value 6.023 × 1023 — a huge number. (It’s Avogadro’s number, which is another term you’ve probably forgotten from school.)

  • Integers and floats can be either positive or negative.

  • Integers are judged by their size, as are floats. The size comes into play when you create storage places for numbers in your programs.

  • The term float is short for floating point. It refers to the method that’s used to store large numbers and fractions in the binary counting system of modern electronics.

How to display values with printf()

The printf() function is ideal for displaying not only strings of text but also values. To make that happen, you use conversion characters in the function’s formatting string. Rather than bore you with a description, consider Exercise 1.

Exercise 1: Start up a new project, ex0501, using the source code illustrated in Displaying Various Values. Save the project. Build it. Run it.


#include <stdio.h>
int main()
  printf("The value %d is an integer.n",986);
  printf("The value %f is a float.n",98.6);

The output looks something like this:

The value 869 is an integer.
The value 98.600000 is a float.

You’re probably surprised that the output doesn’t look like this:

The value %d is an integer.
The value %f is a float.

It doesn’t, because the text included in a printf() function isn’t merely text — it’s a formatting string.

The printf() function’s formatting string can contain plain text, escape sequences, and conversion characters, such as the %d in Line 5 and the %f in Line 6. These conversion characters act as placeholders for values and variables that follow the formatting string.

For the %d placeholder, the integer value 986 is substituted. The %d conversion character represents integer values.

For the %f placeholder, the float value 98.6 is substituted. The %f conversion character represents floating-point values. Of course, 98.6 isn’t displayed. Instead, you see 98.600000.

The %d and %f are only two of many placeholders for the printf() function’s formatting string.

Exercise 2: Create a project that displays the following values by using the printf() function and the appropriate conversion characters, either %d or %f:





Do not type a comma when specifying a value in your C language source code.

When typing a small floating-point value, remember to prefix the decimal point with a zero, as just shown, with 0.00008. Likewise, when typing a float value without a decimal part, type the decimal and a zero anyway:


Beware the extra zeroes

When you wrote the code for Exercise 1, you probably expected the program’s output to display the value 98.6, just as it’s written. The problem is that you directed the printf() function to output that number in an unformatted manner — for example, 98.600000. In fact, you may see more or fewer zeros, depending on your compiler.

The value 98.600000 is a floating-point number, and it most likely represents the way the value is stored inside the computer. Specifically, the value is stored using eight digits. The number is accurate, of course, but human beings don’t usually write trailing zeros after numbers. Computers? They write as many zeros as fills eight digits (not counting the decimal).

To fix the output, direct the printf() function to format the floating-point number. That requires a more complex version of the %f placeholder. Specifically, change the %f placeholder to read %2.1f. Here’s the new Line 6:

  printf("The value %2.1f is an float.n",98.6);

By squeezing 2.1 between the % and the f, you direct printf() to format the output with two digits to the left of the decimal and one digit to the right.

Exercise 3: Modify your source code from Exercise 5-2 so that the value 3.1415926535 is displayed by using the %1.2f placeholder, and the value 0.00008 is displayed by using the %1.1f placeholder.