Basic

input output function in the C language

input output function in the C language

In this tutorial, we will discuss input output function in the C  language.

input output function in the C language

printf() scanf() function in C programming language

The C programming language provides various built-in functions(in-built library function ) for several purposes as a mathematical calculation, string manipulation and input-output operation and many more.

input-output function

input and output functions are very important and mostly used in the C programming language for input and output operation – to read any given data and to display information on the screen.

We must know about two input output functions as a lot of usage in C language for input and output task.

There are printf() and scanf() function

 

printf() function

In the C programming language, the printf() function is used to display the  character, string, integer, float data onto the output screen

The common pattern of  printf() function

printf("formated string",argument);

Example

printf("%d",a);

Usage of printf() function in C language

String output

Example

#include <stdio.h> //stdio.h header file contain definition of printf() function

int main()
{
    printf("code4coding.com is a coding site"); //display string content using printf() function
    return 0;
}

When the above code is executed, it produces the following results

code4coding.com is a coding site

 

Integer output

This is an example for integer output using printf() function in C language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age=34;
    printf("My age is: %d\n",age);
    getch();
    return 0;
}

When the above code is executed, it produces the following results

My age is: 34

float output

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float average=67.98;
    printf("My average is: %.2f\n",average);
    getch();
    return 0;
}

When the above code is executed, it produces the following results

My average is: 67.980003

 

Usage of scanf() function in C language

scanf() function

In the C programming language, the Scanf() function is used to read and store the character, string, numeric data as a standard input from the keyboard

Syntax

scanf("formated string",&argument);

Example

scanf("%d",a)

string input output

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char web[20];
    printf("Enter website name: \n");
    scanf("%s",web);//use scanf function for input
    printf("Your website name is: %s ",web);//use printf function for output
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter website name:
codeforcoding.com
your website name is: codeforcoding.com

 

integer input output

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    printf("Enter your age: ");
    scanf("%d",&age);//use scanf() function for input
    printf("My age is: %d\n",age);//use printf() function for output
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter your age: 23
My age is: 23

 

float input output

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float average;
    printf("Enter your average marks: ");
    scanf("%f",&average);//use scanf() function for input
    printf("My average is: %.2f\n",average);//use printf() function for output
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter your average marks: 76.5
My average is: 76.50

Character input output

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch;
    printf("Enter a character: ");
    scanf("%c",&ch);//use scanf() function for input

    printf("your entered %c\n",ch);//use printf() function for output
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter a character: A
you entered A

 

 

Suggested for you

gets() and puts() function in C

 

C program gets() and puts() function
Write a Java program to print an integer
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago