Input output functions in c language

Programming

Input output functions – A function that displays the result on the screen is known as the output function where get input from the user is known as input function.

What are the input output functions in the c programming language

It divides into two parts such as the formatted and unformatted.

Input output functions

Formatted input output functions: Formatted input and output referred to an input and output data that has been arranged in a particular format. it must required #include<stdio.h> header file.

The following are the formatted input output functions in the c programming language.

Input function

  • scanf()

Output function

  • printf()

scanf(): This is input function, It accept data from the user in various formats such as the integer, character, float, double, long double.

Syntax:

scanf(“control string”,argument1,argument2,argument_n);

As per the above syntax control string specified the field format in which the data is to be entered.  arguments specified the address or location where the data are stored. Control string and arguments are separated by comma (,). Control string must be written into the double quotations (“”). It may include % filed format, Bank spaces, such as the tab \, new line, space. The main purpose of scanf() function is reading the data.

Example:

int a;

float b;

char c;

scanf(“%d”,&a);

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

As per above example No. 1 can hold only integer variable data such as a. in example No. 2 it can hold mixed type of data such as integer, float and character. In that case data must be input in a sequence as per the control string alignment must be follow right to left. Scanf() function must be terminated using semicolon (;) and control string does not accept comma (,).

printf(): printf() function is a statement that produces an output of the screen it is a built-in function which stored in stdio.h header file. We can display the result using printf statement.

Syntax:

printf(“control string”,argument1,argument2,argument_n);

As per the above syntax control string may include format of data such as %d, %f, %c, %s, %h, %b, etc. Control string may also include escape sequences such as \n, \b, \t. Arguments consider as a name of the variable and separated using comma (,).

print() a function is generally used for:

to display the message

display decimal number using %d

display real number using %f

display a single character using %c

display whole string using %s

we can also display data in specified format as per the user requirement but it will read argument and control string from right to left and both are must be match

it also display mixed data

Example:

int a=10;

char b=’x’;

float c=20.80;

char str[100];

printf(“hello friend”); //display the message

printf(“%d”,a); //display decimal value

printf(“%c”,b); //display character

printf(“%f”,c); //display real number

printf(“%s”,str); //display welcome string

printf(“%d%c%f”,a,b,c); //display mixed data

Following are the unformatted input output functions in the c programming language.

Input functions

  • getchar()
  • getch()
  • getche()
  • gets()

Output functions

  • putchar()
  • putch()
  • puts()

Unformatted input: There are following functions are used to input data or read character or string from the user or keyboard. It must require stdio.h header file.

getchar(): This function is used to read a single character from the keyboard. It can read only a single character at a time.

Syntax:

variable name getchar();

Example:

char P;

P=getchar();

getch(): It takes one character from the keyboard but it will be invisible to us. In another word, we can say that it waits until process any key from the keyboard. It must require conio.h header file. It read only a single character at a time and this character does not appear on the screen.

getche(): It is a built-in function available in stdio.h header file. It can read only a single character at a time. it work the same as a getchar().

Syntax:

variable name getche();

Example:

char P;

P=getche();

gets(): It is an individual function which defines in stdio.h header file. It accepts the whole string at a time. Gets() function most required a single argument and it must be typed as a character.

Syntax:

gets(variable_name);

Example:

char P[100];

gets(P);

Unformatted output: There are following inbuilt functions that are used to write or display character or a string.

putchar(): This function is the same as the print a function but it can write only a single character at a time. It must required a single argument and the argument must be the type of character.

Syntax:

putchar(variable_name);

Example:

char P;

putchar(P);

putch(): This function work the same as a putchar() function, generally used to display a single character at a time. It must require stdio.h header file.

Syntax:

putch(variable_name);

Example:

char P;

putch(P);

As per above syntax putch() function must required single argument and it must be type of character.

puts(): This function works same as a printf() function. It must require stdio.h header file generally used to display information on the screen.

Syntax:

puts(variable_name);

Example:

char P[100]=”welcome”;

puts(P);