using scanf() in C, using printf() in C, using the conversion characters in C, using the format specifiers in C
learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back

 

Lesson 4: Input, Output, format specify ers and escape sequences

This lesson will show you how to:

  • Input by using the scanf() function
  • Output by using the printf() function
  • Conversion characters - which make code more readable
  • Format specify ers - specify the data type

    Q. What's the difference between a lawyer and a vampire?
    A. A vampire only sucks blood at night.

This lesson covers basic input and output. Usually i/o, input and output, form the most 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 (stdio.h) 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 means input using the keyboard. Standard output is usually means output onto the monitor.

The printf() standard function
The standard library function printf is used for formatted output. It makes the user input a string and an optional list of variables or strings to output. The variables and strings are output according to the specifications in the printf() function. Here is the prototype for printf.

The easiest way to understand this is by example.

    #include <stdio.h>

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

        printf("Hello World\n");       
        printf("My lucky number is %d\n", luckyNumber);            
        printf("My name is %s\n",myName);        
        printf("My first initial is %c\n",initial);               
        printf("The area of a circle with radius %f is %f\n",                3.14*radius*radius);
                                    
        return 0;
    }

    Conversion specify ers

    The conversion characters tell the compiler what data type the variable or string will be.

    %d      int        (the d is short for decimal)
    %f       float or double
    %e      float or double, output in scientific notation.   (also known as standard form)
    %c      character
    %s      character string (char *)

    Formatted Input


    The standard library function scanf is used for formatted input. It asks the user to input a string or any other data type. Similar to its use in printf, the format string can contain strings and conversion specify ers I

    scanf returns an integer, either the number of values read in, or EOF if an end of file is reached. EOF is a special termination character, specified in stdio.h, which designates the end of a file. If no values are successfully read, scanf returns 0. To use scanf in a program, the file stdio.h must be included.

    #include <stdio.h>

    int main(void)
    {

        float radius;
        char yourName[15];
        int number;

        printf("Enter the radius of circle: ");
        scanf("%f",&radius);     
        printf("A circle of radius %f has                                /                    area of %f\n",radius,3.14*radius*radius);
        printf("Enter a number from 1 to 1000: ");

        scanf("%d",&number);
        printf("Your number is %d\n",number);

        printf("Enter your name: ");
        scanf("%s",&yourName);                
        printf("Hello %s\n",name);

        return 0;

    }

    With one exception scanf will skip over white space such as blanks, tabs and new lines in the input stream. The exception is when trying to read single characters with the conversion specify ers %c. In this case, white space is read in. So, it is more difficult to use scanf for single characters. An alternate technique, using getchar, will be described later in this lesson.

    getchar


    getchar reads a single character from standard input. An example is:

    int getchar();

    It requires the user to press enter after entering

    putchar


    putchar writes a single character to standard output. An example is:


    int putchar(int value);

    Here is a sample program using both both getchar and putchar

    #include <stdio.h>

    int main(void)
    {

        int c;

        while ((c = getchar()) != EOF)
        {
            putchar(c);
        }

        return 0;

    }

    gets


    gets reads a line of input into a character array. An example is:


    gets(name of string)

    puts


    puts writes a line of output to standard output. An example is:


    int puts(name of string);

    It terminates the line with a new line, '\n'. It will return EOF is an error occurred. It will return a positive number on success.

    Here is a long example of strings and their definitions. The example will discuss the use of strings and how they should be declared.

    #include <stdio.h>

    int main()
    {

            /* character arrays are sized properly for data */

        char name[60];
        char address[120];
        char city[60];
        char state[20];
        char zip[15];

    /* For responses that can include white space such as blanks
    use gets. For single word responses scanf is easier */

        printf("Enter your name: ");

        if ((pt = gets(name)) == NULL)
        {
            printf("Error on read\n");
            return(1);
        }

        printf("Enter your address: ");

        if ((pt = gets(address)) == NULL)
        {
            printf("Error on read\n");
            return(1);
        }

        printf("Enter your city: ");
        scanf("%s",&city);

        printf("Enter your state: ");
        scanf("%s",&state);

        printf("Enter your zip: ");
        scanf("%s",&zip);

            /* Output user information */
        printf("%s\n",name);
        printf("%s\n",address);
        printf("%s, %s %s\n",city,state,zip);

        return 0;

    }

    back to top        go to next lesson

 


 






Hosted by www.Geocities.ws

1