Hardware Reference
In-Depth Information
printf(“Today's temperature is %4.1d \n”, temp); /* display the string Today's temperature is followed by the value
of temp. Display one fractional digit and use at least four digits
for the value. */
Conversion
Character
Meaning
c
d
e
f
g
Data item is displayed as a single character.
Data item is displayed as a signed decimal number.
Data item is displayed as a floating-point value with an exponent.
Data item is displayed as a floating-point value without an exponent.
Data item is displayed as a floating-point value using either e-type or f-type conversion,
depending on value; trailing zeros, trailing decimal point will not be displayed.
Data item is displayed as a signed decimal integer.
Data item is displayed as an octal integer, without a leading zero.
Data item is displayed as a string.
Data item is displayed as an unsigned decimal integer.
Data item is displayed as a hexadecimal integer, without the leading 0x.
i
o
s
u
x
Table 5.3 Commonly used conversion characters for data output
5.6 Functions and Program Structure
Every C program consists of one or more functions. If a program consists of multiple func-
tions, their definitions cannot be embedded within another. The same function can be called
from several different places within a program. Generally, a function will process information
passed to it from the calling portion of the program and return a single value. Information is
passed to the function via special identifiers called arguments (also called parameters ) and
returned via the return statement. Some functions, however, accept information but do not
return anything (for example, the library function printf ).
The syntax of a function definition is
return_type function_name (declarations of arguments)
{
declarations and statements
}
The declaration of an argument in the function definition consists of two parts: the type and
the name of the variable. The return type of a function is void if it does not return any value to
the caller. An example of a function that converts a lowercase letter to an uppercase letter is
char lower2upper (char cx)
{
if (cx .5 'a' && cx ,5 'z') return (cx 2 ('a' 2 'A'));
else return cx;
}
A character is represented by its ASCII code. A letter is in lowercase if its ASCII code is
between 97 (0x61) and 122 (0x7A). To convert a letter from lowercase to uppercase, subtract its
ASCII code by the difference of the ASCII codes of letters a and A .
 
Search WWH ::




Custom Search