C Input-Output functions - printf, scanf, putchar, getchar, gets and puts in GCC


­Input and output Functions
C language provides many in-built input-output library functions to read input and output. It is used to perform input output tasks.The standard input-output header file is stdio.h  that contains the printf() and scanf() function.

I/o Function
Description
scanf
It is used to take input through the input device.
getchar
It is used to accept a character from the console.
gets
It is used to scan a line of text from a standard input device.
printf
It is used for display output statement.
putchar
It is used to display a single character on the screen.
puts
It is used to display a string on a standard output device.


Printf() Function C Language
In C programming language, printf() function is used to print the integer,string,character, float or other value. It prints the given statement to the console.

Syntax:
            printf("format string",argument_list); 

Example 1
// C output
#include <stdio.h>    // Header File
int main(){
    printf("C Programming JavaTpoint"); 
    return 0;
}

Output
C Programming JavaTpoint

C scanf() Function C language
C  scanf() function is used as input statement. It is enclosed with double quotes. It reads the input data from the console.
Syntax:
            scanf("format string",argument_list); 
Example 1
// C Integer Input/Output
#include <stdio.h>    // Header File
int main(){
    int num;
    printf("Enter any Integer number:");
    scanf("%d",&num);
    printf("Your Integer number is :%d",num); 
    return 0;
}

Output
Enter any Integer number:567
Your Integer number is :567

Note: C language is case sensitive. For example printf() and scanf() are different from Printf() and Scanf() function. It function name must be in lowercase.



Following are the input-output format string in C Language.

Format string

Description

Example

%d
Scan or print an integer value.
45
%c
Scan or print  a single character.
‘C’
%s
Scan or print string character.
“Clanguage”
%f
Scan or print float number.
2.45
%.1f
Scan or print float number with 1 digit decimal.
10.3
%e
Scan or print a floating-point number in exponential (scientific notation).
2.000000e+000
%g
Scan or print a floating-point number in either fixed decimal or exponential format depending on the size of the number.
10.5
%lf
Scan or print double value.
  20.123456
%o
Scan or print octal value.
226
%x
Scan or print hexadecimal value.
96


Some other example of printf() and scanf() function
Example 1
//  C Integer Output
#include <stdio.h>    // Header File
int main(){
    int num=786;
    printf("Your Integer number is :%d",num);  //displays the content inside quotation
    return 0;
}

Output
Your Integer number is :786

Example 2
// C Floats Input/Output
#include <stdio.h>   
int main(){
    float num;   // declare float number
    printf("Enter any Float number:");
    scanf("%f",&num);
    printf("Your Float number is :%f",num);
    return 0;
}

Enter any Float number:23.56
Your Float number is :23.559999
Example 3
// C Character I/O
#include <stdio.h>   
int main(){
    char chr;   // declare character 
    printf("Enter a character:");
    scanf("%c",&chr);

    printf("Your Enterd Character  is :%c",chr);
    return 0;
}
Output
Enter a character:J
Your Entered Character number is :J
Example 4
// C ASCII Code
#include <stdio.h>   
int main(){
    char chr;   // declare float number
    printf("Enter a character:");
    scanf("%c",&chr);
    printf("Your Entered Character  is :%c \n",chr);
    printf("ASCII value of '%c' is: %d", chr, chr); 
    return 0;
}

Output
Enter a character:A
Your Entered Character  is :A
ASCII value of 'A' is: 65


Example 5
// I/O of Floats and Integers
#include<stdio.h>
int main(){
   char ch = 'a';
   char str[15] = "javaTpoint";
   float flt = 12.334;
   int no = 150;
   double dbl = 20.123456;
   printf("Your Character is: %c \n", ch);
   printf("Your String is: %s \n" , str);
   printf("Your Float value is: %.3f \n", flt);
   printf("Your Integer value is: %d\n" , no);
   printf("Your Double value is: %lf \n", dbl);
   printf("Your Octal value is %o: \n", no);
   printf("Your Hexadecimal value is: %x \n", no);
   return 0;
}
Output
Your Character is: a
Your String is: javaTpoint
Your Float value is: 12.334
Your Integer value is: 150
Your Double value is: 20.123456
Your Octal value is 226:
Your Hexadecimal value is: 96

============================================================================

Getchar() Function 

The getchar() function is used to accept a character from the console and display output immediately while typing  or press any key for proceeding.
Syntax:
                        getchar(void);

Function
Parameter
getchar()
It read a single character from the input device and return  unsigned char.

Example 1


#include<stdio.h>
int main(){
char a;
printf("Enter a character:");
a=getchar(); // take input a single character
printf("Given character is %c:",a);
return 0;
}


Output
Enter a character:C
Given character is C:


agyanadda

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

2 $type={blogger}:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete