www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C Tutorial - Lesson 4: Input and Output

Formatted Output

by John Kopp

Support this site at no cost to you

Welcome to easyCPlusPlus.com's tutorial on C programming. This lesson covers basic input and output. Usually i/o, input and output, form an important part of any program. To do anything useful your program needs to be able to accept input data and report back your results. In C, the standard library provides routines for input and output. The standard library has functions for i/o that handle input, output, and character and string manipulation. In this lesson, all the input functions described read from standard input and all the output functions described write to standard output. Standard input is usually the keyboard. Standard output is usually the monitor.

Formatted Output
The standard library function printf is used for formatted output. It takes as arguments a format string and an optional list of variables or literals to output. The variables and literals are output according to the specifications in the format string. Here is the prototype for printf.

int printf(const char *format, arg1, arg2, arg3, ......);

A prototype is used to declare a function in C. It specifies the functions signature, which is the number and type of its arguments and also the return type of the function. So, printf returns an integer value. It will either be the number of characters output, or some negative number signifying that an error has occurred. The format is a character string that can contain ordinary characters, which are output unmodified and conversion specifications, which control how the additional arguments are converted to characters for output. The easiest way to understand this is by example.

#include <stdio.h>

int main()
{
    int luckyNumber = 5;
    float radius = 2;
    char myName[15] = "John";
    char initial = 'J';

    printf("Hello World\n");    /* The format string contains only ordinary
                    characters. Ordinary characters are output unmodified.
                    A character string in C is of type "char *". */

    printf("My lucky number is %d\n", luckyNumber);    /* The "%d" specifies
                   that an integer value will be output. */

    printf("My name is %s\n",myName);    /* The %s specifies that a character
                   string will be output. */

    printf("My first initial is %c\n",initial);    /* The %c specifies that a
                   character will be output. */

    printf("The area of a circle with radius %f is %f\n", radius, 3.14*radius*radius);
                   /* %f specifies that a float will be output. */

    printf("Hello %s or should I say %c\n",myName,initial);
                   /* Multiple arguments of different types may be output. */
    return(0);
}

Here are the more common conversion specifiers.

Specifier Argument Type
%d int
%f float or double
%e float or double, output in scientific notation.
%c character
%s character string (char *)


Previous Page       Next Page