This blog is under construction

Friday, 27 April 2012

getchar example in C

Header file:
    stdio.h

Synopsis:
     int getchar(void);

Description:
     Inputs a character.  Returns integer value(ascii) of the character read.


getchar function C example:


  #include<stdio.h>
  int main() {
        char ch;
        FILE *fp;
        fp = fopen("file.txt", "w");
        while ((ch = getchar()) != EOF)
                fputc(ch, fp);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
  getchar example program in C
  jp@jp-VirtualBox:~/cpgms/exp$ cat file.txt
  getchar example program in C



1 comment:

  1. I’m still new to programming, but this explanation of getchar() really helped me understand how input works in C. It seems simple, but it’s actually an important part of handling user input in programs. I was just reading this while taking a short break from 24 k bet, and it made the concept a bit clearer for me. Thanks for sharing!

    ReplyDelete