Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - putchar

Node:putchar, Next:, Previous:getchar, Up:Single-character input and output



putchar

If you want to print a single character on standard output, you can use the putchar function. It takes a single integer parameter containing a character (the argument can be a single-quoted text character, as in the example below), and sends the character to stdout. If a write error occurs, putchar returns EOF; otherwise, it returns the integer it was passed. This can simply be disregarded, as in the example below.

Here is a short code example that makes use of putchar. It prints an X, a space, and then a line of ten exclamation marks (!!!!!!!!!!) on the screen, then outputs a newline so that the next shell prompt will not occur on the same line. Notice the use of the for loop; by this means, putchar can be used not just for one character, but multiple times.

#include <stdio.h>

int main()
{
  int i;

  putchar ('X');
  putchar (' ');
  for (i=1; i<=10; i++)
    {
      putchar ('!');
    }
  putchar ('\n');

  return 0;
}

 
 
  Published under free license. Design by Interspire