Introduction to Programming and C++

Contents Previous Topic Next Topic

To begin, if you want to compile C in Unix (for example, on ecelinux), use gcc instead of g++. The executable is still named a.out and may be run by typing ./a.out at the command line. If you are using Dev C++, when you create a new Project (File → New → Project), in the bottom right corner of the New project window, you will be able to select to make this a C Project instead of a C++ Project through the selection of the appropriate radio button. Dev C++ also, by default, uses gcc.

The first feature we will look at is printing in C. In C++, we used the cout object with an overloaded redirection operator <<. In C, printing is much functional. To demonstrate, Program 1 shows the printf function. The function name stands for print format. The include file is stdio.h which stands for standard input and output, however is sometimes pronounced studio-dot-aich or standard-eye-oh.

Program 1. Hello World!

#include <stdio.h>

int main() {
	printf( "Hello World!\n" );

	return 0;
}

The first argument of printf is a string which is displayed to the screen. You will note that we must hard-code new lines using the character '\n'.

The next thing you may want to do is actually print useful information. The way that printf allows you to print, for example, integers, is in a manner similar to method of using \ to escape special characters. If you want an integer to print to the screen, place %d in the location where you want the integer to appear within the string and then add the integer it as an additional argument to printf. See Program 2.

Program 2. Printing an integer.

#include <stdio.h>

int main() {
	printf( "This is my %dnd C program\n", 2 );

	return 0;
}

If you want to print more than one integer, you simply place as many %d (%d is called a conversion) in the string as necessary and place the integers in the same order as additional arguments, as is demonstrated in Program 3.

Program 3. Printing multiple integers.

#include <stdio.h>

int main() {
	int n = 5;
	int m = 372;

	printf( "The result of %d + %d is %d.\n", n, m, n + m );

	return 0;
}

Executing this Program 3 would print the string The result of 5 + 372 is 377.

In C++, the compiler was able to determine which format should be used by the type of the argument. In C, you must explicitly give the type as part of the % conversion. These are listed in Table 1.

Table 2. printf conversions.

ConversionTypeComments
%dintsigned integers
%uunsigned intunsigned integers (0-232 − 1)
%ffloatdon't use float
%lfdoublelong float
%cchara single character
%schar *
char []
a string (array of characters)
%xintan integer as a hexadecimal number using 0123456789abcdef
%Xintan integer as a hexadecimal number using 0123456789ABCDEF

If you simply want to print a %, use %%. This is similar to using \\ if you want a \ to appear in a string.

Program 4 demonstrates integer, double, character, and string conversions.

Program 4. Printing of various conversions.

#include <stdio.h>

int main() {
	int n = 5;
	int m = 9;

	printf( "The result of %d + %d is %d\n", n, m, n + m );

	double x = 3.141592654;
	double y = 2.718281828;

	printf( "The result of %lf + %lf is %lf\n", x, y, x + y );

	char ch = 'x';
	char * str = "Hello world";

	printf( "First print a character: %c, and then a string: %s\n",
		ch, str );

	return 0;
}

A Comment on Formating

When you ran Program 4, you may have noticed that not all the digits of the floating-point number were printed. We can tell printf how many digits to print by adding additional additional information (call flags) between the % and lf. Program 5 demonstrates the behaviour of some of the available flags.

Program 5. More format specifications.

#include <stdio.h>

int main() {
	int n = 1024;

	printf( "Printing an int using %%d ->%d<-\n", n );
	printf( "Printing an int using %%3d ->%3d<-\n", n );
	printf( "Printing an int using %%5d ->%5d<-\n", n );
	printf( "Printing an int using %%10d ->%10d<-\n", n );

	double x = 3.141592654;

	printf( "Printing a double using %%10lf ->%10lf<-\n", x );
	printf( "Printing a double using %%.10lf ->%.10lf<-\n", x );
	printf( "Printing a double using %%7.3lf ->%7.3lf<-\n", x );

	return 0;
}

As you may note, I normally do not print the output of any of the Programs given in this tutorial, however, here I will make an exception. Program 5, when run, produces:

Printing an int using %d ->1024<-
Printing an int using %3d ->1024<-
Printing an int using %5d -> 1024<-
Printing an int using %10d ->      1024<-
Printing a double using %10lf ->  3.141593<-
Printing a double using %.10lf ->3.1415926540<-
Printing a double using %7.3lf ->  3.142<-

The effect of these flags is described here:

  • %nd where n is a positive integer indicates that room should be made for the display of at least n digits (right justified). If the number of digits in the integer is larger than n, the entire integer will still be printed.
  • %n.mlf where n and m are positive integers indicates that room should be made for at least n, and the m indicates the number of digits which should be printed after the decimal point.

A full list of all possible modifiers is easily found on the web, for example printf.

Comment for Geeks

How does printf deal with an unknown number of arguments? It doesn't. It assumes that you're use of the conversions (%d, etc.) is correct and that you have supplied sufficiently many arguments. If you fail to do so, C will fail to print your result correctly. If you supply too many parameters, then there is not going to be a problem (it will ignore other arguments), however, if you supply too few, printf is simply going to continue searching up the stack for more things to print. Suppose we ran Program 3 but forgot the third argument, as is shown in Program 6.

Program 6. Printing multiple integers.

#include <stdio.h>

int main() {
	int n = 5;
	int m = 9;

	printf( "The result of %d + %d is %d.\n", n, m );

	return 0;
}

Try it out. When I run this, it prints a -1 (i.e., it found that the next memory location contained FFFFFFFF).


Questions

1. Print the address of an integer using the %x conversion.

2. What happens when call printf with a "%s" but pass a char as an argument?

3. Create an array of integers which stores these 11 values:

      {523, 813, 235, 9013, 13, 145, 1324, 32, 9235, 13515, 2352353252}

which represent the number of cents in 11 separate bank accounts. Use an appropriate floating-point multiplication and the appropriate flags to output

        5.23
        8.13
        2.35
       90.13
         .13
        1.45
       13.24
         .32
       92.35
      135.15
 23523532.52

Contents Previous Topic Top Next Topic