Is there any wrong about tha static variable? - c

I have a problem about a program. I bet that it has to do with the fact that I use static. Here is my t.h
static int cnt;
void f();
my main.c
#include <stdio.h>
#include "t.h"
void main()
{
cnt=0;
printf("before f : cnt=%d\n",cnt);
f();
printf("after f : cnt=%d\n",cnt);
}
and finally my f.c
#include "t.h"
void f()
{
cnt++;
}
The printf prints cnt=0 both times. How is this possible when I do cnt++? Any ideas?
Thanks in advance

In C, static means "Local to the module"
Take note, that the #include statements just pastes the header file in the including file.
therefore, you are creating two distinct symbols (happens to have the same logical name) in different modules.
f.c cnt is a different cnt then main.c
Note:
static in C has different meaning then its C++ counterpart.
and because C++ is C Compatible, static outside a class have the same meaning as in C
Edit:
In your case, you don't want a static you want a variable, but i guess you had problem with the Linker telling you about "ambiguous symbols".
I would suggest to declare an extern in the header file, and declare the actual variable in a module.
t.h
extern int cnt; // declaration of the variable cnt
main.cpp
#include
#include "t.h"
void main()
{
cnt=0;
printf("before f : cnt=%d\n",cnt);
f();
printf("after f : cnt=%d\n",cnt);
}
t.cpp
#include "t.h"
int cnt = 0; // actual definition of cnt
void f()
{
cnt++;
}

Data should not be defined in the header files.
In your example you will create a separate copy of that static variable in every compilation module which includes this .h file.

Don't define cnt in your header file. Instead, define it in f.c:
#include "t.h"
int cnt = 0;
void f(){
cnt++;
}
Then in main.c, add the following before the beginning of your main function:
extern int cnt;

Related

C static funtions accesible outside of file?

I used code blocks to make a project with main.c:
#include <stdio.h>
#include "t.h"
int main()
{
printf("%c\n", return_f('f'));
printf("%c\n", return_f(return_char(71)));
printf("%d\n", STATIC_INT);
return 0;
}
And t.h:
static int STATIC_INT = 14;
static unsigned char return_char(char n){
return (n/2 + 9);
}
unsigned char return_f(char n){
return ((n=='f' || n=='F') ? n+1 : 'f');
}
Since I assume static should limit globals and functions to their files, how does it allow to run/print out:
g
f
14
Or is that just not how it's supposed to work?
t.h is included textually before the actual compilation process takes place. Therefore static int STATIC_INT = 14; is part of your main.c file.
The real problem is that you are declaring variables in a header file which is almost always wrong.
It work because you import t.h in your .c file.
Static function can't be accesible outside of the file. But when you import t.h in your main.c file, all the code in t.h will be paste into main.c; so now your static function belong to main.c !
You have included t.h in your main.c, so these symbols are in the same unit of your main.c

How can I call to function that exists in other file?

