1 / 11

scanf

scanf. Reads in information from the keyboard Examples scanf(“%d”, &number); scanf(“%d%d”, &value1, &value2); WARNINGS! Don’t forget the & (address of) in front of each variable! Use the correct percent code for the type of the variable you are using!. /* Arup Guha

glenys
Download Presentation

scanf

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. scanf • Reads in information from the keyboard • Examples • scanf(“%d”, &number); • scanf(“%d%d”, &value1, &value2); • WARNINGS! • Don’t forget the & (address of) in front of each variable! • Use the correct percent code for the type of the variable you are using!

  2. /* Arup Guha My Second C Program, edited 9/2/03 Computes the number of feet user ran. */ #include <stdio.h> #define YARDS_IN_MILE 1760 #define FEET_IN_YARD 3 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", feet_in_mile*num_miles); // system(“PAUSE”); Necessary to see screen in DevC++ return 0; }

  3. 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

  4. Types of Errors • Compiler Error • Your program has incorrect syntax • Compiler outputs some message pertaining to the problem • 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 sometimes.

  5. 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

  6. 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.

  7. 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.

  8. 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;

  9. 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);

  10. 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.

  11. /* 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 3.14159 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; }

More Related