kashveyron kashveyron - 1 year ago 210
C Question

What is the difference between getchar/putchar, gets/puts and fgets/fputs (in C)?

I am currently studying input and output in C and I have found out that there are about a billion different ways to get input, such as, getch, getchar, gets and fgets, and the same with output (putchar, puts, fputs, etc.).

All these different methods of I/O are confusing me quite a bit, so I came on here to ask what the basic differences between the aforementioned functions are.

I also wrote a bit of code using these different functions and I commented in how I think they work, based on what I've learnt, but I'm not sure if my understanding is correct. I've read up on them in other places as well, but the explanations are very complicated and don't seem coherent.

So could anyone please tell me if I am using them correctly and if not, how I should use them and what are the main differences between them?

Here is my code:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

void individualCharacters()
char theChar;

while ((theChar = getchar()) != '~') { // getchar() stores all characters in a line buffer as it is entered until newline is entered
putchar(theChar); // putchar() prints the characters in the line buffer and does not print a newline, line buffering depends on compiler

void withoutF()
char name[50];

printf("What is your name? ");

gets(name); // receives a string until newline is entered, newline is then replaced with string terminator, array limit should not be passed

puts("Hi"); // only prints one string at a time and adds the newline because gets() previously replaces the newline

void withF()
char name[50];

printf("What is your name? ");

fgets(name, 50, stdin); // does add a newline so the newline takes up one space in the array, it stores input until either newline is entered or array limit is reached

fputs("Hi ", stdout); // does not print a newline but prints the string input up to the array limit
fputs(name, stdout);

void main()





These are just some functions I wrote that get input and display output in different ways, but I'm having trouble understanding why there are so many different ways of doing so.

If I have made any mistakes using the I/O functions, feel free to tell me so I can make amendments.

Thank you

Answer Source
char * fgets ( char * str, int num, FILE * stream );
Get string from stream

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
A null character is automatically appended in str after the characters read to signal the end of the C string.


int fputs ( const char * str, FILE * stream );
Write string to stream

Writes the string pointed by str to the stream.
The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to the stream.


int getchar ( void );
Get character from stdin

Returns the next character from the standard input (stdin).
It is equivalent to getc with stdin as its argument.


int putchar ( int character );
Write character to stdout

Writes character to the current position in the standard output (stdout) and advances the internal file position indicator to the next position.
It is equivalent to putc(character,stdout).

gets: from standard input to memory 
puts: from memory to standard input 

Example : 

void main( ) 
char name[10]; 
printf("What is your first and last name?"); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download