Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7870825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:43:41+00:00 2026-06-03T01:43:41+00:00

I am working in C, and have some variables that I don’t want to

  • 0

I am working in C, and have some variables that I don’t want to be global, but I do want to have get and set methods for them that can be accessed “Globaly” outside of the file. I am used to doing this in Java, but C is very different in this manner. Basically I am looking for something that follows this pseudo Code, but I have not been able to find anywhere with examples that I might look at.

main.c
#include data.h
set(b);

datalog.c
#include data.h
get(b);

data.c
private int c;
set(b){
    c = b;
}
get(c){
    return c;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:43:42+00:00Added an answer on June 3, 2026 at 1:43 am

    You make the variable static. When a global variable is made static, its scope is restricted to the current file.

    An example is as follows:

    Filename: main.c

    #include <stdio.h>
    
    #include "header.h"
    
    extern int get();
    extern void set(int);
    
    int main()
    {
        set(10);
        printf("value = %d \n", get());   
        set(20);
        printf("value = %d \n", get());   
        set(30);
        printf("value = %d \n", get());   
        set(40);
        printf("value = %d \n", get());   
        return 0;
    }
    

    Filename: header.h

    #include <stdio.h>
    
    int get(void);
    void set(int);
    

    Filename: header.c

    #include "header.h"
    
    static int value = 0;
    
    int get(void)
    {
        return value;
    }
    
    void set(int new_value)
    {
        value = new_value;
    }
    

    Output:

    $ gcc -Wall -o main main.c header.h header.c 
    $ ./main 
    value = 10 
    value = 20 
    value = 30 
    value = 40 
    $ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some working Javascript code that generates an RDF/XML document using variables picked
I have some code (which I cannot change) that I need to get working
I have some code I am working on that worked just fine until I
I have some UIPopoverController that were working fine in 4.3/5.0 iOS iPad. Now, with
I have some issues with a PHP file that is not working properly. The
I have one AlertDialog which is working fine.I have set some background images to
I'm working on a Rails application where I have some a set of two
I want to display a progress bar while doing some work, but that would
We have some SSRS reports that are failing when two of them are executed
I'm working on some C# code where I have several files that use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.