Author : James Rogers
Page : << Previous 6 Next >>
conversion specifications:
% A conversion specification is introduced with this required character.
flags followed by zero or more flags.
width followed by an optional width field.
precision followed by an optional precision field.
argument followed by an optional argument that differs by specific conversions.
conversion ending in a required conversion type.
Flags
These are used to change the default behavior of the conversion.
- left justify the field.
+ used a sign with a number conversion.
# use 0 in front of octal conversions, 0x in front of hex conversions and a decimal point with decimal conversions.
0 pad number conversions with leading zeros, ignore if a - is present.
space if a space follows the % then a space will be placed before the output, ignore if a - is present.
Field Width
The number of characters wide a field is. Spaces are used to pad the extra characters if a value is not as wide as the given width. If the value is larger than this number the field will expand to fit the number.
Precision
The minimum number of digits to appear for integer types or the number of digits to apear after a floating point number or the maximum number of characters to print from a string. This takes the form of a decimal point followed by an optional number. If no number is given the precision defaults to 0.
Argument
These will be discussed with each relevant conversion. They are h, l, or L.
Conversions
d int to signed decimal -9999 or 99
i int to signed decimal -9999 or 99
o unsigned int to unsigned octal 8 becomes 10
u unsigned int to decimal 1 becomes 1
x unsigned int to hexidecimal 13 becomes 1d
X unsigned int to hexidecimal 13 becomes 1D
h is used in front of the above 6 to convert from the short int and unsigned short int to whatever.
l is used in front of the above 6 to convert from the long int and unsigned long int to whatever.
f double to decimal -9999.99 or 99.9
e double to scientific notation -9.999e+99 or 9.9e-9
E double to scientific notation -9.999E+99 or 9.9E-9
g double to decimal (f) or scientific (e) if the # of digits are equal to or greater than the precision.
G double to decimal (f) or scientific (E) if the # of digits are equal to or greater then the precision.
L is used in front of the above 5 to convert from long double to whatever.
c int to unsigned char 65 becomes an A in standard ASCII
s pointer to string prints out the string
n pointer to an int into which the number of characters already written to stream will be placed.
h is used in front of the above 1 to specify a short int.
l is used in front of the above 1 to specify a long int.
% a %% will write out a single %. This is the way to print a % when needed.
A correction to Part Two:
Date: Wed, 12 Aug 1998 11:27:08 +0200
From: Lars Hesdorf <hesdorf@ibm.net>
To: jrogers@u.washington.edu
Subject: The Standard C Library for Linux, Part Two"
Hej James M. Rogers
You wrote somewhere in "The Standard C Library for Linux, Part Two"
"putchar writes a character to standard out. putchar(x) is the same as
fputc(x, STDIN)"
You probably meant "...fputc(x, STDOUT)".
Lars Hesdorf
HESDORF@IBM.NET
Yes I did, thanks a lot. One more problem with all of those examples, STDOUT and STDIN are suposed to be lower case; stdout and stdin. Sorry if this caused anyone any frustrations.
The last article was on <stdio.h> Input and Output. This article is on <ctype.h> character handling.
Character handling allows us to clasify characters as alpha, digit, hexdigit, whitespace, printable, lowercase, uppercase, punctuation and to map to and from the upper and lowercase alphabets. Most importantly <ctype.h> implements these functions in a non-system dependent way.
If you write your program assuming that every computer is an ASCII computer you will have trouble porting your program to non ASCII machines. If you write your character handling functions in terms of these functions your program will be much more portable to other platforms.
I am assuming a knowledge of c programming on the part of the reader. There is no guarantee of accuracy in any of this information nor suitability for any purpose.
The program example that I will do this month will go thru the entire 8bit ASCII range and tell us to which classes any one chacter belongs. The example is rogers_example04.c. The output the program generates will be an html document and the run from my system is rogers_example04.html .
This program can be used as a cgi-bin script and is a demonstration of the flexibility of the c language.
As always, if you see an error in my documentation please tell me and I will correct myself in a later document. See corrections at end of the document to review corrections to the previous articles.
Character Handling
#include <ctype.h>
int isalpha(int c);
int isalnum(int c);
int isdigit(int c);
int isxdigit(int c);
int iscntrl(int c);
int isspace(int c);
int ispunct(int c);
int isgraph(int c);
int isprint(int c);
int islower(int c);
int isupper(int c);
int tolower(int c);
int toupper(int c);
isalpha returns true if the character is in the range of A-Z or a-z.
isalnum returns true if the character is in the range of A-Z or a-z or 0-9.
isdigit returns true if the character is in the range of 0-9.
isxdigit returns true if the character is in the range of 0-9 or a-f or A-F.
iscntrl returns true if the character is in the set (FF, NL, CR, HT, VT, BEL or BS).
isspace returns true if the character is in the set (space, FF, NL, CR, HT or VT).
ispunct returns true if the character is a nonalnum, nonspace and noncntrl.
isgraph returns true if the character isalnum or ispunct.
isprint returns true if the character isspace or isgraph.
islower returns true if the character is in the range of a-z.
isupper returns true if the character is in the range of A-Z.
tolower if isupper return the lowercase character otherwise return the character.
toupper if islower return the uppercase character otherwise return the character.
The last article was on <ctype.h> character handling. This article is on <stdlib.h> which contains many small sections: integer math, sorting and searching, random numbers, string to number conversions, multibyte character conversions, memory allocation and environmental functions. Because this library contains so many small yet very important sections I want to discuss each of these groups in its own section. An example will be given in each section below because these functions are too diverse to have a single example for all of them.
I am assuming a knowledge of c programming on the part of the reader. There is no guarantee of accuracy in any of this information nor suitability for any purpose.
As always, if you see an error in my documentation please tell me and I will correct myself in a later document. See corrections at end of the document to
Page : << Previous 6 Next >>