Basic I/O Functions in C/C++

Basic I/O Functions in C/C++

Character I/O (getc, getchar, putc, putchar)

getchar() &putchar() functions

The getchar() function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. You can use this method in the loop in case you want to read more than one characters. The putchar() function prints the character passed to it on the screen and returns the same character. This function puts only single character at a time. In case you want to display more than one characters, use putchar() method in the loop.
#include <stdio.h>
#include <conio.h>
void main( )
{
int c;
printf("Enter a character");
c=getchar();
putchar(c);
getch();
}

When you will compile the above code,it will ask you to enter a value. When you will enter the value, it will display the value you have entered.

gets() & puts() functions

The gets() function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (end of file) occurs. The puts() function writes the string s and a trailing newline to stdout.
#include<stdio.h>
#include<conio.h>
void main()
{
charstr[100];
printf("Enter a string");
gets(str);
puts(str);
getch();
}

When you will compile the above code, it will ask you to enter a string. When you will enter the string, it will display the value you have entered.

getc(), putc() functions

getc(), putc() functions are file handling function in C programming language which is used to read a character from a file (getc) and display on standard output or write into a file (putc).
#include <stdio.h>
int main()
{
   char ch;
   FILE *fp;
   if (fp = fopen("test.c", "r"))
   {
     ch = getc(fp);
     while (ch != EOF)
     {
        putc(ch, stdout);
        ch = getc(fp);
     }
     fclose(fp);
     return 0;
   }
   return 1;
}

Formatted and Console I/O (printf(), scanf(), cin, cout)

C programming language provides many of the built-in functions to read given input and write data on screen, printer or in any file.

scanf() and printf() functions

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf("Enter a value");
scanf("%d",&i);
printf( "\nYou entered: %d",i);
getch();
}
When you will compile the above code, it will ask you to enter a value. When you will enter the value, it will display the value you have entered.

NOTE : printf() function returns the number of characters printed by it, and scanf() returns the number of characters read by it.
int i = printf("welcometorajkot");
In this program i will get 15 as value, because welcometorajkot has 15 characters.

The Standard Output Stream (cout)

The predefined object cout is an instance of ostream class. The cout object is said to be "connected to" the standard output device, which usually is the display screen. The cout is used in conjunction with the stream insertion operator, which is written as << which are two less than signs as shown in the following example.

#include <iostream>
int main()
{
   char str[] = "Hello C++";
   cout << "Value of str is : " << str << endl;
}

The Standard Input Stream (cin)

The predefined object cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin is used in conjunction with the stream extraction operator, which is written as >> which are two greater than signs as shown in the following example.

#include <iostream>
int main()
{
   char name[50];
   clrscr();
   cout << "Please enter your name: ";
   cin >> name;
   cout << "Your name is: " << name << endl;
}

 

Difference between scanf() and gets()

The main difference between these two functions is that scanf() stops reading characters when it encounters a space, but gets() reads space as character too.
If you enter name as Piyush Patel using scanf() it will only read and store Piyush and will leave the part after space. But gets() function will read it completely.


Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes