If the main block of code gets a value from the user, assigns it to an integer, and then changes that interger's value, is there a way for a function (created outside the main block of code) to access the original value?


int height;
void line(void);

int main (void)

  printf ("Height: ");

 height = get_int();

for (;height>0; height--)

I have a function within the line function that requires access to the original height.


You have global var height. When you get value from user, assign it to height. Then copy it to new local var height1 which you can change as you want.

  • How do I do that? If I try int height1 = height; within the main block of code it gives me error messages like: "use of undeclared identifier 'height1'; did you mean 'height'?" – Chaim Tzvi Kalish Jul 11 '17 at 18:17
  • No I mean height1. – obesman Jul 12 '17 at 3:15
  • int height1 = height declares and initializes variable correctly. You may see error "unused variable height1" - just use height1 somehow below in the main. – obesman Jul 12 '17 at 3:22
  • The usage of global variables should be deferred when there are easier ways to achieve the same goal. – ChrisG Jul 13 '17 at 7:56

You should store temporarily the value of height, to a new variable, so that you don't change the original value, like so:

void foo(int foo_height);

int main(void)
    int height = get_int();

    for (int tmp_height = height; tmp_height >= 0; tmp_height--)
        /* code */


void foo(int foo_height)
    /* code */
  • First of all, thanks for the help. 2nd, why are you suggesting i use foo instead of int? – Chaim Tzvi Kalish Jul 13 '17 at 0:58
  • foo is just the name of the function, that gets an int as input and returns nothing. In general, foo is a bogus keyword used in programming that you use when you want to point that the name is not significant. – ChrisG Jul 13 '17 at 7:55
  • Yes, you are right. This is the right way. – obesman Jul 13 '17 at 8:06
  • I was actually able to do it w/o the foo function.... I'm new to coding.. so is foo better to use even if it works a different way? – Chaim Tzvi Kalish Jul 13 '17 at 22:15
  • foo is just a placeholder. The function could be called anything. It could be called line() as you have in your original code. – ChrisG Jul 14 '17 at 9:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .