Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can i modify a global variable in a function and continue with that same value in other functions?

+7 votes
asked Sep 19, 2023 by Pessoua (500 points)
The problem goes like this
/* Objective: By the 3rd printf on the main function it needs to say 0 *\

int gobal_variable = 1;
int main(){
int num1;
printf("test value -> ");
scanf("%d", &num1);
printf("%d", global_varible); // 1?
Test(1);
printf("%d, global_variable); // 0?
return 0;
}

int Test(int num1){
if (num1 == 1){
printf("Test 1 Passed");
global_variable --; // = 0
main();
} else {
printf("Test failed");
}
}

2 Answers

+2 votes
answered Sep 19, 2023 by Peter Minarik (88,520 points)

You can change the value of a global variable and after the change, everywhere in the code, this new value is stored in the global variable.

Your code has many syntax errors, misspelled variable names, and an unusual logic to call the main function from other functions. Instead, I'll provide my own example below.

Static and global variables

In C, global variables are declared outside of functions (without the static keyword). Anyone in the code can see their values (but other files may need to reference them with extern keyword).

Static variables inside functions are initialized only once and then keep their values within executions of the same function -- without getting re-initialised. Remove the static keyword, and see how the execution changes.

Here's the example code below I promised:

#include <stdio.h>

int functionCallCount = 0;

void PrintFunctionCallCount()
{
    printf("functionCallCount: %d\n", functionCallCount);
}

void Foo()
{
    static int fooCount = 0;

    functionCallCount++;
    fooCount++;
    printf("Foo has been called: %d times\n", fooCount);
}

void Bar()
{
    functionCallCount++;
    printf("Bar has been called.\n");
}

int main()
{
    Foo();
    Bar();
    Foo();
    Bar();
    Foo();
    
    PrintFunctionCallCount();
    return 0;
}

Play around and see how the execution changes if you turn static variables into regular variables.

Have fun!

commented Sep 20, 2023 by Pessoua (500 points)
Im sorry about the place holder code, it was just a small example of my problem :>
But many thanks to this helpfull guide! I will def. play around with this new information and try to make my code run smoothly!
Thank you overall :)
commented Sep 20, 2023 by Peter Minarik (88,520 points)
My pleasure. Good luck! :)
0 votes
answered Sep 22, 2023 by Mohammed Raiyan (230 points)

int gobal_variable = 1;
int main(){
int num1;
printf("test value -> ");
scanf("%d", &num1);
printf("%d", global_varible); // 1?
Test(1);
printf("%d, global_variable); // 0?
return 0;
}

int Test(int num1){
if (num1 == 1){
printf("Test 1 Passed");
global_variable --; // = 0
main();
} else {
printf("Test failed");
}
}

commented Sep 22, 2023 by Mohammed Raiyan (230 points)
easy ah iruthathu akka
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...