Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

C Tutorial
Introduction to C Programming
Variables in C
Opertaors in C
Branching Statement in C
Looping Statement in C
C Storage Class
Functions in C
The C Preprocessor
Input/Output Functions in C
File I/O Functions in C
Pointers in C
Arrays in C
Dynamic Memory Allocation in C
Strings in C
Structures in C

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


Input/Output Functions in C
Previous Next


The Standard Input Output File

To perform,input and output to files or the terminal,UNIX supplies a standard package.To use them,this contains most of the functions which will be introduced in this section,along with definitions of the datatypes required.Your program must include these definitions by adding the line to use these facilities.

#include <stdio.h> near start of the program file. If you do not do this, the compiler may complain about undefined datatypes or functions.




Character Input / Output

If you will not press the return key then they'll not start reading any input, and they'll not print characters on the terminal until there is a whole line to be printed. This is the lowest level of output and input. It provides very precise control, but is usually too fiddly to be useful also. Most computers perform buffering of inputs and outputs. These include :

  1. getchar
  2. putchar




getchar

getchar always returns the next character of keyboard input as an int. The EOF (end of file) is returned,if there is an error . It is usual to compare this value against EOF before using it. So error conditions will not be handled correctly,if the return value is stored in a char, it will never be equal to EOF.

The following program is used to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control - d.

#include <stdio.h>

main()
{ int ch, i = 0;

while((ch = getchar()) != EOF)
i ++;
printf("%d\n", i);
}



putchar

putchar puts its character argument on the standard output (usually the screen).

The following example program converts any typed input into capital letters.It applies the function toupper from the character conversion library ctype.h to each character in turn to do this.

#include <ctype.h> /* For definition of toupper */
#include <stdio.h> /* For definition of getchar, putchar, EOF */

main()
{ int ch;
while((ch = getchar()) != EOF)
putchar(toupper(ch));
}
Formatted Input / Output
These includes following:
  1. printf
  2. scanf



printf

This offers more structured output than the putchar. Its arguments are, in order; a control string, which controls what get printed, followed by a list of values to be substituted for entries in the control string. The prototype for the printf() is:

int printf(const char *format, ...);



To print,printf takes in a formatting string and the actual variables . An example of printf is:
int x = 5;
char str[] = "abc";
char c = 'z';
float pi = 3.14;

printf("\t%d %s %f %s %c\n", x, str, pi, "WOW", c);

With the use of the formatting line,you can format the output.You can change how the particular variable is placed in output,by modifying the conversion specification, . For example

printf("%10.4d", x);

The . allows for the precision.To floats as well,this can be applied.The number 5 is on the tenth spacing as the number 10 puts 0005 over 10 spaces. You can also add - and + right after % to make the number explicitly output as +0005. Note that the value of x does not actually change. In other words,you will not get output using %-10.4d will not output -0005. %e is useful for outputting floats using the scientific notation. %le for doubles and %Le for the long doubles.




To grab things from input,scanf() is used. Beware though, scanf isn't greatest function that C has to offer. Some people brush off the scanf as a broken function that shouldn't be used often. The prototype for scanf is:

Last example will create a rectangle with rounded corner:

int scanf( const char *format, ...);
To read of data from the keyboard,scanf allows formatted . Like printf it has a control string,followed by the list of items to be read. However scanf wants to know the address of items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by the & sign. Character strings are an exception to this. Since a string is already a character pointer, we give the names of the string variables unmodified by a leading &. Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalent. Looks similar to printf, but doesn't completely behave like the printf does. Take the example:
scanf("%d", x);

For grabbing things from input. Beware though, scanf isn't the greatest function that C has to offer,scanf() is used. The following is the example which shows the use of scanf:

int x, args;

for ( ; ; ) {
printf("Enter an integer bub: ");
if (( args = scanf("%d", &x)) == 0) {
printf("Error: not an integer\n");
continue;
} else {
if (args == 1)
printf("Read in %d\n", x);
else
break;
}
}



Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords: c tutorial, c arrays, c atoi, c array, void c, c syntax, functions math, c time, c language, c examples, format c, return c, c library, array functions, c programs, ansi c, standard c, c msdn, c algorithm, c example, variable functions, c program, functions variables, unix c, variable c, c type,


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.