C Input Output Functions

C Input Output Functions.

C Statements:

  • There are two types of C Statements.
    1.Input Statements
    2.Output Statements
  • The data given to a program is called ‘Input’ and the result given by the program is called ‘Output’.
  • A function can be imagined as a set of statements aimed to perform a task.
[su_table responsive=”yes” fixed=”yes”]
Input Functions Output Functions
getchar() putchar()
gets() puts()
scanf() printf()

 

getchar():

  • getchar() function takes the character and stores it into a variable.
ch=getchar();
  • Here getchar() function takes the character and stores it into a variable.

putchar():

  • Using putchar() function we can display a single character as the output on the monitor.
Ex:putchar(ch);
  • Here putchar() function is taking a variable ‘ch’ and it sends the content of that variable to the monitor.

Example: Program to accept and display a single character

#include<stdio.h>
void main()
{
    char  ch;
    ch=getchar();
    putchar(ch);
}
  • Here, the main() function from where a c program execution starts. After the main() function, we should write { which represents the beginning of the main() function and } which represents the ending of the function.
char ch;

Here, ch is a variable declared as char type. So, it is possible to store a single character in ch.

ch=getchar();
  • Here,getchar() function waits for the input of a single character. When we enter a character from the keyboard, it reads it and then stores it into the variable ch. Storage is done by the symbol ‘=’ which is called ‘assignment operator’.
putchar(ch);
  • Here, the putchar() function is displaying the content of ch on the monitor.

gets():

  • This function is useful to accept a string from the keyboard and store it into a variable.
gets(str);
  • In this statement, gets() function will accept a string from the keyboard and stores into the variable set.

puts():

  • This function displays a string on the monitor.
puts(str);
  • Here, the content of ‘str’ is displayed.

Example: Program to accept and display a name.

#include<stdio.h>
void main()
{
     charstr(20);
     puts("type your name");
     gets(str);
     puts(str);
}

In the above program

char str[20];
  • It represents an array of characters. The name of the array is str. After the array name, we should write [ and ] braces. So, str[] array can accommodate a maximum of 20 characters.

scanf():

  • This function is useful to accept any type of input. It may be a number, a character or a string.
General Format: scanf(“format string”,variable list);
  • The format string is a string that can represent any of the following format characters shown in the table.
[su_table responsive=”yes” fixed=”yes”]
Format String Meaning
%c A single character
%s A word
%[^\n] A group of words
%i A decimal integer
%d Double integer number.
%f A floating-point number
%lf A long floating-point number
%c Octal number
%x or %X Hexadecimal Number
%e or %E Number in specific form(1.2.e5 means 1.2X10 power 5)

[/su_table]

  • The variables in scanf() should be separated by a comma, as shown below.
scanf("%x",&num);

 

  • Here,num1 and num2 are variables of integer types. “%d” represents the format string for accepting decimal integer number.

printf():

  • This function is useful to display a character, or a string or any numeric value.
General Format:printf(“format string”,variable list);

 

  • Backslash codes are also called escape sequences.
[su_table responsive=”yes” fixed=”yes”]
Backslash Code
\n To throw the cursor into a new line
\a Beep sound
\t Horizontal tab space
\r Works like entering button in printing
\\ A single \.
\’
\”
[/su_table]
naveed08st
 

Click Here to Leave a Comment Below 0 comments

Leave a Reply: