-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex6.c
65 lines (53 loc) · 2.01 KB
/
ex6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
int main(int argc, char *argv[])
{
int distance = 100;
float power = 2.345f;
double super_power = 5678.4532;
char initial = 'L';
char first_name[] = "Noah";
char last_name[] = "Zucker";
printf("You are %d miles away.\n", distance);
printf("You have %f levels of power.\n", power);
printf("You have %f awesome super powers.\n", super_power);
printf("I have an initial %c.\n", initial);
printf("I have a first name %s.\n", first_name);
printf("I have a last name %s.\n", last_name);
printf("My whole name is %s %c. %s.\n",
first_name, initial, last_name);
/* **************************************************
* EXTRA CREDIT
* ************************************************** */
// This line causes a segmentation fault
/*
printf("My whole name is %s %c. %s.\n",
first_name, last_name, initial);
*/
// This line outputs the following warning:
// Warning: ex6.c:27: warning: format ‘%d’ expects type ‘int’,
// but argument 2 has type ‘double’
// printf("My super power number is %d.\n", power);
// Octal literals for the British Sterling and Japanese Yen symbols.
printf("Supported currencies: $ \243 \245\n");
// Print a number as an octal, hexidecimal and base 64.
int number = 3405691582;
printf("%1$d as octal = %1$#o\n", number);
printf("%1$d as hexadecimal = %1$#X\n", number);
// TODO: Base64, eh, seems like not possible with sprintf
// print float with a bunch of decimals
printf("%1$f formatted to 10 decimal places = %1$.10f\n", super_power);
// printf returns an int
char * fmtstr = "%d\n";
puts("Check it out, printf() returns an int:");
printf(fmtstr, printf(fmtstr, printf(fmtstr, 10)));
// print empty string
printf("Printing an empty string: %s\n", "");
char * mystr = "%' '10.4f\n";
printf("Attempting to format float with %s", mystr);
// Discusion here: http://bit.ly/IdNaYt (cboard.cprogramming.com)
printf(mystr, 1002.0f);
printf(mystr, -99.48f);
printf(mystr, 1.00f);
printf(mystr, -power);
return 0;
}