Suppose there are two codes foo1.c and foo2.c as below and here the task is to link foo1.c and foo2.c which have same variable name x but different data type i.e int in foo1.c and double in foo2.c.
Note that none of the variables is declared as extern.
What do you expect to be the output of the following command with given two programs?
$ gcc -o myprog foo1.c foo2.c $ ./myprog
x = 0 y = 1078263808
Explanation of Output: Output of the program looks unpredictable but reason is: In foo1.c, there are two variables x and y each having 4 bytes(total 8 bytes). In foo2.c there is x variable as double.
While execution, x in foo1 (4 bytes) is replaced by x in foo2 (8 bytes). Ultimately memory of x and y (total 8 bytes) in foo1 is overwritten by x in foo2 (8 bytes).
1078263808 value is floating point representation of 42 in double.
NOTE : x and y addresses in foo1.c are successive.
Related Article : External and Internal Linkages in C
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between fundamental data types and derived data types
- Difference between Static variables and Register variables in C
- Different ways to Initialize all members of an array to the same value in C
- How to print range of basic data types without any library function and constant in C?
- What are the data types for which it is not possible to create an array?
- Data Types in C
- C | Data Types | Question 1
- C | Data Types | Question 2
- C | Data Types | Question 5
- C | Data Types | Question 6
- C | Data Types | Question 9
- Interesting facts about data-types and modifiers in C/C++
- What happen when we exceed valid range of built-in data types in C++?
- Uninitialized primitive data types in C/C++
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- C Program to list all files and sub-directories in a directory
- C Program to merge contents of two files into a third file
- C program to compare two files and report mismatches
- Header files in C/C++ with Examples
- How will you show memory representation of C variables?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.