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

  • Home
  • SEARCH
  • 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 9090719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:26:27+00:00 2026-06-16T22:26:27+00:00

why can’t my normals_since function see my global variable leaps?? I can’t believe that

  • 0

why can’t my normals_since function see my global variable leaps??
I can’t believe that variables declared in main aren’t available in the whole program is that what encapsulation or hiding or something means? is it accessible in some secret way like main.z or _main_z ??
my gcc error>>

yrs_since.c: In function ‘normals_since’:
yrs_since.c:40:9: error: ‘leaps’ undeclared (first use in this function)
yrs_since.c:40:9: note: each undeclared identifier is reported only once </p>
for each function it appears in

possible answer
looks like if I want all functions to see the vars, I have to move 
int z; //place holder
int leaps;
int normals;

outside of main and declare them up at the top after the #defines

#include stdio.h>
#include stdlib.h>
#define START_GREG 1582
int yrs_since(int year); //type-declare the function
int leaps_since(int years);
int normals_since(int years);

int main(int argc, char* argv[]){
    int year = 1599; //local var
    int z; //place holder
    int leaps;
    int normals;
    z = yrs_since(year); //call the function
    leaps = leaps_since(z); //leap years move the doomsday fwd by 2 days
    normals= normals_since(z); //normal years it adjusts one day
    printf("blah blah %d,", z);//print the result
    printf("leap years since 1582:-->> %d  <<", leaps);
    printf("normal years since 1582:-->> %d  <<", normals);
    return EXIT_SUCCESS;
}
int yrs_since(year){
    int x;
    x=year-START_GREG;
    return x;
};
int leaps_since (years){
    return years/4;
};

int normals_since(years){
    int x;
    x=years-leaps;
    return x;
};
  • 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-16T22:26:28+00:00Added an answer on June 16, 2026 at 10:26 pm

    Right, as you have found out, variables INSIDE a function are only visible to that function. main is a function just like any other function, it is not treated in any special way.

    Global variables are declared OUTSIDE of functions (but it’s generally good advice to avoid global functions, in general.

    The solution, if you want to avoid global variables, is to pass the variable from main into the function using the variable.

    For example:

    int normals_since(int years, int leaps){
        int x;
        x=years-leaps;
        return x;
    };
    

    Note that I added “int” to the years variable. Whilst old-style C is still allowed in some compilers, it’s definitely recommended to use ANSI standard (add -ansi -strict -Wall -std=c99 to your gcc command line to give you warnings for “things you may have done wrong” and errors for not following ANSI standard)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can I get the value of a variable declared in a procedure then use
Can I be sure about the order in a Python dictionary? The function op.GetTangent(id)
Can I call select before recv_from on a socket that is blocking?
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can I write this with less code, also is .click(function preferred for mobile or
Can anyone tell me how an IDE like NetBeans or any for that matter
can you show me how to assign css properties to the button that is
Can I somehow see the types and size of the contents of a tuple?

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.