Vision and dreams are the blueprints of soul and achievements.
-Mohammed Ahmed F

Introduction to Input and Output Statements

Input & Output Statements:

The getchar() & putchar() functions

The int getchar(void) function reads the next available character from the screen 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 from the screen.

The int putchar(int c) function puts the passed character on the screen and returns the same character.

This function puts only single character at a time

You can use this method in the loop in case you want to display more than one characters on the screen.

Check the following example:

#include < stdio.h >
int main( )
{
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}

When the above code is compiled and executed, it waits for you to input some text when you enter a text and press enter then program proceeds and reads only a single character and displays it as follows:

Enter a value : this is test

You entered: t

The gets() & puts() functions

The char *gets(char *s) function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF.

The int puts(const char *s) function writes the string s and a trailing newline to stdout.

#include < stdio.h >
int main( )
{
char str[100];
printf( "Enter a value :");
gets( str );
printf( "\nYou entered: ");
puts( str );
return 0;
}

When the above code is compiled and executed, it waits for you to input some text when you enter a text and press enter then program proceeds and reads the complete line till end and displays it as follows:

Enter a value : this is test

You entered: This is test

The scanf() and printf() functions

The int scanf(const char *format, ...) function reads input from the standard input stream stdin and scans that input according to format provided.

The int printf(const char *format, ...) function writes output to the standard output stream stdout and produces output according to a format provided.

The format can be a simple constant string, but you can specify %s, %d, %c, %f etc to print or read strings, integer, character or float respectively.

There are many other formatting options available which can be used based on requirements.

For a complete detail you can refer to a man page for these function.

For now let us proceed with a simple example which makes things clear:

#include < stdio.h >
int main( )
{
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s, %d ", str, i);
return 0;
}

When the above code is compiled and executed, it waits for you to input some text when you enter a text and press enter then program proceeds and reads the input and displays it as follows:

Enter a value : seven 7
You entered: seven 7

Share this

Related Posts

Dear User,

Thank you for your comment. Hope you like this blog. Kindly share us on Social Media so others can be updated.

-Chief Administrative Officer.

Note: only a member of this blog may post a comment.