The Ultimate Guide to Input Output Functions in C
C Statements – Input Output Functions in C
- There are two types of C Statements.
1.Input Statements
2.Output Statements - A program‘s “Input” refers to the data provided, and its “Output” refers to the outcome.
- A function can be imagined as a set of statements aimed at performing a task.
getchar():
getchar() function takes the character and stores it into a variable.
ch=getchar();
Here getchar() function takes the character and stores it into a variable.
putchar():
Using putchar() function we can display a single character as output on the monitor.
Ex:putchar(ch);
Here putchar() function is taking a variable ‘ch’ and it sends the content of that variable to the monitor.
Example: Program to accept and display a single character
#include<stdio.h>
void main()
{
charch;
ch=getchar();
putchar(ch);
}
Here, main() function from where a c program execution starts.After main() function, we should write { which represents the beginning of the main() function and } hwhich represents ending of the function.
char ch;
Here, ch is a variable declared as char type.So, it is possible to store a single character in ch.
ch=getchar();
Here,getchar() function waits for input of a single characher. When we enter a character from the keyboard, it reads it and then stores it into the variable ch. Storage is done by the symbol ‘=’ which is called ‘assignment operator’.
putchar(ch);
Here,putchar() function is displaying the content of ch on the monitor.
gets():
This function is useful to accept a string from the keyboard and store it into a variable.
gets(str);
In this statement gets() function will accept a string from the keyboard and stores into the variable set.
puts():
This function displays a string on the monitor.
puts(str);
Here,the content of ‘str’ is displayed.
Example: Program to accept and display a name.
#include<stdio.h>
void main()
{
charstr(20);
puts("type your name");
gets(str);
puts(str);
}
In above program
char str[20];
It represents an array of characters.The name of the array is str. After the array name, we should weite [ and ] braces. So, str[] array can accommodate a maximum of 20 characters.
scanf():
This function is useful to accept any type of input. It may be a number, a character or a string.
General Format: scanf(“format string”,variable list);
Format string is a string that can represent any of the following format characters shown in table.
Format String | Meaning |
---|---|
%c | A Single character |
%s | A word |
%[^\n] | A group of words |
%i | A decimal integer |
%d | Double integer number. |
%f | A floating point number |
%lf | A long floating point number |
%c | Octal number |
%x or %X | Hexadecimal Number |
%e or %E | Number in specific form(1.2.e5 means 1.2X10 power 5) |
As shown below, a comma should separate the variables in scanf().
scanf("%x",&num);
Here, num1 and num2 are variables of integer types. “%d” represents the format string for accepting decimal integer numbers.
printf():
This function is useful to display a character, a string or any numeric value.
General Format:printf(“format string”,variable list);
Backslash codes are also called escape sequences.
Backslash | Code |
---|---|
\n | To throw the cursor into a new line |
\a | Beep sound |
\t | Horizontal tab space |
\r | Works like enter button in printing |
\\ | A single \. |
\’ | ‘ |
\” | “ |