See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Understanding how variable assignment works

Course Queries Syllabus Queries
Max. 2000 characters

Ekta Singhania


( 6 months ago )

When I started learning C programming a few years ago, my tutor taught me similar to most of the tutors around the world. She said me the very basic things like any int datatype is of 2 bytes memory. If the following is my code,

void main()
    int i,n;
    n = 0;
        n = n + i;
    printf("%d",n); //obviously it prints 3

she will explain like what I have written below. Here my teacher starts off her class yet again. . .

Listen students. Here we have two integer values. So let us draw two boxes each of 2 bytes memory.The first box be the memory space of i and the second box be that of n. At n = 0; 0 gets stored into the box n. As the control gets into the loop, 0 gets stored into the box i intially.

0 0

Now the condition is checked. i < 3. Condition gets true. So now the value of n changes from 0 to n+i i.e., 0+0=0. The first iteration then ends after the increment statement. Now i gets incremented to 1. So our picture becomes like this.

1 0

And now i < 3 again. Condtion gets true. n changes from 0 to n + i i.e., 0+1=1. Don't forget the increment my dears. Only then our iteration gets completed. i++ makes our i to 2 now. And the picture will now look like

2 1

She will go on similarly step by step and will complete when both the boxes get 3, 3 values.

So thats it kids. After we get our boxes like this

3 3

the value of n gets printed on the screen.


By that time, I was merely like a kid nodding its head when a teacher claims earth as a circular mass. I never asked her any questions. I felt logically she was perfect. But now I am plentiful of queries. If there is a statement like n = n + i;, won't there be a conflict as both the destination space and the operation space is the same n box? Will the operations be done with the help of any default temporary space for calculations? What will happen if I use a recursive code like the following snippet.

int factorial(int n)
    if(n < 2)
        return n;
        n = n * factorial(n - 1);
        return n;

How could it be possible to use a single n box as my tutor taught? Won't a new n box be created whenever factorial(n - 1) is called? If I am right, how could the computer know which n box value to be returned? Somebody help me please. I am pulling of hairs from ma head!

what's your interest