# define a variable in header file

#### Kittu20

Oct 21, 2022
17
I understand we declare variables and functions in header file in c programming.

Temp.h
Code:
extern int x;
void foo();

By define I mean we assign a value to a variable, which is called defined.

What is means define a variable in header file in c language

Code:
extern int x; // declared variable x in header file
int y = 10; // defined variable y in header file Temp. h
void foo();

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,884
By define I mean we assign a value to a variable, which is called defined.
• In C, there is no such thing as a "definition" of a variable. The term commonly used is "declaration". Life will be easier for you when you stick to established nomenclature.
• The declaration of a variable in C does not necessaryily include a value. It is from the perspective of the programming language perfectly o.k. to simply declare a variable by name and type only:
C:
int foo;
From the perspective of "good" programming it is good style to declare an initial value, too. This avoids using a uninitialized variable within the code which can lead to unpredictable results.
What is means define a variable in header file in c language
Sorry, I don't get the question here. Can you elaborate in more detail, please?

#### Kittu20

Oct 21, 2022
17
Sorry, I don't get the question here. Can you elaborate in more detail, please?
Mainly we declare variables in header file

Can we assign value to any variable in header file ?

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,884
Mainly we declare variables in header file
Not necessarily. You'll also find lots of declarations within the code.
Can we assign value to any variable in header file ?
Yes. Just try it.

A header file contains normal C code. Anything that can be done in the code can be done in a header file. Header files are an easy way to keep declarations consistent across many code files whcih include these header files. So in case you need to change e.g. the declaration of a variable, you have to do it only once in the header file, not many times in each code file that uses the variable.

#### LilyFathom

Jan 3, 2023
8
Mainly we declare variables in header file

Can we assign value to any variable in header file ?
The whole idea of a header file is to simplify our code. In essence a header file is some code saved elsewhere. The idea of "including a header," any language not just C is to insert code at the beginning and then stuff like functions can be called without needing to write them several times over for each program we write. For example;

Midi.h is a library with functions specifically for the midi protocal. Statements such as "my note handle on," calls the appropriate function should a note on message is recieved or transmitted. Rx and Tx pins of arduino...

Hope this helps clarify what headers are and how they are used.

#### Kittu20

Oct 21, 2022
17
Yes. Just try it.
i tried but i don't get the output as it should

main.c
C:
#include<stdio.h>
#include"temp.h"

int x;

int main()
{
printf("%d", x);
return 0;
}

temp.h

C:
int x = 10;

Program output

Code:
0

#### HarryA

Jan 22, 2017
76
Does this help? Using Arduino C code;
 /// file to be included #ifndef _TEMP_H #define _TEMP_H extern int a = 10; #endif 

 //in main code file #include "temp.h" void setup() { Serial.begin(9600) ; //used to display results } void loop() { //display a Serial.print(" int a = "); Serial.println(a); //display add new line //slow the display down delay(1000); } 
prints: int a = 10

also see: Extern Keyword in C

Last edited:

Feb 19, 2021
271
Side comment, be careful with variable add / declare in header files. If
IDE is generating the file it can overwrite your declaration, so make sure
you know what your tool is doing. Also make sure you use the modifier
"extern" where appropriate.

Regards, Dana.

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,884
i tried but i don't get the output as it should
You get the output you should get, but you expect the wrong output. You have to consider the sequence of operations:
First the preprocessor includes temp.h into main.c. With your code example this reuslts in an intermediate file. Instead of

Original:
C:
#include"temp.h"

int x;

int main()
{
printf("%d", x);
return 0;
}

After preprocessing:
Original:
C:
int x = 10;    // instead of #include"temp.h"

int x;

int main()
{
printf("%d", x);
return 0;
}
As you see the second declaration
C:
int x;
voids the first declaration
C:
int x = 10;
In your case the compiler automatically assigns x = 0 to the re-declared variable.

It's like when you ask someone to buy apples for you, but when he's on his way you call him to bring bananas instead. What will he take home: apples or bananas?

#### Kittu20

Oct 21, 2022
17
You get the output you should get, but you expect the wrong output. You have to consider the sequence of operations:
First the preprocessor includes temp.h into main.c. With your code example this reuslts in an intermediate file. Instead of
static global variable can be accessible where it's defined

I am defining static global variable x in temp.h file and including temp.c file so it can be accesble only in temp.c file

temp.h
Code:
 static int x = 10;
temp.c
Code:
#include"temp.h"

int foo()

