3. Data input and output

C Library Functions

The C library functions are provided by the system and stored in the library. The C library function is also called an inbuilt function in C programming.

C Standard library functions or simply C Library functions are inbuilt functions in C programming.

The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program. For example,

If you want to use the printf() function, the header file <stdio.h> should be included.

LIST OF MOST USED HEADER FILES IN C PROGRAMMING LANGUAGE:

Header file
Description
stdio.hThis is standard input/output header file in which Input/Output
functions are declared
conio.hThis is console input/output header file
string.hAll string related functions are defined in this header file
stdlib.hThis header file contains general functions used in C programs
math.hAll maths related functions are defined in this header file

Unformatted Input/Output function


   

getchar() Function

The getchar() function reads character type data form the input. The getchar() function reads one character at a time till the user presses the enter key.  

Example:

#inclide<stdio.h>
#include<conio.h>
int main()
{
char a;
printf("Enter a character : ");
a= getchar();
printf("\nEntered character : %c",a);
return 0;
}

Output:- Enter a character  : r

              Entered character : r

    

putchar() Function

putchar() function prints only one character at a time.

Example

#include<stdio.h>
#include<conio.h>
int main()
{
char a='R';
putchar(a);
return 0;
}

Output:- R

getch() Function

The getch() function reads the alphanumeric character input from the user. But, that the entered character will not be displayed.
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("hii, press any alphanumeric character to exit ");
getch();
return 0;
}

Output:- hii,press any alphanumeric character to exit

getche() Function

getche() function reads the alphanumeric character from the user input. Here, character you entered will be echoed to the user until he/she presses any key.

Example:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Hii,press any alphanumeric character or symbol to exit");
getche();
return 0;
}

Output:
          Hii,press any alphanumeric character or symbol to exit
            a

putch() Function

The putch() function prints any alphanumeric character.

Example:

#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char a;
printf("Press any key to continue");
a = getch();
printf("\n input : ");
putch(a);
return 0;
}
   
Output: Press any key to continue
              input : r

String I/O Functions

gets() Function

The gets() function can read a full string even blank spaces presents in a string. But, the scanf() function leave a string after blank space space is detected. The gets() function is used to get any string from the user.

Example:

#include <stdio.h> 
#include <conio.h>
int main()
{
char a[25];
printf("Enter a string : ");
gets(a);
printf("\n%s ,New Delhi ",a);
return 0;
}

Output: Enter a string : IIT Dwarka
                IIT Dwarka ,New Delhi

puts() Function

The puts() function prints the charater array or string on the console. The puts() function is similar to printf() function, but we cannot print other than characters using puts() function.

Example:

#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char a[25];
printf("Enter your Name : ");
gets(a);
puts(a);
return 0;
}
 

Output: Enter your Name: itesm
             itesm

Formatted Input/Output function

Scanf()

In the C programming language, scanf is a function that reads formatted data from stdin (i.e, the standard input stream, which is usually the keyboard, unless redirected) and then writes the results into the arguments given.

The scanf function uses the same placeholders as printf:

  • int uses %d
  • float uses %f
  • char uses %c
  • strings uses %s
Example:

#include <stdio.h>
int main()
{
   char ch;
   char str[100];
   printf("Enter any character \n");
   scanf("%c", &ch);
   printf("Entered character is %c \n", ch);
   printf("Enter any string \n");
   scanf("%s", &str);
   printf("Entered string is %s \n", str);
}

OUTPUT : 
Enter any character
a
Entered character is a
Enter any string 
hii
Entered string is hii

printf() function

The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

syntax:-   printf("format string",argument_list);  

Example:

#include <stdio.h>
int main()
{
   char ch = 'A';
   char str[20] = "iitditnotes.blogspot.com";
   float flt = 10.234;
   int no = 15;
   double dbl = 2.123456;
   printf("Character is %c \n", ch);
   printf("String is %s \n" , str);
   printf("Float value is %f \n", flt);
   printf("Integer value is %d\n" , no);
   printf("Double value is %lf \n", dbl);
   return 0;
}

Output:

Character is A
String is iitditnotes.blogspot.com
Float value is 10.234000
Integer value is 15
Double value is 2.123456
        


Post a Comment

0 Comments