+ 1
what "temp" is for ?
I'm beginner , can you tell me the uses of this variable :)
11 Answers
+ 3
Flandre Scarlet, I think it's meaningful enough:
There are convenient conventions for names, like i or j for loops, maybe x or y for generic maths functions, tmp or temp being one of these.
So I think it's helpful to know about these 'habits' as well.
+ 9
Mahad Gady you can think "temp" or "tmp" is a temporary storage that we just need somewhere to place a temporary value, and it's widely used in computer
For example, SQL server has a database called "tempdb", which stored temporary databases, a video app may have a "temp" folder to save some videos you've watched
You can see many temps in your computer or cellphone😀
+ 8
`temp` is the synonym for `temporary`. That means a variable is defined to hold some intermediate values needed for performing an operation. For example, swapping the values of two variables usually gets done by a third variable which we call it `temp` or `t` or something like that.
+ 6
Usually means "temporary", but actually, variable names can be anything, you can even use your name as variables, so it's not very meaningful to ask what does a variable name mean in general😂
+ 4
Okay, let me show you the case C++ Soldier (Babak) mentioned above.
int x = 3;
int y = 4;
Now for some reason you want to switch the two values. If you write x = 4 to start the switching, the 3 will be lost; same if you start with y.
So here comes the tmp!
int tmp;
We prepare a container to keep one of our numbers safe, but only 'temporarily'.
tmp = x;
Now tmp holds the 3.
x = y;
For a moment, both x and y hold the 4, but we have saved our 3!
y = tmp;
So by having a reserve container in place, you can switch the values (for example when you sort a big list of numbers pair by pair).
In Python, by the way, you can just write:
y, x = x, y <-- :'D
+ 4
HonFu AWESOME! 8D
+ 1
I have an example of my own if you don't mind.
In this code (starting with line 53) I multiply large numbers digit by digit, like on paper. You know: Where you get several numbers, and in the end you add them up.
I need to store each number (list of digits) in a list. But while I am calculating each number, I have to store it somewhere.
So I used the name 'tmp'. Because what actually matters is the list of numbers I am building, not every single in-between result.
https://code.sololearn.com/cV3o3NWxVZ11/?ref=app
+ 1
Honfu , thanks for your help ,
but I'm sooo beginner for your 118 lines 😂😂, I felt dizzy when I opened it !😶
can you give me another simple one ?
+ 1
Haha, sorry. :-)
I thought since we all multiplied before, it would be an illustrative example. Let me look...
Hm...
Funnily enough, or maybe reasonably, I find tmp only in my more complicated codes.
Reasonable because: Namegiving is about clarity, right? If you calculate a factorial, it makes sense to call it that way - so that a reader (in the first place you) can make sense of it quickly.
But names can also confuse, when they give meaning to something that hasn't all that much meaning. That's why we just take 'i' in for loops. It doesn't mean anything, we're just counting, and everybody knows that.
So by using 'tmp' you give the reader (again: you!) a signal: 'Concentrate on the important stuff, this is just a necessary in-between variable!'
+ 1
HonFu , C++ Soldier (Babak) ,Flandre Scarlet
Thank you guys! , finally i've got the answer 😍
it was easy to understand by your teaching ~ 😃
0
https://code.sololearn.com/c3u9GQuxTjnc/?ref=app
This is the code , I found it in my book :/