Formated printf(), scanf() functions in C - C-Tutorial

Latest

Thursday, 10 November 2016

Formated printf(), scanf() functions in C

Formatted Input Statements


            scanf() function is known as formatted input statement.

scanf() Function:

            scanf() library function is used for reading data from the console input device – Keyboard.  The general format of a scanf() function is:

Syntax             :           scanf(“Control String”, &Variable1, ------ , &Variablen);
Where,
Ø  The control string consists of format of data being received.  It forms with the combination of % symbol followed by the conversion character of different data types.
Ø  scanf() function requires & operator called address operator to locate memory location of the identifier to store values.
Ø  scanf() library function information is available in stdio.h header file.

Format Specifications:

1)         To read a character, provide at least one white space character in the conversion specification.

            Example          :           scanf(“ %c”,&ch);

2)         Conversion character should preceded with * symbol.  It tells the compiler that the next input field is to be read but not stored.

            Example          :           int x;
                                                float y;
                                                scanf(“%d %*c%f”,&x,&y);

            The above statement accepts one integer value, one character value and one real value.  But it stores only integer and real values.

Formatted Output Statements

            printf() function is known as formatted output statement.

printf() Function:

            printf() library function is used for displaying information on console output device – Monitor.    The general format of  printf() function is:

Syntax             :           printf(“Control String”, Varible1, ------ ,Variablen);


Where,
Ø  Control String specifies type of the data to be displayed.  It forms with the combination of % symbol followed by the conversion character of different data types.  In addition to this, control string allows three types of formats as:

1)         Values that will be printed on the screen as they appear.

            Example          :           printf(“New Programs”);

            Output             :           New Programs


2)         Escape sequence characters are valid to print the output information in different ways.

            Example          :           printf(“New \n Programs”);

            Output             :           New

                                                Programs

No comments:

Post a Comment