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.
Related
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.
I would like to ask if there could be any problem in passing a static variable to a function by reference, in order to let that function modify the variable.
A stupid example is:
void increment (int *b)
{
*b = *b + 1;
}
void f()
{
static int a = 4;
increment(&a);
printf("%d\n", a);
}
int main()
{
f();
f();
f();
return 0;
}
I executed it and it seems to work fine, printing
5
6
7
but I would like to understand if this is a sort of "bad" programming practice, that is preferable not to do.
#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
I have came across this interview question. I know it's tricky but can't think of any approach.
Change the program so that the output of printf is always 20. Only foo() can be changed. main() function can not be changed.
void foo()
{
// Add Here
}
int main()
{
int i = 20;
foo();
i = 100;
printf("%d", i);
//Some other computation. Doesn't have any printf statements.
return 0;
}
We can use Macro Arguments to change the output of printf.
void foo()
{
#define printf(x, y) printf(x, 20);
}
int main()
{
int i = 20;
foo();
i = 100;
printf("%d", i);
return 0;
}
By using this, during printf("%d",i) will get mapped to macro expansion printf("%d",20)
foo() could merely print 20 and call exit().