Input-Output Training (I / O) in C Programming


In this C programming tutorial, with the help of various examples, you will get acquainted with the scanf () function to get the input value from the user and the printf () function to display to the user at the output.

Output in C language

In C programming, the printf () function is one of the main output functions. This function sends the formatted output to the screen.

Example 1: Output in C

#include <stdio.h>

int main ()

{

// Displays the string in parentheses

printf (“C Programming”);

return 0;

}

Output

C Programming

How does this program work?

  • All valid C programs must contain the main () function. Code execution starts from this function.
  • printf () is a library function for sending formatted output to the screen. Here it prints the string inside the quotation marks (“C Programming”).
  • To use the printf () function, you must enter the stdio.h header file into the program using the <include <stdio.h # command.
  • The command; return 0 is inside the main () function “exit status” of the program. This is optional

Example 2: Integer output

#include <stdio.h>

int main ()

{

int testInteger = 5;

printf (“Number =% d”, testInteger);

return 0;

}

Output

Number = 5

D% formatting is used to print int types. Here d% inside the quotation marks is replaced by the value of the testInteger variable.

Example 3: float and double output

#include <stdio.h>

int main ()

{

float number1 = 13.5;

double number2 = 12.4;

printf (“number1 =% f \ n”, number1);

printf (“number2 =% lf”, number2);

return 0;

}

Output

number1 = 13.500000

number2 = 12.400000

The format f% is used for float printing and lf% is used for double values.

Example 4: Character printing

#include <stdio.h>

int main ()

{

char chr = ‘a’;

printf (“character =% c”, chr);

return 0;

}

Output

character = a

The c% format is used to print the char variable.

Input in C language

In C programming, the scanf () function is one of the most common functions for getting input from a user. This function reads the value of the formatted input from a standard input such as a keyboard.

Example 5: Input and output integers

#include <stdio.h>

int main ()

{

int testInteger;

printf (“Enter an integer:”);

scanf (“% d”, & testInteger);

printf (“Number =% d”, testInteger);

return 0;

}

Output

Enter an integer: 4

Number = 4

In this example, the% d formatting in the scanf () function is used to specify the type of int input from the user. When the user enters an integer, that value will be stored in the testInteger variable.

Note that testInteger & is used in the scanf () function. Because testInteger & gives the address of the testInteger variable and the value entered by the user is stored in this address.

Example 6: Float and Double input and output

#include <stdio.h>

int main ()

{

float num1;

double num2;

printf (“Enter a number:”);

scanf (“% f”, & num1);

printf (“Enter another number:”);

scanf (“% lf”, & num2);

printf (“num1 =% f \ n”, num1);

printf (“num2 =% lf”, num2);

return 0;

}

Output

Enter a number: 12,523

Enter another number: 10.2

num1 = 12.523000

num2 = 10.200000

The templates f% and lf% for float and double are used, respectively.

Example 7: Character input and output in C.

#include <stdio.h>

int main ()

{

char chr;

printf (“Enter a character:”);

scanf (“% c”, & chr);

printf (“You entered% c.”, chr);

return 0;

}

Output

Enter a character: g

You entered g.

In this program, when the user enters a character value, the character itself will not be saved, but an correct value or ASCII (ASCII) code will be saved.

The c% text format is also used to display the character. If you use d% to display the character, its ASCII value will be printed.

Example 8: Display the ASCII value

#include <stdio.h>

int main ()

{

char chr;

printf (“Enter a character:”);

scanf (“% c”, & chr);

// Character is displayed% c with formatting

printf (“You entered% c. \ n”, chr);

// ASCII value% d is displayed with formatting

printf (“ASCII value is% d. ”, Chr);

return 0;

}

Output

Enter a character: g

You entered g.

ASCII value is 103.

Multiple inputs / outputs

The following program shows how to receive multiple inputs from the user and display them in the output.

#include <stdio.h>

int main ()

{

int a;

float b;

printf (“Enter integer and then a float:”);

// Receive multiple input values ​​from the user

scanf (“% d% f”, & a, & b);

printf (“You entered% d and% f”, a, b);

return 0;

}

Output

Enter integer and then a float: -3

3.4

You entered -3 and 3.400000

Formats for input / output (I / O)

The following table lists common data types in C and their formatting:

Formattingdata type
% dint
% cchar
% ffloat
% lfdouble
% hdshort int
% uunsigned int
% lilong int
% llilong long int
% luunsigned long int
% lluunsigned long long int
% csigned char
% cunsigned char
% Lflong double

The difference between our store tutorials and free tutorials : First, the packs teach the latest version of the software with much more functionality. Secondly, the packs have been prepared by experts, in a completely project-oriented manner, by solving the challenges that you face in the course of practical and professional work, and by learning them, you will be fully prepared for the labor market! Free tutorials or paid tutorials Many differnet site have only been dubbed or equivalent by non-experts!

Author: Real Targeted Traffic

This site will be ranked well in Google site rankings. Of course, Real (organic) visits Buy Website Traffic - Get and drive real High Quality, Targeted Website Traffic. Use our  Web Traffic booster service and Start Increasing sites Traffic 

4 thoughts on “Input-Output Training (I / O) in C Programming”

Leave a comment

Design a site like this with WordPress.com
Get started