473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Global variable conflict.

I have two files like given below:

/* File 1 */
#include <stdio.h>
#include <stdlib.h>

void tf(void);

int i;

int main(void)
{
i = 10;
tf();

return EXIT_SUCCESS;
}

/* End File 1 */

/* File 2 */
#include <stdio.h>

int i = 48;
void tf(void)
{
printf("%d\n", i);
}
/* End File 2 */
The observed behaviour is that the "printf" always prints the value of i as 10.

Two questions:

1 (for clc). Is the above code standards (ANSI) compliant?
2 (for cup). How do I get the linker to "see" the i with value 48 on Solaris?

Thanks and regards,
Shinu
Nov 13 '05 #1
3 3184
In 'comp.lang.c', su**************@yahoo.co.uk (Shinu George) wrote:
I have two files like given below:

/* File 1 */
#include <stdio.h>
#include <stdlib.h>

void tf(void);

int i;

int main(void)
{
i = 10;
tf();

return EXIT_SUCCESS;
}

/* End File 1 */

/* File 2 */
#include <stdio.h>

int i = 48;
You can't have two global scope variables with the same name in the same
project. Your linker should warn you about that.
void tf(void)
{
printf("%d\n", i);
}
/* End File 2 */
The observed behaviour is that the "printf" always prints the value of i
as 10.
Won't link at home.
Two questions:

1 (for clc). Is the above code standards (ANSI) compliant?


No.

If you insist to have these global scope variables, you must reduce their
scope to the file with 'static'.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #2
Shinu George wrote:

[snip]
cat file2.h #ifndef GUARD_FILE2_H
#define GUARD_FILE2_H 1
#include <stdio.h>
#include "file2.h"

extern int i;

void tf(void);

#endif//GUARD_FILE2_H
cat file2.c /* File 2 */
#include "file2.h"

int i = 48;

void tf(void) {
printf("%d\n", i);
}

/* End File 2 */
cat file1.c /* File 1 */
#include "file2.h"
#include <stdlib.h>

int main(int argc, char* argv[]) {
tf();
i = 10;
tf();

return EXIT_SUCCESS;
}

/* End File 1 */
gcc -Wall -std=c99 -pedantic -O2 -o file file1.c file2.c
./file

48
10

Nov 13 '05 #3
Alan Balmer <al******@att.net> wrote in message news:<8u********************************@4ax.com>. ..
On 22 Jul 2003 12:21:13 -0700, su**************@yahoo.co.uk (Shinu
George) wrote:
I have two files like given below:

/* File 1 */
#include <stdio.h>
#include <stdlib.h>

void tf(void);

int i;

int main(void)
{
i = 10;
tf();

return EXIT_SUCCESS;
}

/* End File 1 */

/* File 2 */
#include <stdio.h>

int i = 48;
void tf(void)
{
printf("%d\n", i);
}
/* End File 2 */
The observed behaviour is that the "printf" always prints the value of i as 10.


Yes. The system initializes the i in file 2 at load time. At
execution, main overwrites the initialization.

Perhaps if you explained what you're trying to accomplish, someone
could suggest a better approach.


Accomplish? Nothing. This question was asked of me at an interview
(with a large database company whose name begins with "O". In
India.). Essentially they asked me the second question with reference
to the linker. Confused the hell out of me.

Didn't get through the interview, anyway.

Thanks,
Shinu

Two questions:

1 (for clc). Is the above code standards (ANSI) compliant?
2 (for cup). How do I get the linker to "see" the i with value 48 on Solaris?

Thanks and regards,
Shinu

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
10
by: a.farhadi | last post by:
hi guys, I want to eval some javascript code at a function. and my code contains some variable and function definitions. my problem is that the code is evaluated in function scope not global...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
16
by: Vadim Biktashev | last post by:
Hello all I would like to give a certain name to a certain global variable. Unfortunately, this name is already used in math.h for a mathematical function. Worse, I do need to use maths library...
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
2
by: quickcur | last post by:
Hi, I used to have different c code in different files and compiled as different programs. For example, I have proc1.c: int x1, x2; void work1()
1
by: quickcur | last post by:
Hi, I used to have different c code in different files and compiled as different programs. For example, I have proc1.c: int x1, x2; void work1()
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
7
patjones
by: patjones | last post by:
Good morning all - This is a pretty simple situation to describe. I have my database set up so that users type in a name and password when it first opens up. Provided the combination is correct,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.