Header File
Header File
char
Header File
• User defined
In many programming language, they have their own convention for getting the input
nd <filename.h>
from - a Header filename can be different as per the requirement for the specific
unctionality.
the user or sending the processed output to the user at the console.
Similarly in C language we have this header file - "stdio.h".
Lets split the keyword into a small individual, So that we can bring a meaning to it later.
The name of header file is "stdio. h", i.e
• Std – standard
• i/o-input/output
• Hence, Its clear that this header file in C program is used for
performing Basic or Standard Input/Output operations.
In other words, when we require Input/Output functionality in our
program it can be achieved by including this header file at the
beginning of the program
Built-In Functions
Built-in C functions in respect of "stdio. h" file
are as follows.
• Input means to provide the program with some data to be used in the
program and Output means to display data on screen or write the data to a
printer or a file.
The standard input-output header file, named stdio.h contains the definition
of the functions printf() and scanf(),which are used to display output on
screen and to take input from user respectively .
•Integer:
• Input: scanf("%d", &intVariable);
• Output:
• printf("%d", intVariable);
•Float:
• Input: scanf("%f", &floatVariable);
•Output:
• printf("%f", floatVariable);
•Character:
• Input: scanf("%c", &charVariable);
•Output:
• printf("%c", charVariable);
We can also limit the number of digits or characters that can be input
or output, by adding a number with the format string specifier,
like "%1d" or "%3s",
the first one means a single numeric digit and the second one means 3
characters, hence if you try to input 42,
while scanf() has "%1d", it will take only 4 as input. Same is the case for
output.
•Integer:
•Input: scanf("%d", &intVariable);
• Output: printf("%d", intVariable);
•Float:
•Input: scanf("%f", &floatVariable);
•Output: printf("%f", floatVariable);
•Character:
•Input: scanf("%c", &charVariable);
#include <stdio.h>
int main()
{
return 0;
}
getchar() & putchar()
The getchar() function reads a character from the terminal and returns it as an
integer.
This function reads only single character at a time. You can use this method in a
loop in case you want to read more than one character.
The putchar() function displays the character passed to it on the screen and
returns the same character.
This function too displays only a single character at a time.
In case you want to display more than one characters, use putchar() method in a
loop.
#include <stdio.h>
void main( )
{
int c;
printf("Enter a character");
/*
Take a character as input and
store it in variable c
*/
c = getchar();
/*
display the character stored
in variable c
*/
putchar(c);
}
gets() & puts() functions
The gets() function reads a line from stdin(standard input) into the buffer pointed to
by str pointer,
until either a terminating newline or EOF (end of file) occurs. The puts() function
writes the string str and a trailing newline to stdout.
str → This is the pointer to an array of chars where the C string is stored
#include<stdio.h>
void main()
{
/* character array of length 100 */
char str[100];
printf("Enter a string");
gets( str );
puts( str );
getch();
}
When you will compile the above code, it will ask you to enter a string. When you
will enter the string, it will display the value you have entered.
Difference between scanf() and gets()
The main difference between these two functions is that scanf() stops reading characters
when it encounters a space,
but gets() reads space as character too
scanf() gets()
when gets() is used to read input it stops reading input when it encounters
when scanf() is used to read string input it stops reading when it encounters newline or End Of File.
whitespace, newline or End Of File It does not stop reading the input on encountering whitespace as it considers
whitespace as a string.
It is used to read input of any datatype It is used only for string input.