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 getchar and puts in C programming

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

Problem

Write simple program in C using functions getchar and puts.

Solution

In this section, you will learn how to read from keyboard and to print information on the screen using the functions getchar and puts. You should include header file <stdio.h> for this program.

Declaration:

int getchar(void); 

this function reads a character from the keyboard and returns the character as an integer. 

int puts(const char *str ); 

it takes a string str as an input argument and show the string followed by a newline character. 

 

Example:

   


#include <stdio.h>
#include <stdlib.h>
//main function
int main(int argc, char *argv[])
{
     char text[100]; //create char array  
     puts("globalproganswer.com");
     puts("Enter some text: ");
     char readCharacter;
     int i=0;
     //use getchar to read each character
     while (( readCharacter = getchar()) != '\n') {
        text[i]=readCharacter;
        i++;
     } // end while
     text[i] = '\0'; //terminate string
     //show input result
    puts("The input text is: ");
    puts(text);
     system("PAUSE");//delay
     return 0;
}

Output

Functions getchar and puts in C programming

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