Browse Source

Use macros instead of magic values

master
T. Meissner 9 years ago
parent
commit
c101daf3fa
3 changed files with 13 additions and 8 deletions
  1. +3
    -3
      c_primer_plus/chapter03/04.c
  2. +6
    -4
      c_primer_plus/chapter03/05.c
  3. +4
    -1
      c_primer_plus/chapter03/06.c

+ 3
- 3
c_primer_plus/chapter03/04.c View File

@ -8,9 +8,9 @@ int main (void) {
printf("Enter a floating-point value: ");
scanf("%f", &a);
printf("fixed-point notation: %f\n", a);
printf("exponential notation: %e\n", a);
printf("p notation: %a\n", a);
printf("fixed-point notation: %.2f\n", a);
printf("exponential notation: %.2e\n", a);
printf("p notation: %.2a\n", a);
return 0;


+ 6
- 4
c_primer_plus/chapter03/05.c View File

@ -2,6 +2,9 @@
#include <limits.h>
#define SEC_PER_YEAR 3.156e7
int main (void) {
@ -10,13 +13,12 @@ int main (void) {
printf("Enter your age: ");
scanf("%u", &age);
if ((unsigned int)(UINT_MAX / 3.156e7) < age) {
if ((unsigned int)(UINT_MAX / SEC_PER_YEAR) < age) {
printf("Integer overflow\n");
return 1;
} else {
printf("You are %u seconds old\n", (unsigned int)(age * 3.156e7));
printf("You are %u seconds old\n", (unsigned int)(age * SEC_PER_YEAR));
return 0;
}
return 0;
}

+ 4
- 1
c_primer_plus/chapter03/06.c View File

@ -1,6 +1,9 @@
#include <stdio.h>
#define MOL_MASS 3.0e-23
#define WATER_QUART_GRAM 950.0
int main (void) {
@ -8,7 +11,7 @@ int main (void) {
printf("Enter a amount of water, in quarts: ");
scanf("%f", &quarts);
printf("Number of molecules: %e\n", quarts * 950.0 / 3.0e-23);
printf("Number of molecules: %.2e\n", quarts * WATER_QUART_GRAM / MOL_MASS);
return 0;


Loading…
Cancel
Save