Input and Output Statement



Header Files:

  

Header files are those files which written at the top of the 

program. It contains many pre-written functions. It is written 

in the program starting with hash (#) symbol followed by

‘include' keyword and then the name of header file within angle 

brackets (<>).For e.g. #include<stdio.h>, #include<conio.h>, 
#include<math.h>, #include<string.h>, 
etc.

It is used for printing and taking as input.




Formatted Input Output


There are many types of input and output functions available in C language. Some of 

them are mentioned below: -


Printf & scanf function are inbelt library funtion in C which are available in C library
by the default. These function are declared and related micros are define in stdio.h which is header file.

1.Printf function
In C programming language, printf() function is used to print the (“character, string, 
float, integer, octal and hexadecimal values”) onto the output screen. We use printf() function with %d format specifier to display the value of an integer variable.
The control strings use some printf() format codes or format specifiers or conversion characters.
Format CodeMeaning
%cTo read a single character
%sTo read a string
%dTo read a signed decimal integer (short)
%ldTo read a signed long decimal integer
%fTo read a float (short0 or a single precision value
%lfTo read a double precision float value
%eTo read a float value exponential
%gTo read double float value
%oTo read an octal integer only
%xTo read a hexadecimal integer only
%uTo read unsigned decimal integer (used in pointer)
2. Scanf function
In C programming language, scanf() function is used to read character, string, numeric data from keyboard. Consider below example program where user enters a character. 
The scanf() format code (specifier) is as shown in the below table:
Format CodeMeaning
%cTo read a single character
%dTo read a signed decimal integer (short)
%ldTo read a signed long decimal integer
%eTo read a float value exponential
%fTo read a float (short0 or a single precision value
%lfTo read a double precision float value
%gTo read double float value
%hTo read short integer
%iTo read an integer (decimal, octal, hexadecimal)
%oTo read an octal integer only
%xTo read a hexadecimal integer only
%uTo read unsigned decimal integer (used in pointer)
%sTo read a string
%[..]To read a string of words from the defined range
%[^]To read string of words which are not from the defined range


Example to show the use of printf and scanf functions

#include<stdio.h>

#include<conio.h>

void main()

{

    int i;

    printf(“Enter the value of i");

    scanf(“%d”, i);

    printf(“The vallue of I is:",i);

}


Unformatted I/O functions


Getchar() and Putchar() Functions

This function is an Input function. It is used for reading a single character from the keyborad. It is 

buffered function. Buffered functions get the input from the keyboard and store it in the memory 

buffer temporally until you press the Enter key.


The putchar function is an output function. It is used to display a single character on the screen.                   


Example: -

#include<stdio.h>

#include<conio.h>

void main()

{

    int C;

    printf(“Enter a character");

    C=getchar();

    putchar(C);

    getch();

}



Gets() and Puts() Functions


gets() : Reads characters from the standard input and stores them as a string.


 puts() : prints characters from the standard output. Just like printf statement.


Example: -

#include<stdio.h>

#include <conio.h>

int main()

{

    char name[50];

    printf("Enter your name: ");

    gets(name);

    printf("Your name is: ");

    puts(name);

    getch();

}


* getch()


This is also an input function. This is used to read a single character from the keyboard like getchar() 

function. But getchar() function is a buffered is function, getchar() function is a non-buffered function. 

The character data read by this function is directly assign to a variable rather it goes to the memory buffer, 

the character data directly assign to a variable without the need to press the Enter key.

The general syntax is as:     v = getch();

Difference between gets() and puts() functions

The main difference between gets and puts in C Language is that gets is a function that 

reads a string from standard input while puts is a function that prints a string to the 

standard output.
























No comments:

Post a Comment

theengineerschoice01@gmail.com