- Basic Input Output in C - Characters
#include <stdio.h>
int main()
{
char c1, c2, c3;
printf("Enter any character : ");
c1 = getchar();
printf("\nThe entered character is: ");
putchar(c1);
// Second character
printf("\nEnter any character : ");
c2 = getchar();
printf("\nThe entered character is: ");
putchar(c2);
// Third character
printf("\nEnter any character : ");
c3 = getchar();
printf("\nThe entered character is: ");
putchar(c3);
return 0;
}
- Basic Input Output in C - Formatted IO
Unformatted IO supports functions those will work on characters or string data mainly. To input other kind of data we need formatted functions. Famous of these functions are scanf() and printf() for taking input and writing output respectively.
printf() and scanf() functions also use some escape sequences. These are special character constants used during printing output to screen. These are helpful in making the output more readable and more catchy. The following are the escape sequences defined: -
These will be used in printf() function in next sections.
Program:
int main()
{
int a=10;
char b='b';
char str[100]="Hello and Welcome to Learning CS.";
float f=10.4;
double d=123456.9876;
long int l=123456789;
long double ld=28761872.67352;
printf("int a=%d, char b=%c",a,b);
printf("\nstr=%s",str);
printf("\nfloat f=%f, double d=%lf",f,d);
printf("\nlong int l=%ld, long double ld=%Lf",l,ld);
return 0;
}
There are also format modifiers for printf() like minimum field width, number of decimal places, alignment etc. these will be placed between % and format code. These are as follows: -
Minimum width specifier: - If we add a number between % sign and format code, it works like the minimum width of the output. It will then pads the output with spaces to make it happen. Although if the value is bigger than this number then it will printed as it is. For example “%5d”, %6f”
The precision specifier: - it will follow the minimum width specifier (if exists). It consists of a period followed by an integer. so “%10.4f” will display a floating number at least 10 characters wide with 4 decimal places.
Justification: -By default the justification in printf() is right alignment. We can change it by placing a ‘-‘ (minus) sign after % sign in format specifier for example “%-10.4f”.
It is used to read the data from keyboard in the same way as the printf() is used for all built-in types. The prototype of scanf() is: -
int scanf(const char *control_string,…);
similar to printf() the control_string will contain format specifiers. Which are defined below to read different kind of data types: -
Code Meaning
%a Reads a floating -point value (C99 only).
%c Reads a single character.
%d Reads a decimal integer.
%i Reads an integer in either decimal, octal, or hexadecimal format.
%e Reads a floating -point number.
%f Reads a floating -point number.
%g Reads a floating -point number.
%o Reads an octal number.
%s Reads a string.
%x Reads a hexadecimal number.
%p Reads a pointer.
%n Receives an integer value equal to the number of characters read so far.
%u Reads an unsigned decimal integer.
%[ ] Scans for a set of characters.
%% Reads a percent sign.
scanf() function will take the address of the variable in which the value needs to be saved so that the vlue entered from keyboard will be placed at that location in memory. So the variable name is passed preceded by a & (address of operator) sign.
The following program will show the use of printf() and scanf() functions: -
Program:
int main( )
{
char str[100];
int i;
printf( "Enter an integer value: ");
scanf("%d", &i);
printf( "Enter a String: ");
scanf("%s", str); // as array name is the address already so no need of & operator
printf( "\nYou Entered: %s %d ", str, i);
return 0;
}