Skip to content

Commit

Permalink
Initial commit to c-primer-plus-book-6ed.
Browse files Browse the repository at this point in the history
  • Loading branch information
szaydel committed Jan 4, 2014
0 parents commit b7832c8
Show file tree
Hide file tree
Showing 254 changed files with 7,771 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Ch01/inform.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
int main(void)
{
printf("A .c is used to end a C program filename.\n");

return 0;
}
11 changes: 11 additions & 0 deletions Ch01/listing1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>
int main(void)
{
int dogs;

printf("How many dogs do you have?\n");
scanf("%d", &dogs);
printf("So you have %d dog(s)!\n", dogs);

return 0;
}
7 changes: 7 additions & 0 deletions Ch01/listing2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
int main(void)
{
printf("Concrete contains gravel and cement.\n");

return 0;
}
14 changes: 14 additions & 0 deletions Ch02/fathm_ft.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// fathm_ft.c -- converts 2 fathoms to feet

#include <stdio.h>
int main(void)
{
int feet, fathoms;

fathoms = 2;
feet = 6 * fathoms;
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
printf("Yes, I said %d feet!\n", 6 * fathoms);

return 0;
}
13 changes: 13 additions & 0 deletions Ch02/first.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// first.c
#include <stdio.h>
int main(void) /* a simple program */
{
int num; /* define a variable called num */
num = 1; /* assign a value to num */

printf("I am a simple "); /* use the printf() function */
printf("computer.\n");
printf("My favorite number is %d because it is first.\n",num);

return 0;
}
14 changes: 14 additions & 0 deletions Ch02/nogood.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* nogood.c -- a program with errors */
#include <stdio.h>
int main(void)
(
int n, int n2, int n3;

/* this program has several errors
n = 5;
n2 = n * n;
n3 = n2 * n2;
printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3)

return 0;
)
14 changes: 14 additions & 0 deletions Ch02/stillbad.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* stillbad.c -- a program with its syntax errors fixed */
#include <stdio.h>
int main(void)
{
int n, n2, n3;

/* this program has a semantic error */
n = 5;
n2 = n * n;
n3 = n2 * n2;
printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3);

return 0;
}
16 changes: 16 additions & 0 deletions Ch02/two_func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//* two_func.c -- a program using two functions in one file */
#include <stdio.h>
void butler(void); /* ANSI/ISO C function prototyping */
int main(void)
{
printf("I will summon the butler function.\n");
butler();
printf("Yes. Bring me some tea and writeable DVDs.\n");

return 0;
}

void butler(void) /* start of function definition */
{
printf("You rang, sir?\n");
}
16 changes: 16 additions & 0 deletions Ch03/altnames.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* altnames.c -- portable names for integer types */
#include <stdio.h>
#include <inttypes.h> // supports portable types
int main(void)
{
int32_t me32; // me32 a 32-bit signed variable

me32 = 45933945;
printf("First, assume int32_t is int: ");
printf("me32 = %d\n", me32);
printf("Next, let's not make any assumptions.\n");
printf("Instead, use a \"macro\" from inttypes.h: ");
printf("me32 = %" PRId32 "\n", me32);

return 0;
}
15 changes: 15 additions & 0 deletions Ch03/badcount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* badcount.c -- incorrect argument counts */
#include <stdio.h>
int main(void)
{
int n = 4;
int m = 5;
float f = 7.0f;
float g = 8.0f;

printf("%d\n", n, m); /* too many arguments */
printf("%d %d %d\n", n); /* too few arguments */
printf("%d %d\n", f, g); /* wrong kind of values */

return 0;
}
11 changes: 11 additions & 0 deletions Ch03/bases.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* bases.c--prints 100 in decimal, octal, and hex */
#include <stdio.h>
int main(void)
{
int x = 100;

printf("dec = %d; octal = %o; hex = %x\n", x, x, x);
printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x);

return 0;
}
12 changes: 12 additions & 0 deletions Ch03/charcode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* charcode.c-displays code number for a character */
#include <stdio.h>
int main(void)
{
char ch;

printf("Please enter a character.\n");
scanf("%c", &ch); /* user inputs character */
printf("The code for %c is %d.\n", ch, ch);

return 0;
}
15 changes: 15 additions & 0 deletions Ch03/escape.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* escape.c -- uses escape characters */
#include <stdio.h>
int main(void)
{
float salary;

printf("\aEnter your desired monthly salary:");/* 1 */
printf(" $_______\b\b\b\b\b\b\b"); /* 2 */
scanf("%f", &salary);
printf("\n\t$%.2f a month is $%.2f a year.", salary,
salary * 12.0); /* 3 */
printf("\rGee!\n"); /* 4 */

return 0;
}
12 changes: 12 additions & 0 deletions Ch03/floaterr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* floaterr.c--demonstrates round-off error */
#include <stdio.h>
int main(void)
{
float a,b;

b = 2.0e20 + 1.0;
a = b - 2.0e20;
printf("%f \n", a);

return 0;
}
22 changes: 22 additions & 0 deletions Ch03/platinum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* platinum.c -- your weight in platinum */
#include <stdio.h>
int main(void)
{
float weight; /* user weight */
float value; /* platinum equivalent */

printf("Are you worth your weight in platinum?\n");
printf("Let's check it out.\n");
printf("Please enter your weight in pounds: ");

/* get input from the user */
scanf("%f", &weight);
/* assume platinum is $1700 per ounce */
/* 14.5833 converts pounds avd. to ounces troy */
value = 1700.0 * weight * 14.5833;
printf("Your weight in platinum is worth $%.2f.\n", value);
printf("You are easily worth that! If platinum prices drop,\n");
printf("eat more to maintain your value.\n");

return 0;
}
14 changes: 14 additions & 0 deletions Ch03/print1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* print1.c-displays some properties of printf() */
#include <stdio.h>
int main(void)
{
int ten = 10;
int two = 2;

printf("Doing it right: ");
printf("%d minus %d is %d\n", ten, 2, ten - two );
printf("Doing it wrong: ");
printf("%d minus %d is %d\n", ten ); // forgot 2 arguments

return 0;
}
16 changes: 16 additions & 0 deletions Ch03/print2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* print2.c-more printf() properties */
#include <stdio.h>
int main(void)
{
unsigned int un = 3000000000; /* system with 32-bit int */
short end = 200; /* and 16-bit short */
long big = 65537;
long long verybig = 12345678908642;

printf("un = %u and not %d\n", un, un);
printf("end = %hd and %d\n", end, end);
printf("big = %ld and not %hd\n", big, big);
printf("verybig= %lld and not %ld\n", verybig, verybig);

return 0;
}
16 changes: 16 additions & 0 deletions Ch03/showf_pt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* showf_pt.c -- displays float value in two ways */
#include <stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;

