Advanced c Programming Topics
Advanced c Programming Topics
Decimal 128 64 32 16 8 4 2 1
As shown the binary value is 255. If you reserve the 8th bit for
the sign, and bits 7 to 1 were all “1”, then the largest decimal
value would be 127. Why is the largest negative -128?
int i = 425;
printf (“%i %o %x %u\n”, i, i, i, i); 425 651 1a9 425
float f = 12.978F;
printf (“%f %e %g\n”, f, f, f); 12.978000 1.297800e+01 12.978
Prof. Steven S. Saliterman Kochan, S.G. Programming in C, 3rd ed., Developer’s Library, Indianapolis, Indiana
(2005).
Character Examples
printf (“%[flags] [width] [.prec] [hlL]”, type);
Example: Displayed Result:
char c = ‘X”;
printf (“%c\n”, c); X
printf (“%3c%3c\n”, c, c); X X (field width of 3)
char s[ ] = “abcdefg”;
printf (“%s\n”, s); abcdefg (display the string)
printf (“%.5s\n”, s); abcde (display 5 characters)
printf (“%10s\n”, s); abcdefg (field width of 10, right
justified)
Prof. Steven S. Saliterman
Flags
printf (“%[flags] [width] [.prec] [hlL]”, type);
Flag Meaning
- Left justify value
+ Precede value with + or -
(space) Precede positive value with space
character ) Zero fill numbers
# Precede octal value with 0, hexadecimal
value with 0x; display decimal point for floats;
leave trailing zeros for g or G format
Prof. Steven S. Saliterman Kochan, S.G. Programming in C, 3rd ed., Developer’s Library, Indianapolis, Indiana
(2005).
Width and .Precision Modifiers
printf (“%[flags] [width] [.prec] [hlL]”, type);
Specifier Meaning
number Maximum size of field
* Take next argument to printf as size of field
.number Minimum number of digits to display for integers; number
of decimal places for e or f formats. maximum number of
significant digits to display for g; maximum number of
characters for s format.
.* Take next argument to printf as precision (and interpret as
indicated in the proceeding row)
Prof. Steven S. Saliterman Kochan, S.G. Programming in C, 3rd ed., Developer’s Library, Indianapolis, Indiana
(2005).
Type Modifiers
printf (“%[flags] [width] [.prec] [hlL]”, type);
Type Meaning
hh Display integer argument as a character
h* Display short integer
l* Display long integer
ll* Display long long integer
L Display long double
j* Display intmax_t or unimax_t value
t* Display ptrdiff_t value
z* Display size_t value
(* Can be placed in front of the n conversion character to indicate the corresponding pointer argument is of the specified type.)
Prof. Steven S. Saliterman Kochan, S.G. Programming in C, 3rd ed., Developer’s Library, Indianapolis, Indiana
(2005).
Conversion Characters
printf (“%[flags] [width] [.prec] [hlL]”, type);
Char Use to Display
i or d Integer
u Unsigned integer
o Octal number
x Hexadecimal integer; using a-f
X Hexadecimal integer; using A-F
f or F Floating point number, to six decimal places by default
e or E Floating point number in exponential format (e places lower and E upper case)
g Floating point number in f or e format
a or A Floating point number in hexadecimal format 0xd.dddp+/-d
c Single character
s Null-terminated string
p Pointer
n Doesn’t print – stores the number of characters written so far by this call
inside the int pointed to by the corresponding argument.
% Percent
Prof. Steven S. Saliterman Kochan, S.G. Programming in C, 3rd ed., Developer’s Library, Indianapolis, Indiana
(2005).
Scanf
1. Method for reading data into your program.
2. Like printf, it takes optional modifiers between the % and
the modifier.
3. Usually, when searching the input stream for a value to
read, it bypasses whitespace characters – blank space,
tabs, carriage return, new line and from feed.
4. A %c will read the next character no matter what it is, or
if it is a string within brackets.
5. When reading the value is terminated when the field
width has been reached or until an invalid character is
read.
Modifier Meaning
* Field is to be skipped and not assigned
size Maximum size of the input field
hh Value is to be stored in a signed or unsigned char
h Value is to be stored in a short int
l Value is to be stored in a long int, double or wchar_t
j, z, or t Value is to be stored in a size_t (%j), ptrdiff_t (%z), intmax_t, or
unimax_t (%t)
ll Value is to be stored in a long int
L Value is to be stored in a long double
type Conversion character
scanf (“%i %5c %*f %s”, &il, text, string); 144abcde 736.55 (wine & 144 stored il,
cheese)
abcde to character array text,
The next call to scanf picks up where the 735.55 is matched but not assigned,
last one left off…
“(wine” to string
cheese) to string3
Prof. Steven S. Saliterman Kochan, S.G. Programming in C, 3rd ed., Developer’s Library, Indianapolis, Indiana
(2005).
Special Functions for Files
1) fopen - opens the file and creates a pointer for
reading, writing or appending to the file;
2) getc and putc - reading and writing characters to
the file.
3) fclose – closes file.
4) feof - test for end of file.
5) fprintf and fscanf – reading or writing data from a
file.
6) fgets and fputs – reading and writing lines of data.
7) stdin, stout and stderr – defined in <stdio.h>