From my current understanding:
Every thread has its own stack
For a local variable int a in main(), it's on the stack of main()
After a thread created in main(), they can both access the same a. Wait a minute...?
What's wrong of my reasoning...
I should probably not to guess but here is it: It seems like on the perspective of the thread the parent-stack is ... global?
(I know this is probably asked before but I cannot find the exact one explaining this)
variables that are inside a function are called local variables.
void function_1(){ int a,b;}
A and B can only be used inside this function. After the function is called, the variables will be destroyed.
Next example:
#include<stdio.h>
int main()
{
int a = 100;
{
/* variable a declared in this block is
completely different from variable
declared outside. */
int a = 10;
printf("Inner a = %d\n", a);
}
printf("Outer a = %d\n", a);
// signal to operating system everything works fine
return 0;
}
a can be used inside main()
Global Variable:
#include<stdio.h>
void func_1();
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();
// 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);
}
A and B can be used inside main() and func_1().
Related
How is this code different from the second one? What is the difference between initializing it when declaring and initializing it in the next line?
#include<stdio.h>
int main()
{
static int i;
i=1;
printf("%d\n",i);
i++;
if(i==6)
{
return 0;
}
main(); // recursive call of main()
}
////////////second code////////////////
#include<stdio.h>
int main()
{
static int i=1;
printf("%d\n",i);
i++;
if(i==6)
{
return 0;
}
main(); // recursive call of main()
}
Static variables persist between function calls, upon re-entring the function the variable will have the same value it was last assigned in the function.
If you initialise it in the declaration it will be initialised once to that value
If you set the value in code it will be set every time the code runs.
Normally main() is only executed once. but other fuctions are often executed multiple times.
I'm writing a program that's running on an embedded MCU, so all of my memory allocations are to the stack (no malloc()). I have a struct being created in a function and returned to a caller, and I'm trying to keep a reference to that variable in an array, prior to being returned.
The problem is that the variable stored in the array seems to be a copy of the variable returned to the caller, so they're not in sync. The code below illustrates the issue:
#include <stdio.h>
int *ARRAY_OF_VARIABLES[1];
int initalizeVariables() {
int i = 0;
ARRAY_OF_VARIABLES[0] = &i;
return i;
}
int main(void)
{
int test = initalizeVariables();
test = 1;
printf ("Test: %d, ARRAY_OF_VARIABLES[0]: %d\r\n", test, (int)*ARRAY_OF_VARIABLES[0]);
}
This prints: Test: 1, ARRAY_OF_VARIABLES[0]: 0
How can I keep "test" and "ARRAY_OF_VARIABLES[0]" in sync without malloc() and pointers?
I solved this by creating a function that I can pass a pointer of i back into for storage in ARRAY_OF_VARIABLES. Here's the new code:
#include <stdio.h>
int *ARRAY_OF_VARIABLES[1];
int initalizeVariables() {
int i = 0;
return i;
}
void setInArray(int *i, int idx) {
ARRAY_OF_VARIABLES[idx] = i;
}
int main(void)
{
int test = initalizeVariables();
setInArray(&test, 0);
test = 123;
printf ("Test: %d, ARRAY_OF_VARIABLES[0]: %d\r\n", test, *ARRAY_OF_VARIABLES[0]);
}
If there are any better ways to do this, please feel free to post them :).
my question is how does the function malloc_queue() can access variables from init_queue() without giving any arguments!
For example:
The main.c:
if (init_queue()) {
malloc_queue()
}
init_queue() creates the variable que:
int init_queue{
struct Queue *que;
return 1;
}
malloc_queue() want to do something with the variable que from init_queue():
void malloc_queue{
struct Queue *que = (struct Queue*)malloc(sizeof(struct Queue));
return;
}
but that doesnt work since malloc_queue doesnt know what que is. Are there any possible ways without giving any arguments?
Maybe you want a static variable outside functions, which has scope inside the residing file after its definition.
Check this code:
#include <stdio.h>
static int x = 0;
void a() {
x = 5;
}
void b() {
x*=2;
}
int main(int argc, char * argv[]) {
printf("%d\n", x);
a();
printf("%d\n", x);
b();
printf("%d\n", x);
return 0;
}
Just as the comments suggested, read about c scopes.
If you need to declare struct other than primary types, you might need to declare a pointer to structure as the static variable outside function, then allocate memory (e.g malloc()) inside one of your functions.
I am trying to print an int a before and after calling a set function to set the value of a. I am doing this in C. When I compile it I have no errors but when I attempt to run it, I get a segmentation fault.
Here is what I have so far:
#include <stdio.h>
int main(){
int* a;
printf("%d",*a);
set(10);
printf("%d", *a);
return 0;
}
int set(int*a, int val){
*a = val;
return *a;
}
int main(){
int* a;
printf("%d",*a);
What you have there is a pointer to an int rather than an actual int.
And, while that's the correct way to print the int it points to, unfortunately it points to an arbitrary memory location which is why you're crashing.
You are not allowed to dereference arbitrary pointers, they have to point to something valid, such as if you begin your code with:
int main(){
int target_of_a = 42;
int *a = &target_of_a;
printf ("%d", *a);
In addition, you probably should be calling set with something like:
set (a, 10);
something the compiler would generally warn you about though, in this case, it would probably just say it didn't know about set at the time you called it. If it had known, it could have told you about the parameter mismatch.
One way for you to acheive that is to ensure you have a prototype defined for the function before you call it:
int set(int*,int);
or just move the function to before main. With all those changes (and a bit of a general tidy up), you'd end up with:
#include <stdio.h>
int set (int *a, int val) {
*a = val;
return *a;
}
int main (void) {
int target_of_a = 42;
int *a = &target_of_a;
printf ("%d\n", *a);
set (a, 10);
printf ("%d\n", *a);
return 0;
}
The wisdom of returning the variable you're changing is also debatable but there are situations where that might be useful (such as if you want to us it immediately without another statement: printf ("%d\n", set (a, 10)); for example) so I've left that as is.
I should also mention that it's a little unusual to artificially create a pointer variable in a situation like this.
Now it may be that your code is just a simplification of some more complex scenario where you already have a pointer but, if not, the usual way to do this would be to just have the int itself and just use & to create one on the fly:
#include <stdio.h>
int set (int *a, int val) {
*a = val;
return *a;
}
int main (void) {
int a = 42;
printf ("%d\n", a);
set (&a, 10);
printf ("%d\n", a);
return 0;
}
This code should work:
#include <stdio.h>
#define FIRST_VALUE 20
#define SECOND_VALUE 10
int main(){
int a = FIRST_VALUE; /* Declare a as an int variable. */
printf("Before set: a = %d\n",a); /* Print the first value. */
set(&a, SECOND_VALUE); /* Pass the ADDRESS of a to set. */
printf("After set: a = %d\n", a); /* Print the new value of a. */
return 0;
}
int set(int*a, int val){
*a = val;
return *a;
}
Note that the variable a in main() is not the same as the variable a in set(); you have to pass a pointer to a to set() in order for set() to operate on it.
Try this:
And remember, all functions before the main() (if you're using only one file)
Take a read on value and reference params.
#include <stdio.h>
int set(int* a, int val){
*a = val;
}
int main(){
int a = 2;
printf("%d\n", a);
set(&a, 10);
printf("%d\n", a);
return 0;
}
I'm a little confused about this:
#include <stdio.h>
#define MAXLINE 1000
int x;
int main()
{
int x;
getch();
return 0;
}
Where is the variable definition in this code? I'm assuming that it would be the external variable. In that case, shouldn't the variable in the function have an extern modifier?
What if the external variable was below the main function?
Example 1:
int x; // declares and defines global variable
int main()
{
int x; // declares and defines *new* local variable, which hides (shadows) the global variable **in this scope**
}
Example 2:
int main()
{
extern int x; // declares variable that will refer to variable defined *somewhere*
}
int x;
Example 3:
int x; // declares and defines global variable
int main()
{
extern int x; // redundant, declares variable that will refer to variable defined *somewhere*, but it is already visible in this scope
}
extern doesn't mean outside the current scope, it means an object with external linkage. An automatic variable never has external linkage, so your declaration int x inside main can't possibly refer to that. Hence it's hiding the global int x, that is, the the variable x with auto storage class is hiding the global x. You need to read more about storage classes in C
Refer the below program AFTER reading about them :
#include <stdio.h>
int i = 6;
int main()
{
int i = 4;
printf("%d\n", i); /* prints 4 */
{
extern int i; /* this i is now "current". */
printf("%d\n", i); /* prints 6 */
{
int *x = &i; /* Save the address of the "old" i,
* before making a new one. */
int i = 32; /* one more i. Becomes the "current" i.*/
printf("%d\n", i); /* prints 32 */
printf("%d\n", *x); /* prints 6 - "old" i through a pointer.*/
}
/* The "previous" i goes out of scope.
* That extern one is "current" again. */
printf("%d\n", i); /* prints 6 again */
}
/* That extern i goes out of scope.
* The only remaining i is now "current". */
printf("%d\n", i); /* prints 4 again */
return 0;
}