0% found this document useful (0 votes)
25 views

Header File

Uploaded by

ajoantony2021
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Header File

Uploaded by

ajoantony2021
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Header,printf,scanf,getchar,put

char
Header File

• A header file in C is the one in which it contains function


declarations/ definitions, variables and macro
definitions to be shared between several source files and has a
filename with extension ".h".
• Built-In

• User defined

• Syntax to include any Header file, irrespective of its type,


• #include <filename.h>
ere #include is a preprocessing directive(which informs the C compiler to include those
pecific files for the program).

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.

1. Formatted Input / Output Functions

•printf(): To print the values onto the output


screen.
•scanf(): To read a character, string, numeric
data from keyboard.
•sprint(): Writes formatted output to string.
•sscanf(): Reads formatted input from a
string.
•fprintf(): To Write formatted data to a file.
•fscanf(): To Read formatted data from a file.
File Operation Functions

• fopen(): To Open a file.


• fclose(): Closes an opened file.
• remove(): To Delete a file.
• fflush(): To Flush a File Buffer.
Character Input / Output Functions

• getc(): It reads character from a file.


• gets(): It reads line of string from keyboard.
• getchar(): It reads character from keyboard.
• puts(): It writes line of string to output console.
• putchar(): It writes a character to console.
• putc(): Writes a character to a file.
Basic Input and Output in C
• C language has standard libraries that allow input and output in a program.
The stdio.h or standard input output library in C that has methods for
input and output.

• 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.

• C programming language provides many built-in functions to read any given


input and to display data on screen when there is a need to output the
result.
scanf() and printf() functions

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.

NOTE : printf() function returns the number of characters printed by it,


and scanf() returns the number of characters read by it.
Format String Meaning
%d Scan or print an integer as signed decimal number
%f Scan or print a floating point number
%c To scan or print a character
%s To scan or print a character string. The scanning ends
at whitespace.
The basic type in C includes types like int, float, char, etc. Inorder to input or
output the specific type,
the X in the above syntax is changed with the specific format specifier of that
type.
The Syntax for input and output for these are:

•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()
{

// Declare the variables


int num;
char ch;
float f;

// --- Integer ---

// Input the integer


printf("Enter the integer: ");
scanf("%d", &num);

// Output the integer


printf("\nEntered integer is: %d", num);

// --- Float ---

// Input the float


printf("\n\nEnter the float: ");
scanf("%f", &f);

// Output the float


printf("\nEntered float is: %f", f);

// --- Character ---

// Input the Character


printf("\n\nEnter the Character: ");
scanf("%c", &ch);

// Output the Character


printf("\nEntered integer is: %c", ch);

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.

You might also like