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

 

[wp_table id=11337/]

 

Suggested for you

gets() and puts() function in C

User defined function in C Language

Function in C Language with example

Arithmetic function in C Language

 

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago