scanf • Reads in information from the keyboard • Examples • WARNINGS!

• Reads in information from the keyboard
• Examples
– scanf(“%d”, &number);
– scanf(“%d%d”, &value1, &value2);
– Don’t forget the & (address of) in front of each
– Use the correct percent code for the type of
the variable you are using!
/* Arup Guha
My Second C Program, edited
Computes the number of feet user ran. */
#include <stdio.h>
#define YARDS_IN_MILE 1760
#define FEET_IN_YARD
int main(void) {
int feet_in_mile, num_miles;
feet_in_mile = YARDS_IN_MILE*FEET_IN_YARD;
printf("How many miles did you run?\n");
scanf("%d", &num_miles);
printf("You ran %d feet.\n",
// system(“PAUSE”); Necessary to see screen in
return 0;
Programming Style
Include ample white space
Indent code in between matching {}
Always write a header comment
Comment each major block of code
Use meaningful variable names
Define constants when appropriate
Be consistent with your style
Types of Errors
• Compiler Error
– Your program has incorrect syntax
– Compiler outputs some message pertaining to the
• Run-Time Error
– Some statement in your program forces your
computer to crash on an individual execution
– Not all executions may create a run-time error
• Logic Error
– Program runs, but produces incorrect output
Common Errors
• Forgetting a matching double quote
• Forgetting a semicolon
• Using the incorrect character code in a
printf or scanf
• Forgetting & (address of) when reading
into a variable
• Not initializing variables
• Incorrect order of assignment statements
Rules for Variable Names
Comprised of letters, digits, underscores
Can NOT start with a digit
Usually, variable names are case sensitive
Can NOT be a keyword (such as double)
Although it’s not a rule, variable names
should reveal the purpose of the variable.
Why use constants?
• Symbolic name (eg. WATER_DENSITY) has
more meaning that the value (1).
• If you ever have to change its value, you can
change it in one place.
• The compiler does NOT allow you to change its
value DURING execution.
• Two Different Schools of Thought
– Some think constants should ONLY be used for things
that never change (PI, E, etc.)
– Others think constants can be used for values that
might change sometime, but WON’T change within
the execution of a program.
Increment/Decrement Operators
• variable++ or ++variable
– Equivalent to variable = variable+1;
– There’s a subtle difference between the two
forms. (We won’t go into it in this class.)
• variable-- or –variable
– Equivalent to variable = variable-1;
Other Shorthand Assignments
• General Form <var> <op> <expr>
– Short hand for <var> = <var> <op> <expr>
• Examples
– x += 10;
– money -= 20;
– value *= (rate+2);
– portion /= (students+staff);
Pesky Detail about =
• Can be used multiply
– x = y = z – 4; is valid
– Not good style.
– If z were 7 before this statement executed
• y would first get set to 3, and this operation would
return 3
• Then, x would ALSO get set to 3, the return value
of the original operation.
– Thus, the associativity is right to left.
/* Arup Guha 8/29/07
Circle Program – edited to read in the radius of a circle
and the cost for one square foot of it and print out the
total cost of the whole circle. */
#include <stdio.h>
#define PI
int main(void) {
double radius, area;
double cost, totalcost;
printf(“Enter the radius&cost per sqft of your circle.”);
scanf(“%lf%lf”, &radius, &cost);
area = PI*radius*radius;
totalcost = cost*area;
printf(“The total cost of the circle is $.2lf\n”, totalcost);
// system(“PAUSE”); Necessary to see screen in DevC++
return 0;