(part 1) Han from China's C learning program
- From: Nomen Nescio <nobody@xxxxxxxxx>
- Date: Sat, 22 Nov 2008 05:50:10 +0100 (CET)
Plot:
The story of one man's struggle to relearn C from the start and fill in
any gaps in his understanding. Contrast this with his initial approach, which
consisted of diving right in and sludging through source code, asking dumb
questions on newsgroups, screwing around with code fragments, wanting to kick
the living *** out of some smartasses and pedants on comp.lang.c (and still
wanting to), and typing "man" a few thousand times. Since he's by nature a
procrastinating lazy ***, he hopes that a record of his progress posted to
comp.lang.c will force him to finish the course, lest he be thought stuck at a
certain point, which would open him up to ridicule from the annoying fuckwits
on comp.lang.c. Any beginners out there who want to follow along, hey, that's
awesome that I can do my part for humanity. Just remember me when you have
your high-paying job and your penthouse apartment and your parties with
expensive wine and strippers.
Textbook used:
C Programming: A Modern Approach (2nd Edition), K. N. King
Rules:
1. Allowed to use only the material covered up to the current point.
2. The method of positive reinforcement will be used. I will reward myself with
a chocolate bar upon completing each chapter, a pizza upon completing every
five chapters, and a threesome with a couple of escorts upon completing the
book.
CHAPTER 2
EXERCISES
1. No warning from gcc until the "-Wall" switch is used, at which point we get
the following message:
helloworld.c: In function `main':
helloworld.c:6: warning: control reaches end of non-void function
The code is legal in C99 (returning 0) but generates an undefined return value
in C89. One solution is to add "return 0;" after the printf().
2. (a) DIRECTIVES: #include <stdio.h>
STATEMENTS: printf("Parkinson's Law:\nWork expands so as to ");
printf("fill the time\n");
printf("available for its completion.\n");
return 0;
(b) Parkinson's Law:
Work expands so as to fill the time
available for its completion.[newline]
3.
/* Computes the dimensional weight of a 12" x 10" x 8" box */
#include <stdio.h>
int main(void)
{
int height = 8, length = 12, width = 10, volume;
volume = height * length * width;
printf("Dimensions: %dx%dx%d\n", length, width, height);
printf("Volume (cubic inches): %d\n", volume);
printf("Dimensional weight (pounds): %d\n",
(volume + 165) / 166);
return 0;
}
4.
#include <stdio.h>
int main(void)
{
int i, j, k;
float a, b, c;
printf("i=%d j=%d k=%d\n", i, j, k);
printf("a=%.2f b=%.2f c=%.2f\n", a, b, c);
return 0;
}
Output can vary.
5. LEGAL IDENTIFIERS: _100_bottles, one__hundred__bottles,
bottles_by_the_hundred
ILLEGAL IDENTIFIERS: 100_bottles
6. Using more than one adjacent underscore at the start of an identifier
crosses into the implementation's territory. Using more than one adjacenet
underscore elsewhere in an identifier is inconvenient to type and makes it hard
for the stereotypical drug-addled C programmer to keep track of the number of
underscores used. Some displays may merge the underscores into one unbroken
line.
7. KEYWORDS: for, while
NOT KEYWORDS: If, main, printf
8. |answer|=|(|3|*|q|-|p|*|p|)|/|3|;|
1 2 3 4 5 6 7 8 9 0 1 2 3 4
14 tokens.
9. answer = ( 3 * q - p * p ) / 3 ;
answer = (3*q - p*p) / 3;
10. In dweight.c, the only spacing that is essential is as follows:
* the internal spacing of the comment, sort of.
* the spacing that puts the #include directive on a line by itself.
* the spacing between int and main; between int and height; and
between return and 0.
* the internal spacing of the string literals, sort of.
PROGRAMMING PROJECTS
1.
#include <stdio.h>
int main(void)
{
printf(" *\n");
printf(" * \n");
printf(" * \n");
printf("* * \n");
printf(" * * \n");
printf(" * \n");
return 0;
}
Well, that was fun. If any of you dickheads want to reuse that code in your own
projects, be my guest.
2.
#include <stdio.h>
#define PI 3.14f
int main(void)
{
int radius = 10;
float volume;
volume = 4.0f/3.0f * PI * radius*radius*radius;
printf("volume: %.2f\n", volume);
return 0;
}
I may decide to make that a foursome.
3.
#include <stdio.h>
#define PI 3.14f
int main(void)
{
int radius;
float volume;
// King doesn't fflush() his non-'\n' printf()'s, so by rule
// 1, I'm not allowed to either. All the standard tells us
// is that stdout will be fully buffered if and only if
// stdout can be determined not to refer to an interactive
// device. Here we'll hope for one of the following
// possibilities:
// 1. stdout has no buffering.
// 2. stdout is line-buffered, and we're on a system
// that flushes output streams when input is requested
// (and if certain conditions must hold, we hold them).
// 3. stdout is line-buffered, and the implementor decided
// that a buffer of only a few bytes would save valuable
// memory if he needed to use his C library on his
// fucking calculator watch.
printf("Enter radius: ");
// fflush(stdout);
scanf("%d", &radius);
volume = 4.0f/3.0f * PI * radius*radius*radius;
printf("volume: %.2f\n", volume);
return 0;
}
4.
#include <stdio.h>
int main(void)
{
float amount;
printf("Enter an amount: ");
// fflush(stdout);
scanf("%f", &amount);
// I know, but we're still supposed to be using
// the 'f' suffix at this point.
printf("With tax added: $%.2f\n", amount * 1.05f);
return 0;
}
5.
#include <stdio.h>
int main(void)
{
int x;
printf("Please enter a value for x: ");
// fflush(stdout);
scanf("%d", &x);
printf("Thanks, dickhead. Here's your result: %d\n",
3*x*x*x*x*x + 2*x*x*x*x - 5*x*x*x - x*x + 7*x - 6);
return 0;
}
6.
#include <stdio.h>
int main(void)
{
int x;
printf("Please enter a value for x: ");
// fflush(stdout);
scanf("%d", &x);
printf("Thanks, dickhead. Here's your result: %d\n",
((((3*x + 2)*x - 5)*x - 1)*x + 7)*x - 6);
return 0;
}
7.
#include <stdio.h>
int main(void)
{
int amount;
printf("Enter a dollar amount: ");
// fflush(stdout);
scanf("%d", &amount);
printf("\n$20 bills: %d\n", amount / 20);
amount = amount - amount/20 * 20;
printf("$10 bills: %d\n", amount / 10);
amount = amount - amount/10 * 10;
printf(" $5 bills: %d\n", amount / 5);
amount = amount - amount/5 * 5;
printf(" $1 bills: %d\n", amount);
return 0;
}
8.
#include <stdio.h>
int main(void)
{
float loan, rate, payment;
printf("Enter amount of loan: ");
// fflush(stdout);
scanf("%f", &loan);
printf("Enter interest rate: ");
// fflush(stdout);
scanf("%f", &rate);
printf("Enter monthly payment: ");
// fflush(stdout);
scanf("%f", &payment);
// monthly rate
rate = rate/100.0f / 12.0f;
loan = loan-payment + loan*rate;
printf("\nBalance remaining after first payment: $%.2f\n", loan);
loan = loan-payment + loan*rate;
printf("Balance remaining after second payment: $%.2f\n", loan);
loan = loan-payment + loan*rate;
printf("Balance remaining after third payment: $%.2f\n", loan);
return 0;
}
Now I'm going to get my fucking chocolate bar. See you next time.
Yours,
Han from China
.
- Prev by Date: Re: setjmp/longjmp query.
- Next by Date: Re: c size 0 question
- Previous by thread: setjmp/longjmp query.
- Next by thread: using gbd to debug a program reading from stdin
- Index(es):