User Input and Variable Scope in C Programming

cs 131 autumn 2022 lecture 7 user input parameters n.w
1 / 24
Embed
Share

Explore the concepts of user input handling using scanf function in C programming, along with the limitations of variables and implications of variable scope. Learn how to read multiple values with scanf, format string patterns, and understand variable visibility within different parts of a program. Enhance your understanding of C programming through examples and explanations provided in the slides.

  • C Programming
  • User Input
  • Variable Scope
  • scanf Function
  • Format String

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS& 131, Autumn 2022 Lecture 7: user input; parameters Thank you to Marty Stepp and Stuart Reges for parts of these slides 1

  2. scanf scanf: a built in function that reads input from the console. Reading a number using scanf: scanf(format, &variable); Example: int count; scanf("%d", &count); printf("There were %d cats\n", count); 2

  3. scanf example 2 int main() { printf("Please type two numbers: "); int num1; int num2; scanf("%d %d", &num1, &num2); int product = num1 * num2; printf("The product is %d", product); } Output (user input underlined): Please type two numbers: 8 6 The product is 48 scanf can read multiple values from one line. 3

  4. Format string Can contain any pattern Uses similar place holders for variables as printf %d - int %lf - double %s - string When a token is not the type you ask for, it doesn't match and doesn't read anything 4

  5. Limitations of variables Idea: store the user input size in a variable. Use the variable's value in the functions. Problem: A variable in one function can't be seen in others. void top_half() { for (int i = 1; i <= size; i++) { // ERROR: size not found ... } } void bottom_half() { for (int i = size; i >= 1; i--) { // ERROR: size not found ... } } int main() { int size; scanf("%d", &size); top_half(); print_bottom(); } 5

  6. Scope scope: The part of a program where a variable exists. From its declaration to the end of the {} braces A variable declared in a for loop exists only in that loop. A variable declared in a function exists only in that function. void example() { int x = 3; for (int i = 1; i <= 10; i++) { printf("%d\n", x); } // i no longer exists here } // x ceases to exist here i's scope x's scope 6

  7. Scope implications Variables without overlapping scope can have same name. for (int i = 1; i <= 100; i++) { printf("/"); } for (int i = 1; i <= 100; i++) { // OK printf("\\"); } int i = 5; // OK: outside of loop's scope A variable can't be declared twice or used out of its scope. for (int i = 1; i <= 100 * line; i++) { int i = 2; // ERROR: overlapping scope printf("/"); } i = 4; // ERROR: outside scope 7

  8. Parameterization parameter: A value passed to a function by its caller. Instead of line_of_4, line_of_3, write line to draw any length. When declaring the function, we will state that it requires a parameter for the number of equals. When calling the function, we will specify how many equals signs to draw. 4 #================# line main 3 line #============# 10

  9. Declaring a parameter Stating that a function requires a parameter in order to run void <name>(<type><name>) { <statement>(s); } Example: void say_password(int code) { printf("The password is: %d\n", code); } When say_password is called, the caller must specify the integer code to print. 11

  10. Passing a parameter Calling a function and specifying values for its parameters <name>(<expression>); Example: void main() { say_password(42); say_password(12345); } Output: The password is 42 The password is 12345 12

  11. Parameters and loops A parameter can guide the number of repetitions of a loop. void main() { chant(3); } void chant(int times) { for (int i = 1; i <= times; i++) { printf("Just a salad...\n"); } } Output: Just a salad... Just a salad... Just a salad... 13

  12. How parameters are passed When the function is called: The value is stored into the parameter variable. The function's code executes using that value. void main() { chant(3); chant(7); } 3 7 void chant(int times) { for (int i = 1; i <= times; i++) { printf("Just a salad...\n"); } } 14

  13. Common errors If a function accepts a parameter, it is illegal to call it without passing any value for that parameter. chant(); // ERROR: parameter value required The value passed to a function must be of the correct type. chant(3.7); // ERROR: must be of type int 15

  14. Complex figure that scales Finish modifying the mirror code to use parameters so it can be scaled successfully. A mirror of size 3: #============# | <><> | | <>....<> | |<>........<>| |<>........<>| | <>....<> | | <><> | #============# A mirror of size 4: #================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================# 16

  15. Loop tables and parameters Let's modify our loop table to use a user input size This can change the amount added in the loop expression spaces dots size line 4 1,2,3,4 6,4,2,0 0,4,8,12 3 1,2,3 4,2,0 0,4,8 #================# | <><> | | <><> | | <>....<> | | <>....<> | | <>........<> | |<>........<>| |<>............<>| |<>........<>| |<>............<>| | <>....<> | | <>........<> | | <><> | | <>....<> | #============# | <><> | #================# #============# 17

  16. Partial solution // Prints the expanding pattern of <> for the top half of the figure. void top_half(int size) { for (int line = 1; line <= size; line++) { printf("|"); for (int space = 1; space <= (line * -2 + (2*size)); space++) { printf(" "); } printf("<>"); for (int dot = 1; dot <= (line * 4 - 4); dot++) { printf("."); } printf("<>"); for (int space = 1; space <= (line * -2 + (2*size)); space++) { printf(" "); } printf("|\n"); } } 18

  17. Observations about scale factors The size can change the "intercept" in an expression. Usually the "slope" is unchanged. void example(int size) { for (int space = 1; space <= (line * -2 + (2 * SIZE)); space++) { printf(" "); } ... It doesn't replace every occurrence of the original value. for (int dot = 1; dot <= (line * 4 - 4); dot++) { printf("."); } 19

  18. Double border Alter the mirror code so that a double border can be drawn at the top and bottom #++++++++++++++++# #================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================# #++++++++++++++++# Does this new border row remind you of another part of our program? How can we keep this new code from being redundant? 20

  19. Multiple parameters A function can accept multiple parameters. (separate by , ) When calling it, you must pass values for each parameter. Declaration: void <name>(<type><name>, ...,<type><name>) { <statement>(s); } Call: <name>(<exp>, <exp>, ..., <exp>); 21

  20. Multiple parameters example void print_number(int number, int count) { for (int i = 1; i <= count; i++) { printf("%d", number); } printf("\n"); } void main() { print_number(4, 9); print_number(17, 6); print_number(8, 0); print_number(0, 8); } Output: 444444444 171717171717 00000000 22

  21. Strings string: A sequence of text characters. char <name> [] = "<text>"; Examples: char name[] = "Marla Singer"; char point[] = "(1, 2)"; Note: You will sometimes see strings declared as char* This will work too but is slightly different More later on 23

  22. Strings as parameters void say_hello(char name[]) { printf("Welcome, %s\n", name); } int main() { say_hello("Marty"); char teacher[] = "Bictolia"; say_hello(teacher); } Output: Welcome, Marty Welcome, Bictolia 24

  23. Value semantics value semantics: by default when variables (int, double) are passed as parameters, their values are copied. Modifying the parameter will not affect the variable passed in. void strange(int x) { x = x + 1; printf("1. x = %d\n", x); } int main() { int x = 23; strange(x); printf("2. x = %d\n", x); ... } Output: 1. x = 24 2. x = 23 25

  24. A "Parameter Mystery" problem void mystery(int x, int z, int y) { printf("%d and %d", z, (y - x)); } int main() { int x = 9; int y = 2; int z = 5; mystery(z, y, x); mystery(y, x, z); } 26

More Related Content