5)

Program 4 - Variables and format specifiers 2

Program 4 Variables and format specifiers 2 Source code - prog4.c
If you cracked the last program okay, then this next one is not a problem. It shows how to declare more than one variable of the same type on a single line and uses more than one format specifier in a printf statement. On top of this it uses the multiple escape sequences in the printf statement to neatly order the output.

This program shouldn't need a great deal of description, as most of what it contains is from past programs, however there is a reference table for the different data types besides the int, that can be used for variables and also a list of some other useful format specifiers including the %d

 

Program code - new parts shown in red

#include <stdio.h>

void main(void)
{

int tom,dick,harry;
tom=20;
dick=32;
harry=27;
printf("Tom is %d years old\nDick is %d years old\nHarry is %d years old\n",
tom,dick,harry);

}

 

Description of the program code
This time no description is given for the new portions of this program, but the output screen is shown below. By combining the two and using some logic, it should be possible to work it out. Instead there is a list of different data types and more format specifiers for reference.
Tom is 20 years old
Dick is 32 years old
Harry is 27 years old

C Data types

The table below shows all the common data types in 'C' that can be used to specify the type of data being stored in a variable. So far only the int data type has been used in the sample programs, but this has not necessarily been the most efficient data type to use. Below the table is a brief part on selecting correct data types for variables.

Type Min Max Desc
char -128 127 Whole numbers
unsigned char 0 255 Whole positive numbers
short -32,768 32,767 Whole numbers
unsigned short 0 65,535 Whole positive numbers
int see note 1 Whole numbers
unsigned int see note 1 Whole positive numbers
long –2,147,483,648 2,147,483,647 Whole numbers
unsigned long 0 4,294,967,295 Whole postive numbers
float see note 2 Fractional numbers
double see note 2 Fractional numbers

Note 1 : The range of integer values depends on the compiler and the operating system a program is running on. If the operating system is a 16 bit O/S like MS DOS then the range of the int and unsigned int data types is the same as the short and unsigned short data types above. If it is a 32 bit operating system like Windows 95 onwards, the range of the int and unsigned int data types is the same as the long and unsigned long data types above.

Note 2 : The range of floating point ( fractional ) for the float and double data types is possible to show but would require a long table or be represented using exponential values. You can achieve better precision with the double data type than the float, but for anything other than scientific applications, which require extreme precision, the float data type is used.

The decision about which data type to use for a variable is governed by what type of data you intend the variable to hold. The general rule is to select a data type that you know with 100% certainty will be adequate. For example, if a variable is to be used to store a persons age, like above, you would probably select the the unsigned char data type, not an int data type as above. The reason the unsigned char would be selected is because there isn't a person who is less than 0 and there certainly isn't a person who is older than 255 !

This leads again to another difference between a game programmer and an application programmer. An application programmer will always look for the smallest data type possible to store data, whilst still allowing scope to cover the entire range of values. So an application programmer would probably use the unsigned char data type to store a persons age. A games programmer on the other hand would use an int. The way memory organisation works, the int is the fastest data type of all to use in a program over all of the others, even smaller ones ! The speed difference it makes is minute, but when calculating and processing thousands, tens of thousands, or millions of them this difference is not so minute.

Printf format specifiers

There are lots of different format specifiers that are used in printf statements besides the %d that has been used so far. Some of the more useful ones are shown in the below with a brief explanation.

%d - Signed decimal integer : This is the one that has been used in the sample programs. It can be used to display either positive or negative whole numbers.

%u - Unsigned decimal integer : This is very similar to %d apart from it can only be used to display positive whole numbers.

%s - String : Strings haven't been covered yet, but in short they are collections of characters to make words, sentences etc.. A person name would be stored in a string for example.

%f - Floating point number : Examples of floating point numbers are : 2.345, 18.0023123, 0.9728 . Some floating point numbers can go on forever and ever, for example 3.33333333 recurring. When displaying floating point numbers you can specify the precision you want to display. Lets say for example, you have the floating point number 18.0023123 stored in a float variable. If you use the format specifier %.3f then the displayed value will be 18.002. The %.3f tells the program to display the value to 3 decimal places. The actual value held in the variable is not changed, it's only displayed in a cut down form. If you was to use the same format specifier with a variable holding 18.0027123 then the displayed value would be 18.003 - note how it's rounded it up.

 

Summary
Although the program for this page isn't very much different than the previous programs, the page does go into more detail on variables, data types and format specifiers. While the ability to store data in memory is useful, it's better still if you can manipulate the data, which is what the next page explains how to do. A computer program consists of four rough parts : Input, Storage, Process, and Output. So far the output part has been covered relatively well, albeit on a text based DOS screen. Internal storage through variables in the computers memory has been touched upon, but now there is a definate need for some processing to create semi-usable programs.

At the moment things are moving along slowly and a DOS text screen is still being used, eventually this will be replaced by a graphical screen and the printf statement won't be used at all, neither will format specifiers or escape sequences, so it's not all that important to understand these in any great detail.

 

Tasks

4.1) Decide on the most efficient data types you would use to store the following :

a) The year a living person was born ( eg 1950, 1962 etc.. )
b) The outside temperature for display on a weather map
c) The price of grocery shop items
d) The distance in yards between any two points in the UK

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001