How to Declare a Global Variable in C++

How to declare global variable in C?

/* a.h */
extern int globali; /* Declaration for compilation */
/* Visible here */

Later make sure you define in (exactly) one of the compilation units.

/* something.c */
int globali = 42; /* Definition for linking */

Where can I declare global variable in c program , whether in header or source file

Assuming your variable is global and non static.

You need to declare it in a header file. We use extern keyword for this. As pointed out in comments, this keywords is not necessary, but most C users prefer to use it in headers, this is a convention.

stackoverflow.h:

#ifndef STACHOVERFLOW_H
#define STACHOVERFLOW_H

extern int my_var;

#ifndef

And you initialize it in source file. (Use of keyword extern is prohibited if you want to provide an initialization value).

stackoverflow.c

#include "stackoverflow.h"

int my_var = 50;

Do not put initialization value in a header, or you will get a linker error if the header is used at least twice.

Now you can use your variable in any other module by including the header.

main.c

#include <stdio.h>
#include "stackoverflow.h"

int main()
{
printf("my_var = %d\n", my_var);
return 0;
}

Including header "stackoverflow.h" in "stackoverflow.c" is a way to get sure definitions in source file match declarations in header file. This permit to have errors as soon as compilation instead of sometimes cryptic linker errors.

Edit: This is not at all the way to make a variable "private". You have to use a static variable to make it "private". See R Sahu's answer

How to declare a Global variable inside any function in 'C'?

You may want to use an array allocated into the heap by using malloc

#include<stdio.h>
int *a;
int main()
{
int n;
scanf("%d", &n);
a = malloc(sizeof(*a) * n);
if(a == NULL) {
// malloc error
}

// use your array here

free(a); // at the end of the program make sure to release the memory allocated before
}

C: Define global array variable that user can declare its size

Declare a pointer to an int as a global variable and initialize it in main using malloc.

/* outside any function, so it's a global variable: */
int *array;
size_t array_size;

/* inside main(): */
array_size = user_defined_size;
array = malloc( sizeof(int)*array_size);
if ( array == NULL) {
/* exit - memory allocation failed. */
}
/* do stuff with array */
free(array);

If you need to access the global variable from another module (source file), declare it there again using

extern int *array;
extern size_t array_size;

or, preferably, declare them extern in a header file included in any source file that uses the array, including the source where they are defined (to ensure type consistency).

How to access local and global variable with same name in C

You could cheat and create a pointer to the global i before declaring the local i:

void fun2( void )
{
int *ip = &i; // get address of global i
int i = 50; // local i ”shadows" global i

printf( "local i = %d, global i = %d\n", i, *ip );
}

EDIT

Seeing as this answer got accepted, I must emphasize that you should never write code like this. This is a band-aid around poor programming practice.

Avoid globals where possible, and where not possible use a naming convention that clearly marks them as global and is unlikely to be shadowed (such as prefixing with a g_ or something similar).

I can't tell you how many hours I've wasted chasing down issues that were due to a naming collision like this.

How to declare a global const variable and initialize it with a function in C?

How to declare a global const variable and initialize it with a function in C?

There is no way. It is not possible.

Is there any simple, portable way to solve my problem?

No.

You can use a macro #define MY_FUNCTION() (42).

You can write a separate program, that uses that C function (or is written in a completely different language), that generates C source code with that constant and that generated source code is then compiled with your project.

Or you can switch to a different programming language with more features, for example Rust, D, C++.



Related Topics



Leave a reply



Submit