Thread: Program show result without extern

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    Program show result without extern

    I am wondering how program is showing correct output without external storage external class in source files because I have two source file

    main.c
    Code:
        #include <stdio.h>  
      void fun ();
      
      int main ()
      
      {
    	  int x = 1;
    	  
    	  fun (x);
    	  
          return 0;
      }
    FileA.c

    Code:
      #include <stdio.h>
      void fun ( int  y)
      {
    	  y++;
    	  
    	  printf ("x = %d \n", y);
    	  
      }
    output : 2

    variable x is declared in main.c source file and this variable is being passed to function fun which is in source file FileA.c

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because your function prototype on line 2 of main.c is extern by default.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by Salem View Post
    Because your function prototype on line 2 of main.c is extern by default.
    I have declared variable x in main.c file. I want to access this variable in another file FileA.c. That's why i used extern storage class

    Code:
    #include <stdio.h> 
      extern int x;  
    
    int main ()
    
    {
         x = 1;
    
        fun (x);
    
        return 0;
    }
    FileA.c
    Code:
     
    
      #include <stdio.h>
      
     void fun ( )
      {
    	  x++;
    	  
    	  printf ("x = %d \n", x);
    	  
      }
    I don't know why compiler show error error: 'x' undeclared (first use in this function)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read all your other 'extern' threads again!
    extern function
    extern variable is not working as expected
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by Salem View Post
    Read all your other 'extern' threads again!
    extern function
    extern variable is not working as expected
    I am surprised because I am accessing a variable x that is declared in another source file file1.c without extern

    main.c

    Code:
     #include <stdio.h>
    #include "file1.h"
    
    int main() {
      x = 6;
      printf("%d\n", x);
    
      return 0;
    }
    header
    Code:
    int x;
    file1.c
    Code:
    int x;
    output : 6
    Does compiler makes variable as extern variable by default ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, an object name declared at file scope has external linkage by default.
    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. C Program compilation result result still error
    By Zach Watterson in forum C Programming
    Replies: 3
    Last Post: 03-06-2021, 10:35 AM
  2. C ++ Program compilation result result still error
    By Zach Watterson in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2021, 09:27 AM
  3. my program show inf. how???
    By ttzz in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2020, 04:23 AM
  4. not show my program on taskbar
    By ReXXuSS in forum C Programming
    Replies: 12
    Last Post: 08-19-2006, 03:27 AM
  5. Program doesn't show up
    By Krasimir11 in forum C Programming
    Replies: 7
    Last Post: 01-05-2006, 06:21 PM

Tags for this Thread