Intoduction to Printf() & Scanf()

Input and Output

The following two functions are used for input and output purpose in C language.
1.      scanf()
2.      printf()

1.scanf():-It is the most commonly used standard input function in ‘C’ language. This function is used to take input value from the user and store it into associated variable. The Input value can be char, int, and float type. This function is stored in the standard header file stdio.h therefore we must include stdio.h header file in our C Program before using this function.

Syntax: -         scanf("Format Specifier",&Variable Name);
Ex: -                scanf (“%i”, &Num);


Syntax: -         scanf("Format Specifier list",&Variable list separate by coma);
Ex: -                scanf (“%i%i”, &Num1, &Num2);

WAP to input a integer number from the user.
#include<stdio.h>
#include<conio.h>
void main ()
{
      int Num;
clrscr();
      scanf(“%i”,&Num);
      getch ();
}


2.printf ():-It is the most commonly used standard output function in ‘C’ language. This function is used to display the message or the result of calculation on the console screen. This function is also called the `print-formatted'. This function is stored in the standard header file stdio.h therefore we must include stdio.h header file in our C program before using this function.

The simplest way to print out a string:
Syntax: -         printf ("Some String");
Ex: -                printf (“Welcome”);

But if we want to to print out the contents of variables then we use the following syntsx:
Syntax: -         printf ("Format Specifier", Variable Name);
Ex: -                printf (“%i”, Num);







WAP which display a welcome message on the screen.
#include<stdio.h>
#include<conio.h>
void main ()
{
      clrscr();
      printf("welcome to Spire Academy,kota");
      getch ();
}

Format Specifiers
Format Specifiers in C language tells us which type of data to store and which type of data to print. The format specifiers are use only in two places
1) When we taking Input
2) When we displaying Output

The following are the basic format specifiers in ‘C’ language:-
1.      %i or %d                                  :-             Integer Data Type
2.      %f                                            :-             Float Data Type
3.      %ld                                          :-             Double Data Type
4.      %c                                            :-             Character Data Type
5.      %s                                            :-             String data Type
6.      %o                                            :-             Octal Number
7.      %x                                            :-             Hexadecimal Number


Escape Sequence
‘C’ language provides some special codes which are used to format the result on the output screen. These codes are called escape sequence. These codes are used with the standard output function printf().The escapes sequence code consist of backslash (\) followed by a character. ’C’ language provides the following escape sequences:-
\b         :           backspace                                            \f          :           form feed FF (also clear screen)
\n         :           new line                                               \r          :           carriage return(cursor to start of line)
\t          :           horizontal tab HT                                \v         :           vertical tab (not all versions)
\"          :           double quotes                                      \'          :           single quote character
\\          :           backslash character \                           \a         :           Audio Bell


WAP which display a welcome message on the screen with the help of New line escape sequence character.
#include<stdio.h>
#include<conio.h>
void main ()
{
      clrscr();
      printf("Spire \n Academy \n kota");
      getch ();
}

WAP which display a welcome message on the screen with the help of Tab escape sequence character.
#include<stdio.h>
#include<conio.h>
void main ()
{
      clrscr();
      printf("Spire \t Academy \t kota");
      getch ();

}

Comments