Browse Source

Initial commit of C primer plus examples

master
T. Meissner 9 years ago
parent
commit
ce61e4b153
6 changed files with 112 additions and 0 deletions
  1. +25
    -0
      c_primer_plus/chapter03/01.c
  2. +21
    -0
      c_primer_plus/chapter03/02.c
  3. +12
    -0
      c_primer_plus/chapter03/03.c
  4. +17
    -0
      c_primer_plus/chapter03/04.c
  5. +22
    -0
      c_primer_plus/chapter03/05.c
  6. +15
    -0
      c_primer_plus/chapter03/06.c

+ 25
- 0
c_primer_plus/chapter03/01.c View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main (void) {
int a = INT_MAX;
float b = FLT_MAX;
// overflow
printf("Integer overflow: %d\n", a+1);
printf("Float overflow: %f\n", b + 1.0);
a = INT_MIN;
b = -FLT_MAX;
// underflow
printf("Integer underflow: %d\n", a-1);
printf("Float underflow: %f\n", b-1.0);
return 0;
}

+ 21
- 0
c_primer_plus/chapter03/02.c View File

@ -0,0 +1,21 @@
#include <stdio.h>
int main (void) {
unsigned int a;
printf("Give an ASCII code value: ");
scanf("%u", &a);
if (a > 127) {
printf("Outside of ASCII range\n");
return 1;
} else {
printf("The character is: %c\n", a);
}
return 0;
}

+ 12
- 0
c_primer_plus/chapter03/03.c View File

@ -0,0 +1,12 @@
#include <stdio.h>
int main (void) {
printf("Startled by the sudden sound, Sally shouted,\n");
printf("\"By the Great Pumpkin, what was that!\"\n");
return 0;
}

+ 17
- 0
c_primer_plus/chapter03/04.c View File

@ -0,0 +1,17 @@
#include <stdio.h>
int main (void) {
float a;
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);
return 0;
}

+ 22
- 0
c_primer_plus/chapter03/05.c View File

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

+ 15
- 0
c_primer_plus/chapter03/06.c View File

@ -0,0 +1,15 @@
#include <stdio.h>
int main (void) {
float quarts;
printf("Enter a amount of water, in quarts: ");
scanf("%f", &quarts);
printf("Number of molecules: %e\n", quarts * 950.0 / 3.0e-23);
return 0;
}

Loading…
Cancel
Save