Can a variable in main function be access outside the main? - c

#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.

Related

Auto VS Static Variable Scope in C programming

Helloo!! I am currently learning about variable scope in C programming, auto, static and extern. I am running this code below and I am not sure why it works. I thought auto variables are only defined in the function it is defined in and does not retain its value? How is it that this code is working? Shouldn't the int a,b be static variables instead?
#include <stdio.h>
void sum(void)
{
auto int a,b;
a = 91; b = 7;
printf("%d + %d = %d\n",a,b,a+b);
}
int main()
{
puts("Calling the sum() function:");
sum();
puts("done");
return(0);
}
Your variables are not retaining anything. They're always the same value because you're explicitly assigning them the same values in sum().
a = 91; b = 7;
If you change your code to accept the variables as parameters instead, you'll get the output you would expect.
#include <stdio.h>
void sum(int a, int b)
{
printf("%d + %d = %d\n",a,b,a+b);
}
int main()
{
puts("Calling the sum() function:");
sum(91, 7);
sum(23, 9);
auto int x = 44;
auto int y = 24;
sum(x, y);
puts("done");
return(0);
}
If you try to access x or y from within sum() now, you'll get an error that they aren't in scope.

About a static value in function's parameter in C

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.

Life of a variable defined in a function other than main

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

How to access global variable when there is a local and global conflict [duplicate]

This question already has an answer here:
how refer to a local variable share same name of a global variable in C? [duplicate]
(1 answer)
Closed 9 years ago.
Code :
int a = 33;
int main()
{
int a = 40; // local variables always win when there is a conflict between local and global.
// Here how can i access global variable 'a' having value '33'.
}
If you ask : Why would someone want to do above thing? Why [a-zA-Z]* ?
My answer would be : Just to know that 'it is possible to do so'.
Thanks.
How about this old trick:
int main()
{
int a = 40; // local variables always win when there is a conflict between local and global.
{
extern int a;
printf("%d\n", a);
}
}
int a = 33;
int main()
{
int a = 40;
int b;
{
extern int a;
b = a;
}
/* now b contains the value of the global a */
}
A harder problem is getting a if it's static with file scope, but that's also solvable:
static int a = 33;
static int *get_a() { return &a; }
int main()
{
int a = 40;
int b = *get_a();
/* now b contains the value of the global a */
}
IT IS C++, I OVERLOOKED C tag, SORRY !
int a = 100;
int main()
{
int a = 20;
int x = a; // Local, x is 20
int y = ::a; // Global, y is 100
return 0;
}

how to set a int value passed by parameter to a function and assign it to global so I can use outside of the function?

how to set a int value passed by parameter to a function and assign it to global so I can use outside of the function?
Example:
int assignValues(int valor_assign1, valor_assign2){
valor_assign1 = 7;
valor_assign2 = 3;
}
main (){
int valor1 = 0;
int valor2 = 0;
assignValues(valor1,valor2);
printf("%d,%d",valor1, valor2);
}
The output is actually 0,0 I want it to be 7,3 how do I do this?
I know its a simple question but I've been trying and searching but I can't find anything like that =/
Thanks in advance.
You could pass a pointer to your integers into the function, instead of passing the values. E.g.:
int assignValues(int *valor_assign1, int *valor_assign2){
*valor_assign1 = 7;
*valor_assign2 = 3;
}
main (){
int valor1 = 0;
int valor2 = 0;
assignValues(&valor1, &valor2);
printf("%d,%d",valor1, valor2);
}
You might want to read a pointer tutorial, however.
Pass the pointers:
int assignValues(int *valor_assign1, int *valor_assign2){
*valor_assign1 = 7;
*valor_assign2 = 3;
}
main (){
int valor1 = 0;
int valor2 = 0;
assignValues(&valor1,&valor2);
printf("%d,%d",valor1, valor2);
}
You pass pointers to the variables:
void assignvalues(int *v1, int *v2) {
*v1 = 7;
*v2 = 3;
}
But, in your snippet, valor1 and valor2 are not global. They are local to main. If they were global, you could just assign to them
#include <stdio.h>
int valor1; /* global */
int valor2; /* variables */
int assignvalues(void) {
valor1 = 7;
valor2 = 3;
}
int main(void) {
printf("%d, %d\n", valor1, valor2);
}
Note: the usage of globals is frowned upon. Your usage, with local variables, is much better.

Resources