4. printf("Also, your first name has %d letters,n", letters);
printf("and we have %d bytes to store it in.n", size);
return 0;
}
5. /* With loop*/
#include <stdio.h>
#define ADJUST 7.64
#define SCALE 0.325
int main(void)
{
double shoe, foot;
printf("Shoe size (men's) foot lengthn");
shoe = 3.0;
while (shoe < 18.5) /* starting the while loop */
{ /* start of block */
foot = SCALE*shoe + ADJUST;
printf("%10.1f %15.2f inchesn", shoe, foot);
shoe = shoe + 1.0;
} /* end of block */
printf("If the shoe fits, wear it.n");
return 0;
}
6. Output
Shoe size (men's) foot length
3.0 8.62 inches
4.0 8.94 inches
... ...
17.0 13.16 inches
18.0 13.49 inches
If the shoe fits, wear it.
7. C’s Terminology: Data Objects, Lvalues,
Rvalues, and Operands
• Data object is a general term for a region of data storage that can be
used to hold values.
• C uses the term lvalue to mean a name or expression that identifies a
particular data object. The name of a variable, for instance, is an lvalue Not
all objects can have their values changed.
• C uses the term modifiable lvalue to identify objects whose value can
be changed.
• Rvalues can be constants, variables, or any other expression that yields a
value.
8. Unary and binary operators
• Unary operators appears before their operand and associate from
right to left.
• Binary operators is an operator that operates on two operands and
manipulates them to return a result.
9. /* wheat.c -- exponential growth example*/
#include <stdio.h>
#define SQUARES 64 /* squares on a checkerboard */
#define CROP 1E15 /* US wheat crop in grains */
int main(void)
{
double current, total;
int count = 1;
printf("square grains total ");
printf("fraction of n");
printf(" added grains ");
printf("US totaln");
10. total = current = 1.0; /* start with one grain */
printf("%4d %13.2e %12.2e %12.2en", count, current,
total, total/CROP);
while (count < SQUARES)
{
count = count + 1;
current = 2.0 * current; /* double grains on next square */
total = total + current; /* update total */
printf("%4d %13.2e %12.2e %12.2en", count, current, total, total/CROP);
}
printf("That's all.n");
return 0;
}
12. Operator Precedence
• In C Each operator is assigned a precedence level. As in ordinary arithmetic,
multiplication and division have a higher precedence than addition and
subtraction, so they are performed first.
• What if two operators have the same precedence? If they share an operand, they
are executed according to the order in which they occur in the statement. For
most operators, the order is from left to right. (The = operator was an exception
to this.) Therefore, in the statement.
- butter = 25.0 + 60.0 * n / SCALE;
60.0 * n The first * or / in the expression (assuming n is 6 so that 60.0
* n is 360.0)
360.0 / SCALE Then the second * or / in the expression
25.0 + 180 Finally (because SCALE is 2.0), the first + or - in the
expression, to yield 205.0
15. Precedence and the Order of Evaluation
The strlen() function gives the length of a string in characters. Note that you can not use
sizeof() to get the string length.
16. Modulus Operator: %
The modulus operator is used in integer arithmetic. It gives the remainder that results
when the integer to its left is divided by the integer to its right. For example, 13 % 5
(read as "13 modulo 5") has the value 3.
17. #include <stdio.h>
#define SEC_PER_MIN 60 // seconds in a minute
int main(void)
{
int sec, min, left;
printf("Convert seconds to minutes and seconds!n");
printf("Enter the number of seconds (<=0 to quit):n");
scanf("%d", &sec); // read number of seconds
while (sec > 0)
{
min = sec / SEC_PER_MIN; // truncated number of minutes
left = sec % SEC_PER_MIN; // number of seconds left over
printf("%d seconds is %d minutes, %d seconds.n", sec, min, left);
printf("Enter next value (<=0 to quit):n");
scanf("%d", &sec);
}
return 0;
}
18. Output
Convert seconds to minutes and seconds!
Enter the number of seconds (<=0 to quit):
154
154 seconds is 2 minutes, 34 seconds.
Enter next value (<=0 to quit):
567
567 seconds is 9 minutes, 27 seconds.
Enter next value (<=0 to quit):
0
19. Increment and Decrement Operators:
++ and --
The increment operator performs a simple task; it increments (increases)
the value of its operand by 1. This operator comes in two varieties. The
first variety has the ++ come before the affected variable; this is the prefix
mode. The second variety has the ++ after the affected variable; this is
the postfix mode. The two modes differ with regard to the precise time that
the incrementing takes place.
20. #include <stdio.h>
int main(void)
{
int ultra = 0, super = 0;
while (super < 5)
{
super++;
++ultra;
printf("super = %d, ultra = %d n", super, ultra);
}
return 0;
}
21. Output
super = 1, ultra = 1
super = 2, ultra = 2
super = 3, ultra = 3
super = 4, ultra = 4
super = 5, ultra = 5
22. #include <stdio.h>
#define MAX 100
int main(void)
{
int count = MAX + 1;
while (--count > 0)
{
printf("%d bottles of spring water on the wall, "
"%d bottles of spring water!n", count, count);
printf("Take one down and pass it around,n");
printf("%d bottles of spring water!nn", count - 1);
}
return 0;
}
23. Output
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 spring water!
Take one down and pass it around,
0 bottles of spring water!
24. Expressions
An expression consists of a combination of operators and operands. The simplest
expression is a lone operand, and you can build in complexity from there.
25. Statements
Statements are the primary building blocks of a program. A program is a
series of statements with some necessary punctuation. A statement is a
complete instruction to the computer. In C, statements are indicated by
a semicolon at the end.
26. Type Conversions
Statements and expressions should normally use variables and constants of
just one type. If, however, you mix types, C doesn't stop dead in its tracks the
way, say, Pascal does. Instead, it uses a set of rules to make type conversions
automatically.
• When appearing in an expression, char and short, both signed and unsigned,
are automatically converted to int or, if necessary, to unsigned int.
• In any operation involving two types, both values are converted to the higher
ranking of the two types.
• In an assignment statement, the final result of the calculations is converted
to the type of the variable being assigned a value.
• When passed as function arguments, char and short are converted to int, and
float is converted to double. This automatic promotion can be overridden by
function prototyping.
27. #include <stdio.h>
int main(void)
{
char ch;
int i;
float fl;
fl = i = ch = 'C'; /* line 9 */
printf("ch = %c, i = %d, fl = %2.2fn", ch, i, fl); /* line 10 */
ch = ch + 1; /* line 11 */
i = fl + 2 * ch; /* line 12 */
fl = 2.0 * ch + i; /* line 13 */
printf("ch = %c, i = %d, fl = %2.2fn", ch, i, fl); /* line 14 */
ch = 5212205.17; /* line 15 */
printf("Now ch = %cn", ch);
return 0;
}
28. Output
ch = C, i = 67, fl = 67.00
ch = D, i = 203, fl = 339.00
Now ch = -
29. • Lines 9 and 10— The character 'C' is stored as a 1-byte ASCII value in ch. The integer
variable i receives the integer conversion of 'C', which is 67 stored as 4 bytes. Finally,
fl receives the floating conversion of 67, which is 67.00.
• Lines 11 and 14— The character variable 'C' is converted to the integer 67, which is
then added to the 1. The resulting 4-byte integer 68 is truncated to 1 byte and stored in
ch. When printed using the %c specifier, 68 is interpreted as the ASCII code for 'D'.
• Lines 12 and 14— The value of ch is converted to a 4-byte integer (68) for the
multiplication by 2. The resulting integer (136) is converted to floating point in order to
be added to fl. The result (203.00f) is converted to int and stored in i.
• Lines 13 and 14— The value of ch ('D', or 68) is converted to floating point for
multiplication by 2.0. The value of i (203) is converted to floating point for the addition,
and the result (339.00) is stored in fl.
• Lines 15 and 16— Here the example tries a case of demotion, setting ch equal to a
rather large number. After truncation takes place, ch winds up with the ASCII code for
the hyphen character.
30. The Cast Operator
cast is a manual type conversion and it consists of preceding the quantity
with the name of the desired type in parentheses. The parentheses and
type name together constitute a cast operator. This is the general form of
a cast operator:
(type)
The actual type desired, such as long, is substituted for the word type.
31. mice = 1.6 + 1.7;
mice = (int) 1.6 + (int) 1.7;
The first example uses automatic conversion. First, 1.6 and 1.7 are
added to yield 3.3. This number is then converted through truncation to
the integer 3 to match the int variable. In the second example, 1.6 is
converted to an integer (1) before addition, as is 1.7, so that mice is
assigned the value 1+1, or 2.