School of Physics, Mathematics, and Computing 
Not logged in (login)
help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.

Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!" followup message.

How do I ask a good question?
 

Final examination information
Options:
RSS cloud
Jump to:

about static variable

9 of 951 articles shown, currently no other people reading this forum.
photo
From: ANONYMOUS
Date: Tue 10th Aug 2021, 8:36pm
Actions: 
Login-to-reply

Hi,
I'm trying to make Rot as a static global variable so that Rot can be changed, but it 
still not works.So how to let Rot change every time in question9, lab1?

about static variable

photo
From: Christopher M.
Date: Wed 11th Aug 2021, 5:18am
Actions: 
Login-to-reply

ANONYMOUS wrote:

> Hi,
> I'm trying to make Rot as a static global variable so that Rot can be changed, but it 
> still not works.So how to let Rot change every time in question9, lab1?

All variables in C11 may be modified**

So it does not matter whether a variable is defined as a global, or not, or whether it is 
declared as a static, or not - your program will always be able to modify it.

So your problem is more likely to be with the logic of your implementation, not with C's 
variables.


** with the exception of variables declared with the modifier 'const',
   which declares that you have no *intention* of modifying them (sort of read-only).

about static variable

photo
From: ANONYMOUS  O.P.
Date: Wed 11th Aug 2021, 9:41am
Actions: 
Login-to-reply

but if I let Rot be a const variable, then I can't change it's value if I want, can you give me 
some idea to modify Rot so that it can work correctly for question9

about static variable

photo
From: Christopher M.
Date: Wed 11th Aug 2021, 9:46am
Edited: 2 minutes later
Actions: 
Login-to-reply

ANONYMOUS wrote:

> but if I let Rot be a const variable, then I can't change it's value if I want, can you give me 
> some idea to modify Rot so that it can work correctly for question9

I was not suggesting that you use 'const' at all.
I was indicating that (nearly) all variables, static, global, ..... can be modified.

So I do not think that that is your problem.

What gives you the suspicion that your variable named 'Rot' is not, or cannot, be modified?

If you have:

   printf("%i\n", Rot );
   Rot = Rot + 1;
   printf("%i\n", Rot );

what happens?

about static variable

photo
From: ANONYMOUS  O.P.
Date: Wed 11th Aug 2021, 10:21am
Actions: 
Login-to-reply

I mean question9 asks program to recieve an argument if it's a number, then the rotation amount changes 
to this value.Can you give me some idea how to achieve this? What do I need to do with the "Rot" 
variable?

about static variable

photo
From: Christopher M.
Date: Wed 11th Aug 2021, 10:49am
Edited: 3 minutes later
Actions: 
Login-to-reply

ANONYMOUS wrote:

> I mean question9 asks program to recieve an argument if it's a number, then the rotation amount changes 
> to this value.Can you give me some idea how to achieve this? What do I need to do with the "Rot" 
> variable?

It seems that you're misunderstanding the question,  rather than having any programming problems with variables 
that cannot be modified.

If you invoke your program with, say, the arguments:

    ./rotate9  horse  lion  4  elephant  17  22  budgie  alpaca

then:

   horse will be rotated by 13 (the starting value)
   lion will also be rotated by 13 (unchanged)
   elephant will be rotated by 4 (the new rotation value)
   budgie will be rotated by 22
   alpaca will also be rotated by 22

Does that make more sense?

_____
I'm (genuinely) interested to understand how you read question 9 on the labsheet to mean that it had something 
to do with static or global variables  (so that I can improve the wording of the question).

And what was your evidence that any of your variables could not be modified?

Thanks,

about static variable

photo
From: Amitava D.
Date: Wed 11th Aug 2021, 11:38am
Actions: 
Login-to-reply

ANONYMOUS wrote:

> I mean question9 asks program to recieve an argument if it's a number, then the rotation amount changes 
> to this value.Can you give me some idea how to achieve this? What do I need to do with the "Rot" 
> variable?
You can only read strings from the command line, if you want an argument to be an integer, you have to use the atoi() (ascii to integer) function. Is that the problem?

about static variable

photo
From: ANONYMOUS  O.P.
Date: Wed 11th Aug 2021, 2:56pm
Actions: 
Login-to-reply

sorry,I just misundertood question,I thought if we pass an integer, then the next time when running the program, the 
Rot needs to be that number.

about static variable

photo
From: Christopher M.
Date: Wed 11th Aug 2021, 3:20pm
Actions: 
Login-to-reply

ANONYMOUS wrote:

> sorry,I just misundertood question,I thought if we pass an integer, then the next time when running the program, the 
> Rot needs to be that number.

OK, sounds like you're getting it now.

When a program runs, it starts with a 'fresh' copy of its variables (held in memory).
When a process terminates, its final values of its variables are entirely forgotten
(and the memory it was using will soon be used by another process).

No information about a previosuly running program is remembered for the next time it runs -
UNLESS that program, itself, saves some information somewhere, such as by using a file on disk,
reading/creating the file each time the program runs, and writing information each time the program finishes.

I think I can see where your earlier confusion with the keyword 'static' came from, but in this context it only means within each 
*single* execution of a program,  not across multiple executions.

Good luck,
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  4:54pm Oct 24 2021