40 likes | 51 Views
This guide explains how to test and debug a program by removing syntax and link errors, checking for runtime, semantic, and logic errors, and reading input from the terminal using the scanf() function.
E N D
Testing a program • Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines • Run time errors: • Semantic errors (check function arguments) • Logic errors • manual trace • add debugging code to check variables use print statements • check expressions; order of precedence, operation rules
Reading input from terminalscanf() • read data typed at the keyboard • convert the data to its internal form • store it into an object How can this be done? • scanf() is a function • Functions cannot directly access a variable by its name defined in another function • If scanf() cannot directly access a variable in main(), it cannot assign a value to that variable
Address of variable Use address of variable to access object int x; scanf(“%d”,&x); Scanf command uses a format string similar to printf command. %d indicates variable is an integer. Scanf reads input, converts it to an internal form, and stores it into an integer object whose address is given by the next unmatched argument. Value read is stored into the object whose address is &x and the value is stored into x.
Additional scanf() comments • scanf() is buffered as the input data is not read until a full buffer has been entered, indicated by a return key (a newline). • scanf() is free form as it ignores blanks. The first data item found in the input will be converted and stored in the address provided. Likewise, scanf() will not return until it has converted the requested number of data items. • Additional data in the buffer is not affected by scanf()