{

x++;

return x;

}
main.c
Code:
#include<stdio.h>
extern int foo();
int main()
{
int y = foo();
printf("%d", y);

return 0;
}

Output
Code:
11

I thought program would give error because x is static global variable and we can't access it in main.c file

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,884
static global variable can be accessible where it's defined
Global variables can be accessed everywhere. That's why they are "global".

#### Kittu20

Oct 21, 2022
17
Global variables can be accessed everywhere. That's why they are "global".
I am not agree with you
x is static global variable and I can't access it in temp.c
temp.h
Code:
 static int x = 10;
temp.c

Code:
#include<stdio.h>
#include"temp.h"
x+1;
printf("%d", x);

temp.c:3:2: error: expected '=', ',', ';', 'asm' or '__attribute__' before '+' token
x+1;
^
temp.c:4:8: error: expected declaration specifiers or '...' before string constant
printf("%d", x);
^~~~
temp.c:4:14: error: expected declaration specifiers or '...' before 'x'
printf("%d", x);

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,884
The problem is not the globality or acessibility of x, it is an error in your code:
Code:
Code:
x+1;
is invalid.
Correct instructions are:
Code:
x = x+1;
or
Code:
x += 1;

#### Kittu20

Oct 21, 2022
17
The problem is not the globality or acessibility of x, it is an error in your code:
Code:
Code:
x+1;
is invalid.
Correct instructions are:

or
Code:
x += 1;
temp.c
Code:
#include<stdio.h>
#include"temp.h"
x = x+1;
printf("%d", x);
temp.c:3:1: warning: data definition has no type or storage class
x = x+1;
^
temp.c:3:1: warning: type defaults to 'int' in declaration of 'x' [-Wimplicit-int]
temp.c:3:1: error: redefinition of 'x'
In file included from temp.c:2:0:
temp.h:1:13: note: previous definition of 'x' was here
static int x = 10;
^
temp.c:3:5: error: initializer element is not constant
x = x+1;
^
temp.c:4:8: error: expected declaration specifiers or '...' before string constant
printf("%d", x);
^~~~
temp.c:4:14: error: expected declaration specifiers or '...' before 'x'
printf("%d", x);
^

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,884
There is no main function in temp.c.

I tested this code and it works fine:
temp.h:
C:
static int x = 10;

main.c:
C:
#include<stdio.h>
#include"temp.h"

int main()
{
x += 1;
printf("%d", x);

return 0;
}

#### Kittu20

Oct 21, 2022
17
There is no main function in temp.c.

I tested this code and it works fine:
Thanks. I understood if a static global variable is defined in a header file it is required to be included header file in the source file.

we can't access the static global variable without including the header file in a source file

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,884
we can't access the static global variable without including the header file in a source file
You can't access anything that is defined in a header file if you don't include the header file.
Such is the design of header files.

#### Kittu20

Oct 21, 2022
17
You can't access anything that is defined in a header file if you don't include the header file.
Such is the design of header files.
I am trying write a program that reverse array

why line 12 print value 1 instead of printing 5 ?

C:
#include<stdio.h>

void foo ( int array[], int size)
{
int i, temp;
int start = 0;
int last = 4;
for ( i = 0; i < size; i++)
{
temp = array[start];   // temp = 1
array[start] = array[last]; //array[start] = 5
printf("%d", array[start]); // this line print value 1
array[last] = temp;
break;
start++; last--;

}
}
int main()
{
int array[]=  {5, 4, 3, 2, 1};
foo(array, 5);
return 0;
}

Feb 19, 2021
271
Because in foo you keep passing to print the element that you
just replaced its value with array[4] ?

Regards, Dana.

#### Kittu20

Oct 21, 2022
17
Because in foo you keep passing to print the element that you
just replaced its value with array[4] ?

Regards, Dana.

why last two number doesn't swap correctly ? How to swap them ?

C:
#include<stdio.h>

void foo ( int array[], int size)
{
int i, temp;
int start = 0;
int last = 4;

for ( i = 0; i < size; i++)
{
temp = array[start];
array[start] = array[last];
printf("%d \n", array[start]);
array[last] = temp;
start++; last--;
}

}
int main()
{
int array[]=  {5, 4, 3, 2, 1};
foo(array, 5);
return 0;
}

output
Code:
1
2
3
2
1

Replies
5
Views
2K
Replies
5
Views
1K
Replies
3
Views
1K
D
Replies
2
Views
1K
Dick Chambers
D
M
Replies
5
Views
2K
M