I'm starting at the word of programming so I'm reading a book about programming in C, it says that if I want to call a global variable into a function I need to call the global variable with (::) before the name... in terminal shows this:
funcion2.c:17:57: error: expected expression before ‘::’ token
17 | printf("value of global variable is %d\n",::k);
thaks.
#include <stdio.h>
void f1(void);
int K=5;
void main()
{
int I;
for(I=1; I<=3; I++)
{
f1();
}
void f1(void)
{
int k=2;
K+=K;
printf("VALUE IS: %d\n",k);
::K=::K+K; // <-
printf("VALUE IS %d\n",::k); // <-
}
}
The global variable is called before the main() function, and that beeing said, heres an example of programm using global variable in main() and in funtions:
#include<stdio.h>
void func_1();
void func_2();
int a, b = 10; // declaring and initializing global variables
int main()
{
printf("Global a = %d\n", a);
printf("Global b = %d\n\n", b);
func_1();
func_2();
// signal to operating system program ran fine
return 0;
}
void func_1()
{
printf("From func_1() Global a = %d\n", a);
printf("From func_1() Global b = %d\n\n", b);
}
void func_2()
{
int a = 5;
printf("Inside func_2() a = %d\n", a);
}
Output:
Global a = 0
Global b = 10
From func_1() Global a = 0
From func_1() Global b = 10
Inside func_2() a = 5
In line 4, a and b are declared as two global variables of type int. The variable a will be automatically initialized to 0. You can use variables a and b inside any function. Notice that inside function func_2() there is a local variable with the same name as a global variable. When there is a conflict between the global variable and local variable, the local variable gets the precedence, that's why inside the func_2() value of local variable a is printed.
Unlike local variables, global variables are not destroyed as soon as the function ends. They are available to any function until the program is executing.
Related
why this code --https://onlinegdb.com/AJUXuxMwt prints 908080
but if this code https://onlinegdb.com/ft4zurpP4 prints 908081??
How are these two declarations different, if they are not then why different outputs?-
#include <stdio.h>
int fun();
static int i = 10;
int main()
{
static int i = 90;
printf("Hello World %d\n",i);
fun();
fun();
return 0;
}
int fun()
{
static int i=80;
printf("%d \n",i);
i++;
return 0;
}
static int i=80;
static int i;
i=80;
I am a novice in C, just started but can not clear this one up.
First of all it does not print 908081 only
Hello World 90
80
81
Explanation.
When you are declaring the static automatic variable (local variable in function scope) it is initialized only once and keeps the value between the function calls.
static int i=80; - Initalization happens only once, then i keeps its value between the function calls. If you increase the i in your code the, the increased value will be kept when you call the function again.
static int i; i = 80; In this case it is not initialization only normal assignment. Every time the function is call the assignment i = 80; is executed, and i will have value of 80 after it.
This question already has an answer here:
Static variable with the same name in different file [duplicate]
(1 answer)
Closed 2 years ago.
int a, b, c = 0;
void prtFun(void);
int main()
{
static int a = 1; /* line 1 */
prtFun();
a += 1;
prtFun();
printf("\n %d %d ", a, b);
}
void prtFun(void)
{
static int a = 2; /* line 2 */
int b = 1;
a += ++b;
printf(" \n %d %d ", a, b);
}
The output to the above program is:
4,2
6,2
2,0
The local variables are generally stored in the stack of the function and the global variables and the static variables are stored in the data segment of the process right? in that case does having the same static variable name in multiple functions cause any conflicts? how are variables referred to in the data segment?
First of all when you ask the question format your code correctly
int a, b, c = 0;
void prtFun (void);
int main(void)
{
static int a = 1; /* line 1 */
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
void prtFun (void)
{
static int a = 2; /* line 2 */
int b = 1;
a += ++b;
printf (" \n %d %d " , a, b);
}
prtFun
b is an automatic variable and it is initialized every time you call this function.
a is static local variable and it is initialised only once and it keeps its value between the function calls. But its scope is local to the function prtFun
So when you call it first time a is assigned 4 and b is assigned 2. You print this values
in function main another a static local variable is assigned value of 2 (initialization + increment).
when you call the prtFun again you add 2 to the function own a variable which keeps the value from the previous call (4) and the result is 6. The b is initialized again to 1 and pre-incremented - so it 2.
Then in function main you print its own a variable (which is 2) and global variable b which is 0
what should be the output of this code below? and why?
I am not sure if the int i declared in the main function acts like a local variable, or not.
static int i = 3;
void f(int*j){
j++;
}
int g(int* j){
return i+=*j;
}
void h(int j, int k){
printf("%d,%d,%d\n", i,j,k);
}
int main(){
int i = 3;
int j = 5;
int *p = &i;
f(&i);
f(p);
for(;i<5;i++){
*p = g(p);
}
h(i,j);
return 0;
}
These calls of the function f
f(&i);
f(p);
have no effect.
Within the function g there is changed the global variable i
int g(int* j){
return i+=*j;
}
As the function is called in the loop
for(;i<5;i++){
*p = g(p);
}
one time for i equal to 3 then the global variable i will be equal to i + 3 = 6. The local variable i will be equal to the returned value of the global variable i (equal to 6) from the function and then will be incremented in the loop. So after exiting the loop it will be equal to 7..
This call
h(i,j);
outputs the global variable i and passed as arguments the local variables i and j. So its output will be 6,7,5.
#include <stdio.h>
void foo();
int main()
{
int b = 0;
int a[6] = {1,2,3,4,5,6};
foo();
return 0;
}
void foo()
{
//write some code to print a and b
}
My Question is:
How to get the local variable from outside without pass any parameter ?
That's not possible. You need to pass a pointer to it to access it from a function or make it global.
You can create two global variables and store each the values of a and b in them. In the foo() create two variables a and b and store values of ga and gb
#include <stdio.h>
void foo();
int gb,ga[6];
int main()
{
int b = 0;
int a[6] = {1,2,3,4,5,6};
gb=b;
for (int x = 0 ; x<6 ; x++)
ga[x]=a[x];
foo();
return 0;
}
void foo()
{
int a=ga;
int b[6];
for (int x = 0 ; x<6 ; x++)
b[x]=gb[x];
//now you can use a and b here
}
Declare a global pointer,and inside main,assign the address of the local variabe to that pointer,and use it from anywhere.
No we cant. We can make public class or even another function. As we are defining it in the main, we can't.
Here's a code written in c
#include<stdio.h>
int foo()
{
static int a=0;
a=a+1;
return a;
}
int main()
{
foo();
foo();
printf("%d",foo());
}
I've compiled this code using gcc11 in eclipse IDE and I've got 3 as my output.
Here's what I think should happen which leads me to the output as 1 not 3.
Function call-1: The main function calls the function foo and the
control goes to the function foo then the variable 'a' in foo is
created with an initial value of zero then it is incremented by
one and this incremented value (1) is returned to the main function. At this step the variables created for the function foo should have been destroyed.
Function call-2: Same as Function call-1:
Function call-3: Same as Function call-1:
In the end the value printed by the printf function in main should have been 1.
Why the output of the program is 3?
Try this with more printing.
#include<stdio.h>
int foo()
{
static int a=0;
a=a+1;
printf("a is now: %d",a);
return a;
}
int main()
{
foo();
foo();
printf("%d",foo());
}
You will notice that the variable a is initialised just the once and its value is retained across function call invocations.
See here: http://en.wikipedia.org/wiki/Static_variable
static variables are not destroyed when the function returns. Remove that keyword and it will work as you expected.
Edit:
The static variable is only present in the scope of its own function. You can create other variables with the same name (static or otherwise) in other functions.
void foo()
{
static int a = 0;
a++;
printf("%d\n", a);
}
void bar()
{
int a = 10;
a++;
printf("%d\n", a);
}
void baz()
{
static int a = 100;
a++;
printf("%d\n", a);
}
int main()
{
foo();
bar();
baz();
foo();
bar();
baz();
return 0
}
This will print:
1
11
101
2
11
102
Here, the variable a is a Static Variable.
Although the scope of variable has to be ended after the complete execution of function. But the Static variable has Scope through out the program and they are not deleted throughout the program.
if it was
int a;
in the place of
static int a;
The result would have been different.
Static variable inside a function keeps its value between invocations.
see the below example for difference between static and normal variable.
void sample()
{
int nv = 10;//nv refers normal variable.
static int sv = 10;//sv refers static variable.
nv += 5;
sv += 5;
printf("nv = %d, sv = %d\n", nv, sv);
}
int main()
{
int i;
for (i = 0; i < 5; ++i)
sample();
}
output:
nv = 15, sv = 15
nv = 15, sv = 20
nv = 15, sv = 25
nv = 15, sv = 30
nv = 15, sv = 35