/* * programming_functions_mem.c * * Created on: Oct 14, 2024 * Author: johnsontimoj */ /////////////////////////////////// // // program to show some stack operations and variable scope // note: the formal parameter and actual parameter names are duplicated intentionally // // inputs: user // outputs: prints // ///////////////////////////////// #include /////////////////////////////// // // Function prototypes (declarations) // /////////////////////////////// void splash(void); int gt(int a, int b); // poor choice of variable names int read_int(void); // /////////////////////////////////// // // main - just another function // ///////////////////////////////////// int main(void){ setbuf(stdout, NULL); int a; int b; int c; splash(); while(1){ a = read_int(); b = read_int(); printf("variable a in main has value %i and is stored in memory at location %p\n", a, &a); printf("variable b in main has value %i and is stored in memory at location %p\n", b, &b); printf("variable c in main has value %i and is stored in memory at location %p\n", c, &c); printf("\n"); c = gt(a, b); printf("The greater is %i\n", c); printf("\n"); }// end while return 0; }// end main //////////////////////////////////////// // // Function DEFINITIONS // ////////////////////////////////////// /////////////////////////////////// // splash // // function to print a splash screen // // inputs: none // outputs: none // /////////////////////////////////// void splash(void){ printf("---------------------------------\n"); printf("-- This is prog_fns_mem\n"); printf("---------------------------------\n"); printf("\n"); return; }// end splash /////////////////////////////////// // gt // // function to provide the greater of 2 ints // // inputs: 2 ints // outputs: int - greater value // /////////////////////////////////// int gt(int a, int b){ // poor choice of variable names int res; static int cnt = 1; printf("variable a inside gt has value %i and is stored in memory at location %p\n", a, &a); printf("variable b inside gt has value %i and is stored in memory at location %p\n", b, &b); printf("variable res inside gt has value %i and is stored in memory at location %p\n", res, &res); printf("variable cnt inside gt has value %i and is stored in memory at location %p\n", cnt, &cnt); printf("\n"); if(a > b) res = a; else res = b; printf("variable res inside gt has value %i and is stored in memory at location %p\n", res, &res); printf("count is %i\n\n", cnt); if(cnt == 5) printf("\nwhoo hoo\n\n"); cnt++; return res; }// end gt /////////////////////////////////// // read_int // // function to ask for and return an int // // inputs: none // outputs: int value entered // /////////////////////////////////// int read_int(void){ int val; printf("please enter an int:"); scanf("%i", &val); return val; }// end read_int