Science Fair Projects Ideas - Printf

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Printf

Several programming languages implement a printf function, to output a formatted string. It originated from the C programming language, where it has the following prototype:

 int printf (const char *template, ...)

The string constant template provides a description of the output, with placeholders marked by "%" escape characters, to specify both the location and the type of output that is to be performed.

The printf function returns the number of characters printed, or a negative value if there occurred an output error.

Perl also has a printf function. Common Lisp has a format function which acts under the same principles as printf, but uses different characters for output conversion. The GLib library contains g_printf, an implementation of printf. Some Unix systems have a printf program for use in shell scripts.

C provides a number of derivative functions to gain further leverage from the printf functionality.:

  • fprintf enables printf output to be written to a regular file.
  • sprintf prints to a string (char array) instead of standard output. As it stands, sprintf is unsafe, since it will print into a buffer and not check for overflows, one may need to check the length of what is printed, which may be impossible to ensure, for example, if the program is making use of incoming data without restrictions.
  • snprintf behaves like sprintf, but has a length argument, limiting the amount of characters snprintf will write (including the string terminator). snprintf returns -1 if this limit was reached.
  • vprintf, vfprintf, vsprintf, vsnprintf behave respectively to the functions not prefixed with a v and make use of variable argument lists.

printf format placeholders

Formatting is denoted by placeholders within the format string. For example, if a program wanted to print out a person's age, it could present the output by prefixing it with "Your age is ". To denote that we want the integer for the age to be shown immediately after that message, we may use the format string

"Your age is %d."

Some of the most commonly used placeholders follow:

  • `%d', `%i' : Print an integer as a signed decimal number. `%d' and `%i' are synonymous for output, but are different when used with scanf() function for input.
  • `%f' : Print a floating-point number in normal (fixed-point) notation.
  • `%g', `%G' : Print a floating-point number in either normal or exponential notation, whichever is more appropriate for its magnitude. `%g' uses lower-case letters and `%G' uses upper-case.
  • `%s' : Print a character string.
  • `%%' : Print a literal `%' character.

If the syntax of a conversion specification is invalid, behavior is undefined. If there are too few or extranous function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are also undefined. In a number of cases the undefined behavior has lead to "Format string attack" security vulnerabilities.

See also

10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice