Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Functions fgets and putchar in C programming

September 10, 2013 Posted by: GPA Views: 804 0 comments

Problem

Write simple example in C using functions fgets and putchar.

Solution

In this article, you will learn how to read from keyboard and to show information on the screen. First of all you should include header file <stdio.h> for using these functions.


Declaration:

char *fgets(char *str, int n, FILE *filestream); 

char *str - function fgets reads characters from the keyboard into this first argument str until a newline or the EOF indicator is apeared.

int n -it is the maximum number of characters to be read. It is the length of array str.

FILE *filestream - specifies the stream for reading characters in this example, we use the standard input keyboard.  

int putchar(int character); - shows the character stored in variable character and returns it as an integer. 

 

Example:

   


#include <stdio.h>
#include <stdlib.h>
//main function
int main(int argc, char *argv[])
{
   char sentence[100]; //create char array
   printf("globalproganswer.com\n");
   printf("Enter sentence: ");
   //use  function fgets to read line of sentence from keyboard
   fgets( sentence, 100, stdin );
   int i=0;
   printf("The input line is: ");
   while(sentence[i] != '\0' ){
      putchar(sentence[i]);//use  function putchar to print information from array sentence
     i++;
   }
   system("PAUSE");//delay
  return 0;
}

Output

Functions fgets and putchar in C

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe