i have some really specific/deatiled questions of which I am really confused by.
here is some sample code:
#include <stdio.h>
int mystery(int a, int B)/>;
main ()
{
int x = 2, y = 5;
y = mystery(x*3, y-2) + 4; //function caller
printf("(%d, %d)\n", x+3, y+2);
}
//function definition
int mystery(int a, int B)/>
{
int c;
c = a + b;
return c + 2; //takes value of c adds 2 and returns it to mystery
}
I totally understand this code, but the questions are a little confusing (my responses are in italics and my doubts/questions are in courier new font.
please correct me if i'm wrong or add more detail, i'd really like to have a solid foundation in C before I proceed onto C++ and other languages.
1. During the execution of the program above, what variables exist in memory and under which scope just before the statement
" return c + 2 " executes, and what are their values?
the variables that exist in memory during execution are "x", "y", "a", "b", "c" (all of type integer). "x" and "y" exist under the main function's scope while a, b, and c exist under the scope of the function definition "mystery." The value of x is 2, y is 5. The value of a is 6, b is 3 and c is 9.
im not sure if this is correct. do variables a, b, c really exist or are they there just to generate a value to return to the function caller located within function main? is the value of y really 5? or is it 15 because y = mystery(x*3, y-2) + 4 ? when i comment out the " return c + 2 " the output is (5, 15)?
2. During the execution of the program above, what variables exist in memory and under which scope just before the statement
" printf(" (%d, %d)\n", x+3, y+2); " executes, and what are their values?
the variables that exist just before the printf statement are x and y and c, and they exist under the scope of the main function. their values are x = 2, y = 15
as I was saying above, do variables a, b, c really exist or are they there just to generate a value to return to the main function? and am I using the term "scope" correctly? I know that the identifier's scope is where the identifier can be reference in a program. like i can't reference variable x within the funtion definition mystery because its not within it's scope?
3. Exactly what will be displayed when this program executes?
(5, 17)
Any help you guys can give me will be much appreciated, thanks in advance everyone
This post has been edited by iqbalmmz: 17 March 2011 - 12:41 PM

New Topic/Question
Reply




MultiQuote










|