Thread: How to use variable in main function that is declare in other function ?

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    66

    How to use variable in main function that is declare in other function ?

    If I want to use variable in main function that is declare in another function so How to do that, for example x is declare in function foo but I want to use it in main function so I am making it extern but when I did it show the error

    How to use variable in main function that is declare in other function ?
    Code:
    #include<stdio.h>
    
    void foo ()
    {
      extern int x = 2;
    }
     
    int main()
    {
     
        x++;
        
        printf( "x = %d", x);
     
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    This is what return values and output parameters (i.e., a pointer parameter that points to an object in the caller to which the function can write the result) are for.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by laserlight View Post
    This is what return values and output parameters (i.e., a pointer parameter that points to an object in the caller to which the function can write the result) are for.
    I do not understand your answer program should supposed to give result 3 but it's give error so how to get result 3

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Quote Originally Posted by Player777
    I do not understand your answer program should supposed to give result 3 but it's give error so how to get result 3
    Try this program then:
    Code:
    #include <stdio.h>
    
    int foo(void)
    {
        int x = 2; // we don't really need this, but as an example...
        return x;
    }
    
    int main(void)
    {
        int x = foo();
        x++;
        printf("x = %d\n", x);
        return 0;
    }
    or this one:
    Code:
    #include <stdio.h>
    
    void foo(int *result)
    {
        *result = 2;
    }
    
    int main(void)
    {
        int x;
        foo(&x);
        x++;
        printf("x = %d\n", x);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by laserlight View Post
    Try this program then:
    Thank you ! The program gives me the output that I wanted But my understanding has been failed to understand the extern keywords

    I though if any variable is declared in function and If we want to use that variable in main function we can do that by making it extern variable. I tried to do that but I couldn't do this in my program

    your example proves that extern is used in two different source files not in same source file.
    Last edited by Player777; 04-14-2020 at 11:47 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    The extern keyword is concerned with linkage. Linkage is about deciding, given an identifer such as a variable name: does this identifer refer to the same object or function as that identifier? Where "this" and "that" could be in the same translation unit (internal linkage) or in separate translation units (external linkage). A "translation unit" means a source file and its included headers.

    A local variable has no linkage: it is clear from the local scope that "this" or "that" local variable name refers to exactly which object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by laserlight View Post
    The extern keyword is concerned with linkage. Linkage is about deciding, given an identifer such as a variable name: does this identifer refer to the same object or function as that identifier? Where "this" and "that" could be in the same translation unit (internal linkage) or in separate translation units (external linkage). A "translation unit" means a source file and its included headers.

    A local variable has no linkage: it is clear from the local scope that "this" or "that" local variable name refers to exactly which object.
    Can the extern be used only in the different source file or can it be used in the same source file?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    You can use it in the same source file, but why would you? The variable has already been declared.

    Frankly, I don't think you should dwell so much on this topic. In general, global state makes it more difficult to reason about your program, hence global variables should be sparingly used as they result in global state. Therefore, dedicating so much time to learn about them when you could learn about other things that are more commonly used is not good time allocation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-11-2015, 05:20 AM
  2. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  3. Replies: 2
    Last Post: 09-19-2012, 11:32 AM
  4. How to declare a global variable in a function?
    By exclusive in forum C Programming
    Replies: 5
    Last Post: 11-06-2011, 04:28 AM
  5. How to declare a global variable in Main()
    By vnrabbit in forum C Programming
    Replies: 2
    Last Post: 06-20-2002, 12:59 PM

Tags for this Thread