Given 2 files, for examples:
file1.c :
int main(){
f();
return 0;
}
file2.c:
void f(){
return;
}
Why I can't call f from file1.c like that?
Because first you need to tell the compiler (declare) that it exists somewhere:
void f(); //function declaration
int main()
{
f();
return 0;
}
Usually, though, it is better to put such declarations in a separate header file (e.g. file2.h) so that later you could include this file (e.g. #include "file2.h") instead of duplicating such declaration in every other file where you need this function.
The problem is that file1.c does not "know" that the function f exists. You need to use a prototype. The standard way is to put prototypes in header files and definitions in .c files.
It could look like this:
file1.c:
#include "file2.h"
int main(){
f();
return 0;
}
file2.h:
#ifndef FILE2_H
#define FILE2_H
void f();
#endif
file2.c:
#include "file2.h"
void f(){
return;
}

Functions and variables scope in C

Why we don't use extern when using function from one .c file in another .c file , but we must do extern for variables case? Is it related to linker?
Functions are extern qualified by default (unless you change it to internal with static). For example,
int func(void) {
}
extern int func2(void) {
}
Both func and func2 are external. The extern keyword is optional for external functions.
Actually, function names act just like variable names, but function prototypes are extern by default.
From cpprerefence:
If a function declaration appears outside of any function, the identifier it introduces has file scope and external linkage, unless static is used or an earlier static declaration is visible.
you can create a .hfile,declare functions you want to use in the other .c files and #include the .hfile in the other .c files.
Demo program,
one.c
#include "one.h"
void func1() //defination
{
//code
}
one.h
void func1(); //declaration
main.c
#include <stdio.h>
#include "one.h"
int main()
{
func1();
}
Then compile program in Gcc Linux : gcc main.c one.c
Yes, Let consider you have one .c file as process.c and you declared it in process.h . Now if you want to use the function from process.c to suppose tools.c then simply #include "process.h" in tools.c and use ther function. The process.h and process.c file should be in your project.
process.c file
#include<stdio.h>
#include<conio.h>
#include "process.h"
unsigned int function_addition(unsigned int a, unsigned int b)
{
unsigned int c = 0;
c = a + b;
return c;
}
process.h:
<bla bla bla >
unsigned int function_addition(unsigned int a, unsigned int b);
<bla bla bla >
tools.c file:
#include<stdio.h>
#include<conio.h>
#include "process.h"
my_tools()
{
unsigned int X = 1, Y = 9, C = 0;
C = function_addition(X,Y);
}
All these files are in one project.

How to define variables in a header file, C

I want to declare and define (with a default value) a variable in a .h file.
When I do that I get
/tmp/cc19EVVe.o:(.data+0x0): multiple definition of `car_name'
/tmp/cc3twlar.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
How do I achieve my goal? Namely, to declare and define with default values a variable in a .h file and use that variable in multiple .c files?
Here is the A.h file
char * car_name = "Volkswagen";
void execute();
Here are the first file that uses the variable car_name defined in A.h: (The file is called execute.c)
#include "A.h"
#include <stdio.h>
#include <string.h>
void execute(){
int len = sizeof(car_name) + 2;
char car_name_with_new_line[len];
strncat(car_name_with_new_line, car_name, sizeof(car_name));
strncat(car_name_with_new_line, "\n", 1);
printf(car_name_with_new_line);
}
That's the other .c file: (It's called main.c)
#include "A.h"
int main(int argc, char ** argv){
execute();
return 0;
}
The answer is simple: Define your variables in exactly one compilation unit (.c file). Declare them in the header file associated with that .c file.
foo.h
extern char *g_name; // Declare that g_name exists
foo.c
#include "foo.h"
char *g_name; // Define g_name in one place
static char *m_private; // Private to foo.c, not "exported" via foo.h
main.c
#include "foo.h"
void somefunc(void)
{
// use g_name
}
1) define the variable in a single file, do not add a static modifier
2) place an extern statement for that variable in the header file.
then only one instance of the variable exists anyone that includes the header file can access the variable.
Note: it is poor programming practice to have global variables.
Good programming practice is to write accessor functions and hide the variable within a file. similar to the following:
static int myVariable = 0;
void setMyVariable( int myVariableParm )
{
myVariable = myVariableParm;
}
int getMyVariable( void )
{
return myVariable;
}

Use function pointer initialized in other source file

I have function pointer, that is declared as follows in file 1.h
// file : 1.h
typedef void (*my_func_ptr_T)(int , int);
In file 1.c I create a global instance and initialize it
// file 1.c
my_func_ptr_T func;
void callback (my_func_ptr_T _ptr) {
func = _ptr;
}
Now, how can i use this function ptr 'func' in another file say 2.c
// file 2.c
void do_callback() {
// i want to do this
func(1,2);
}
Check the below changes
// file : 1.h
typedef void (*my_func_ptr_T)(int , int);
extern my_func_ptr_T func;
//file 1.c
#include "1.h"
//func is visible
// file 2.c
#include "1.h"
//func is visible
Changing 2.c to below will help. Leave 1.h and 1.c unchanged.
#include "1.h"
extern my_func_ptr_T func;
void do_callback() {
func(1,2);
}
//sample sum function
void sum( int a, int b ) {
printf ("SUM : %d\n", a+b);
}
//main
int main() {
func = sum;
do_callback( );
}
It is important to understand that, keyword extern doesn't define the variable. It is just a variable declaration. No memory is allocated for an extern variable declaration. The func variable is actually defined in 1.c and that is where memory is allocated. You are free to use extern declarations in as many files ( for e.g. 3.c) as you need.
Note: Careful when adding extern to header file as in accepted answer. That makes variable visible, in which ever source file the header in included and so a potential risk of name collisions or unintentional modifications.
You make the variable func known in file 2.c:
// file 2.c
extern my_func_ptr_T func;
void do_callback() {
// i want to do this
func(1,2);
}
By declaring funcas extern you tell the compiler that this particular variable will live in a file / different compilation unit (e.g. 1.o compiled from 1.c, where a variable of the same name and type is declared without the externkeyword), and it does not need to create it when compiling 2.c into 2.o.
This is an example about using function pointers...
#include <stdio.h>
void prev(int x)
{
printf( "%d\n", x-1 );
}
void next(int x)
{
printf( "%d\n", x+1 );
}
typedef void (*my_func_ptr_T)(int);
int main()
{
my_func_ptr_T foo;
foo = &prev;
foo( 5 );
return 0;
}
You can run the code in this DEMO

Resources