Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Weird usage of printf/scanf? Why do they work?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Meske Moday, post: 3936140"] ======================================================= I'm posting the three assignments he completed. I'm honestly not concerned about copyright given that these are common first course programming assignments. Script started on Mon Sep 7 14:47:00 2009 darwin:~ kevin$ ls *.c prog-2-1.c prog-2-2.c prog-2-3.c darwin:~ kevin$ cat prog-2-1.c /* * Author: xxx * Date: 9/5/09 * Version: 1.0 * Class: CIT 131 / Fall 2009 * * Assignment: This Program accepts a user's integer (n) and * returns the integer cubed. */ #include <stdio.h> /* function main begins program execution */ int main(void) { int n; /* User's input integer */ int cubed; /* Variable which will store the user's variable, cubed */ printf("Enter an integer: "); /* prompt */ scanf("%d", &n); /* read an integer */ cubed = n * n * n; /* cubing the user's integer */ printf(n, "^3 = ", cubed, "\n"); /* print results */ return 0; /* indicate a successful program end */ } /* End function main */ darwin:~ kevin$ gcc -ansi -pedantic -Wall prog-2-1.c -o prog-2-1 darwin:~ kevin$ ./prog-2-1 Enter an integer: 5 5^3 = 125 darwin:~ kevin$ cat prog-2-2.c /* * Author: xxx * Date: 9/5/09 * Version: 1.0 * Class: CIT 131 / Fall 2009 * * Assignment: This program accepts a user's input radius, and finds the * area/circumference of that circle. */ #include <stdio.h> /* function main begins program execution */ int main(void) { int radius; /* user's input radius */ float pi = 3.14159; /* approximation of Pi */ printf("Enter the radius of a circle: "); /* prompt */ scanf("%d", &radius); /* getting the user's radius */ printf("The area A of a circle with radius ", radius, " = %f", pi * (radius * radius), "\n"); /* Printing the calculated area */ printf("The circumference C of a circle with radius ", radius, " = %f", 2 * pi * radius, "\n"); /* Printing the calculated circumference */ return 0; /* indicate a successful program end */ } /* End function main */ darwin:~ kevin$ gcc -ansi -pedantic -Wall prog-2-2.c -o prog-2-2 darwin:~ kevin$ ./prog-2-2 Enter the radius of a circle: 10 The area A of a circle with radius 10 = 314.158997 The circumference C of a circle with radius 10 = 62.831802 darwin:~ kevin$ cat prog-2-3.c /* * Author: xxx * Date: 9/5/09 * Version: 1.0 * Class: CIT 131 / Fall 2009 * * Assignment: This program will return ths sum, average, product, * smallest, and largest of 3 given integers. */ #include <stdio.h> /* function main begins program execution */ int main(void) { int input1; /* User's first input integer */ int input2; /* User's second input integer */ int input3; /* User's third input integer */ printf("Input three different integers: "); /* prompt */ scanf("%d", &input1, &input2, &input3); /* get user input */ printf("Sum is ", input1 + input2 + input3, "\n"); /* printing sum */ printf("Average is ", (input1 + input2 + input3) / 3, "\n"); /* printing average */ printf("Product is ", input1 * input2 * input3, "\n"); /* printing product */ /* If block to find the smallest number */ if (input1 < input2 && input1 < input3){ printf("Smallest is ", input1, "\n"); } /* end if */ if (input2 < input1 && input2 < input3){ printf("Smallest is ", input2, "\n"); } /* end if */ if (input3 < input2 && input3 < input1){ printf("Smallest is ", input3, "\n"); } /* end if */ /* If block to find the largest number */ if (input1 > input2 && input1 > input3){ printf("Largest is ", input1, "\n"); } /* end if */ if (input2 > input1 && input2 > input3){ printf("Largest is ", input2, "\n"); } /* end if */ if (input3 > input2 && input3 > input1){ printf("Largest is ", input3, "\n"); } /* end if */ return 0; /* indicate a successful program end */ } /* End function main */ darwin:~ kevin$ gcc -ansi -pedantic -Wall prog-2-3.c -o prog-2-3 darwin:~ kevin$ ./prog-2-3 Input three different integers: -2 5 12 Sum is 15 Average is 5 Product is -120 Smallest is -2 Largest is 12 darwin:~ kevin$ exit exit Script done on Mon Sep 7 14:48:53 2009 I haven't tried this on another compiler, or for that matter another machine. I'm "assuming" that the programs worked on the student's machine as well, otherwise he would not have submitted them! I'm uncertain what OS/compiler he's using (I don't require a specific set up - just ANSI C). Based on my rubric, I had no choice but to give him full credit and then turn around say "don't do that anymore! - but don't ask why... Kevin [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Weird usage of printf/scanf? Why do they work?
Top