I l @ ve RuBoard |
int , possibly short or unsigned or unsigned short ; population is a whole number.
float ; it's unlikely the average will be an exact integer. (You could use double but don't really need the extra precision.)
char .
int , possibly unsigned .
One reason is that long may accommodate larger numbers than int on your system; another reason is that if you do need to handle larger values, you improve portability by using a type guaranteed to be at least 32 bits on all systems.
char constant (but stored as type int )
int constant
double constant
unsigned int constant, hexadecimal format
double constant
Line 1:
Should be #include <stdio.h> .
Line 2:
Should have a pair of parentheses containing void following main ; that is, main(void) .
Line 3:
Use { , not ( .
Line 4:
Should be a comma, not a semicolon, between g and h .
Line 5:
Fine.
Line 6:
(blank) Fine.
Line 7:
There should be at least one digit before the e . Either 1e21 or 1.0e21 is okay.
Line 8:
Fine.
Line 9:
Use } , not ) .
Missing lines:
First, rate is never assigned a value. Second, the variable h is never used. Also, the program never informs you of the results of its calculation. Neither of these errors will stop the program from running (although you might be given a warning about the unused variable), but they do detract from its already limited usefulness . Also, there should be a return statement at the end.
Here is one possible correct version:
#include <stdio.h> int main(void) { float g, h; float tax, rate; rate = 0.08; g = 1.0e21; tax = rate*g; h = g + tax; printf("You owe $%f plus $%f in taxes for a total of $%f.\n", g, ->tax, h); return 0; }
Constant | Type | Specifier |
---|---|---|
a. 12 | int | %d |
b. 0X3 | unsigned int | %X |
c. 'C' | char (really int ) | %c |
d. 2.34E07 | double | %e |
e. '\040' . | char (really int ) | %c |
f. 7.0 | double | %f |
g. 6L | long | %ld |
h. 6.0f | float | %f |
Constant | Type | Specifier |
---|---|---|
a. 012 | unsigned int | %o |
b. 2.9e05L | long double | %Le |
c. 's' | char (really int ) | %c |
d. 100000 | long | %ld |
e. '\n' | char (really int ) | %c |
f. 20.0f | float | %f |
g. 0x44 | unsigned int | %x |
printf("The odds against the %d were %ld to 1.\n", imate, shot); printf("A score of %f is not an %c grade.\n", log, grade);
ch = '\r'; ch = 13; ch = '5' ch = '\xd'
Line 0: It's better form to have #include <stdio.h> .
Line 1: Use /* and */ .
Line 3: int cows, legs;
Line 5: count?\n");
Line 6: %d , not %c
Line 6: &legs
Line 8: %d , not %f
Add a return statement.
Here's one correct version:
#include <stdio.h> int main(void) /* this program is perfect */ { int cows, legs; printf("How many cow legs did you count?\n"); scanf("%d", &legs); cows = legs / 4; printf("That implies there are %d cows.\n", cows); return 0; }
newline character
a slash character
a double quotation mark
a tab character
I l @ ve RuBoard |