printf("%f can be written %e\n", aboat, aboat);
// next line requires C99 or later compliance
printf("And it's %a in hexadecimal, powers of 2 notation\n", aboat);
printf("%f can be written %e\n", abet, abet);
printf("%Lf can be written %Le\n", dip, dip);

return 0;
}
12 changes: 12 additions & 0 deletions Ch03/toobig.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* toobig.c-exceeds maximum int size on our system */
#include <stdio.h>
int main(void)
{
int i = 2147483647;
unsigned int j = 4294967295;

printf("%d %d %d\n", i, i+1, i+2);
printf("%u %u %u\n", j, j+1, j+2);

return 0;
}
16 changes: 16 additions & 0 deletions Ch03/typesize.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//* typesize.c -- prints out type sizes */
#include <stdio.h>
int main(void)
{
/* c99 provides a %zd specifier for sizes */
printf("Type int has a size of %zd bytes.\n", sizeof(int));
printf("Type char has a size of %zd bytes.\n", sizeof(char));
printf("Type long has a size of %zd bytes.\n", sizeof(long));
printf("Type long long has a size of %zd bytes.\n",
sizeof(long long));
printf("Type double has a size of %zd bytes.\n",
sizeof(double));
printf("Type long double has a size of %zd bytes.\n",
sizeof(long double));
return 0;
}
17 changes: 17 additions & 0 deletions Ch04/defines.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// defines.c -- uses defined constants from limit.h and float.
#include <stdio.h>
#include <limits.h> // integer limits
#include <float.h> // floating-point limits
int main(void)
{
printf("Some number limits for this system:\n");
printf("Biggest int: %d\n", INT_MAX);
printf("Smallest long long: %lld\n", LLONG_MIN);
printf("One byte = %d bits on this system.\n", CHAR_BIT);
printf("Largest double: %e\n", DBL_MAX);
printf("Smallest normal float: %e\n", FLT_MIN);
printf("float precision = %d digits\n", FLT_DIG);
printf("float epsilon = %e\n", FLT_EPSILON);

return 0;
}
10 changes: 10 additions & 0 deletions Ch04/flags.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* flags.c -- illustrates some formatting flags */
#include <stdio.h>
int main(void)
{
printf("%x %X %#x\n", 31, 31, 31);
printf("**%d**% d**% d**\n", 42, 42, -42);
printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6, 6, 6);

return 0;
}
15 changes: 15 additions & 0 deletions Ch04/floatcnv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* floatcnv.c -- mismatched floating-point conversions */
#include <stdio.h>
int main(void)
{
float n1 = 3.0;
double n2 = 3.0;
long n3 = 2000000000;
long n4 = 1234567890;

printf("%.1e %.1e %.1e %.1e\n", n1, n2, n3, n4);
printf("%ld %ld\n", n3, n4);
printf("%ld %ld %ld %ld\n", n1, n2, n3, n4);

return 0;
}
18 changes: 18 additions & 0 deletions Ch04/floats.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// floats.c -- some floating-point combinations
#include <stdio.h>

int main(void)
{
const double RENT = 3852.99; // const-style constant

printf("*%f*\n", RENT);
printf("*%e*\n", RENT);
printf("*%4.2f*\n", RENT);
printf("*%3.1f*\n", RENT);
printf("*%10.3f*\n", RENT);
printf("*%10.3E*\n", RENT);
printf("*%+4.2f*\n", RENT);
printf("*%010.2f*\n", RENT);

return 0;
}
15 changes: 15 additions & 0 deletions Ch04/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// input.c -- when to use &
#include <stdio.h>
int main(void)
{
int age; // variable
float assets; // variable
char pet[30]; // string

printf("Enter your age, assets, and favorite pet.\n");
scanf("%d %f", &age, &assets); // use the & here
scanf("%s", pet); // no & for char array
printf("%d $%.2f %s\n", age, assets, pet);

return 0;
}
18 changes: 18 additions & 0 deletions Ch04/intconv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* intconv.c -- some mismatched integer conversions */
#include <stdio.h>
#define PAGES 336
#define WORDS 65618
int main(void)
{
short num = PAGES;
short mnum = -PAGES;

printf("num as short and unsigned short: %hd %hu\n", num,
num);
printf("-num as short and unsigned short: %hd %hu\n", mnum,
mnum);
printf("num as int and char: %d %c\n", num, num);
printf("WORDS as int, short, and char: %d %hd %c\n",
WORDS, WORDS, WORDS);
return 0;
}
Loading

0 comments on commit b7832c8

Please sign in to comment.