C Primer Plus 5/e...void main(int) / this program is perfect / {cows, legs integer; printf(“How...
Transcript of C Primer Plus 5/e...void main(int) / this program is perfect / {cows, legs integer; printf(“How...
1.1 C
C
C
2
C Primer Plus 5/e
int main() // traditional rules{
int doors;int dogs;doors = 5;dogs = 3;// other statements
}
int main() // C99 rules{// some statements
int doors;doors = 5; // first use of doors
// more statementsint dogs;dogs = 3; // first use of dogs// other statements
}
33
2 C
n3 = n2 * n2;
47
2 C
void main(int) / this program is perfect /{cows, legs integer;printf(“How many cow legs did you count?\n);scanf(“%c”, legs);cows = legs / 4;printf(“That implies there are %f cows.\n”, cows)
}
Startled by the sudden sound, Sally shouted,“By the Great Pumpkin, what was that!”
3 C
101
What is the radius of your pizza?6.0Your basic pizza parameters are as follows:circumference = 37.70, area = 113.10
#define BEEP ‘\a’#define TEE ‘T’#define ESC ‘\033’#define OOPS “Now you have done it!”
/* the following is wrong */#define TOES = 20
digits = fingers + TOES;
digits = fingers + = 20;
const
const int MONTHS = 12; // MONTHS a symbolic constant for
114
C Primer Plus 5/e
scanf()
4.14 input.c
// 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;
}
133
4
shoe = 3.0;while (shoe < 18.5){
foot = SCALE*size + ADJUST;printf(“%10.1f %20.2f inches\n”, shoe, foot);++shoe;
}
shoe = 2.0;while (++shoe < 18.5){
foot = SCALE*shoe + ADJUST;printf(“%10.1f %20.2f inches\n”, shoe, foot);
}
5.4
5
169
shoe 3
true
while
5.12 bottles.c
printf(“Take one down and pass it around,\n”);printf(“%d bottles of spring water!\n\n”, count - 1);
}return 0;
}
100 bottles of spring water on the wall, 100 bottles of spring water!Take one down and pass it around,99 bottles of spring water!
99 bottles of spring water on the wall, 99 bottles of spring water!Take one down and pass it around,98 bottles of spring water!
1 bottles of spring water on the wall, 1 bottles of springwater!Take one down and pass it around,0 bottles of spring water!
5
173
5.16 running.c
// converts time to pure seconds time = S_PER_M * min + sec;
// converts kilometers to miles distm = M_PER_K * distk;
// miles per sec x sec per hour = mph rate = distm / time * S_PER_H;
// time/distance = time per mile mtime = (double) time / distm;mmin = (int) mtime / S_PER_M; // find whole minutes msec = (int) mtime % S_PER_M; // find remaining seconds printf(“You ran %1.2f km (%1.2f miles) in %d min, %d sec.\n”,
distk, distm, min, sec);printf(“That pace corresponds to running a mile in %d min, ”,
mmin);printf(“%d sec.\nYour average speed was %1.2f mph.\n”,msec,
rate);return 0;
}
This program converts your time for a metric raceto a time for running a mile and to your averagespeed in miles per hour.Please enter, in kilometers, the distance run.10.0Next enter the time in minutes and seconds.Begin by entering the minutes.36Now enter the seconds.
5
191
*****************************************************************1) Fairfield Arms 2) Hotel Olympic3) Chertworthy Plaza 4) The Stockton5) quit*****************************************************************3How many nights are needed? 1The total cost will be $80.00.
*****************************************************************Enter the number of the desired hotel:1) Fairfield Arms 2) Hotel Olympic3) Chertworthy Plaza 4) The Stockton5) quit*****************************************************************4How many nights are needed? 3The total cost will be $285.25.*****************************************************************Enter the number of the desired hotel:1) Fairfield Arms 2) Hotel Olympic3) Chertworthy Plaza 4) The Stockton5) quit*****************************************************************5
while ((status = scanf(“%d”, &code)) != 1 ||(code < 1 || code > 5))
9
395
c z 3
int main(void){double q, x, duff(); /* declare in calling function */int n;
...q = duff(x,n);
...}
double duff(u, k) /* declare in function definition */double u;int k;{
double tor;...
return tor; /* returns a double value */}
408
C Primer Plus 5/e
dates + 2 == &date[2] /* same address */*(dates + 2) == dates[2] /* same value */
*(dates + 2) /* value of the 3rd element of dates */*dates + 2 /* 2 added to the value of the 1st element */
10.9 day_mon3.c
/* day_mon3.c -- uses pointer notation */#include <stdio.h>#define MONTHS 12
int main(void){
int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};int index;
for (index = 0; index < MONTHS; index++)printf(“Month %2d has %d days.\n”, index +1,
*(days + index)); // same as days[index]return 0;
}
10
433
show_array(rates, 5); // valid show_array(locked, 4); // valid
mult_array(rates, 5, 1.2); // validmult_array(locked, 4, 1.2); // not allowed
double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};double * const pc = rates; // pc points to beginning of the arraypc = &rates[2]; // not allowed *pc = 92.99; // ok -- changes rates[0]
double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};const double * const pc = rates;pc = &rates[2]; // not allowed*pc = 92.99; // not allowed
450
C Primer Plus 5/e
int * p1;const int * p2;const int ** pp2;p1 = p2; // not valid -- assigning const to non-constp2 = p1; // valid -- assigning non-const to constpp2 = &p1; // not valid -- assigning non-const to const
p2 = p1; // valid -- assigning non-const to const
const int **pp2;int *p1;const int n = 13;pp2 = &p1; // not allowed, but suppose it were*pp2 = &n; // valid, both const, but sets p1 to point at n*p1 = 10; // valid, but changes const n
10
457
extern char permis = ‘Y’; /* error */
static int svil = 1; // static variable, internal linkageint main(void){
int traveler = 1; // external linkagestatic int stayhome = 1; // internal linkageint main(){
extern int traveler; // use global travelerextern int stayhome;..// use global stayhome
12
555
14.14 firms2.c
/* films2.c -- using a linked list of structures */#include <stdio.h>#include <stdlib.h> /* has the malloc prototype */#include <string.h> /* has the strcpy prototype */#define TSIZE 45 /* size of array to hold title */struct film {
char title[TSIZE];int rating;struct film * next; /* points to next struct in list */
};
int main(void){
struct film * head = NULL;struct film * prev, * current;char input[TSIZE];
puts(“Enter first movie title:”);while (gets(input) != NULL && input[0] != ‘\0’){
current = (struct film *) malloc(sizeof(struct film));if (head == NULL) /* first structure */
head = current;else /* subsequent structures */
prev->next = current;current->next = NULL;strcpy(current->title, input);puts(“Enter your rating <0-10>:”);scanf(“%d”, ¤t->rating);while(getchar() != ‘\n’)
continue;puts(“Enter next movie title (empty line to stop):”);prev = current;
}if (head == NULL)
printf(“No data entered. “);else
printf (“Here is the movie list:\n”);current = head;while (current != NULL){
printf(“Movie: %s Rating: %d\n”, current->title, current->rating);
current = current->next;}
14
665
union hold valA;valA.letter = ‘R’;union hold valB = valA; // initialize one union to anotherunion hold valC = {88}; // initialize digit member of unionunion hold valD = {.bigfl = 118.2}; // designated initializer
pu = &fit;x = pu->digit; // same as x = fit.digit
fit.digit = 23; // 23 is stored in fit; 4 bytes usedfit.bigfl = 2.0; // 23 cleared, 2.0 stored; 8 bytes usedfit.letter = ‘h’; // 2.0 cleared, h stored; 1 byte used
fit.letter = ‘A’;flnum = 3.02*fit.bigfl; // ERROR ERROR ERROR
14
671
typedef struct complex {float real;float imag;
} COMPLEX;
typedef struct {double x; double y;} rect;
rect r1 = {3.0, 6.0};rect r2;
struct {double x; double y;} r1= {3.0, 6.0};struct {double x; double y;} r2;r2 = r1;
typedef char (* FRPTC ()) [5];
680
C Primer Plus 5/e
val &= 0377;
val = val & 0377;
OR |
(10010011) | (00111101) // (10111111) //
val |= 0377;
val = val | 0377;
OR ^
696
C Primer Plus 5/e
#include <stdio.h>
#include “mystuff.h”
#include <stdio.h>
#include “hot.h”
#include “/usr/biff/p.h” /usr/biff
16 C C
729
printf”Where are the parentheses?”;.
A
759
“ ” “ ”
‘ ’
‘ ’
#define P(X) printf(“name: “#X”; value: %d; address: %p\n”,X, &X)
#define _SKIP_ /* */#ifndef _SKIP_
/* */#endif
#ifdef PR_DATEprintf(“Date = %s\n”, __DATE__);
#endif
enum days visit;
A
797