473,405 Members | 2,287 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,405 software developers and data experts.

Problems with basics..

hi All,
This is my first post.. I am trying to understand how #include
works... I wrote a simple multi file project like this in C::B.
///// MyFunc.h ///////////

#ifndef MYFUNC_H_INCLUDED
#define MYFUNC_H_INCLUDED

int Val;
void PrintVal();

#endif // MYFUNC_H_INCLUDED
////////////// MyFunc.c ///////////////
#include <stdio.h>

#include "myfunc.h"

void PrintVal()
{
printf("%d",Val);
}

/////////////////// main.c //////////////////
#include <stdio.h>
#include <stdlib.h>

#include "myfunc.h"

int main()
{
Val = 30;
PrintVal();
return 0;
}

I "think" #include "xyz.pqr" dumbly replaces the line with contents of
file xyz.pqr.. Then it compiles and links.
With this "assumption" there should be a 'int Val' local to both
MyFunc.c and main.c. So whatever modifications i do for 'Val' should
not be reflected in MyFunc.c and PrintVal should print garbage. To my
surprise it prints the value of 'Val' with exact value i have
modified.
Also tell me how "extern" works exactly.. For me, in the above
program, i should declare void PrintVal() as extern i suppose so that
main.c understands it properly.
Thanks in advance.

Mar 24 '07 #1
2 1309
"iCoder" <iF****@gmail.comschrieb im Newsbeitrag
news:11**********************@d57g2000hsg.googlegr oups.com...
hi All,
This is my first post.. I am trying to understand how #include
works... I wrote a simple multi file project like this in C::B.
///// MyFunc.h ///////////

#ifndef MYFUNC_H_INCLUDED
#define MYFUNC_H_INCLUDED

int Val;
void PrintVal();

#endif // MYFUNC_H_INCLUDED
////////////// MyFunc.c ///////////////
#include <stdio.h>

#include "myfunc.h"

void PrintVal()
{
printf("%d",Val);
}

/////////////////// main.c //////////////////
#include <stdio.h>
#include <stdlib.h>

#include "myfunc.h"

int main()
{
Val = 30;
PrintVal();
return 0;
}

I "think" #include "xyz.pqr" dumbly replaces the line with contents of
file xyz.pqr.. Then it compiles and links.
With this "assumption" there should be a 'int Val' local to both
MyFunc.c and main.c. So whatever modifications i do for 'Val' should
No, you should have ended up with 2 global ints called Val and your Linker
should have warned about it.
not be reflected in MyFunc.c and PrintVal should print garbage. To my
surprise it prints the value of 'Val' with exact value i have
modified.
Also tell me how "extern" works exactly.. For me, in the above
program, i should declare void PrintVal() as extern i suppose so that
main.c understands it properly.
extern in Val; in the header
int Val; in either of the the c files but not in both.

You should never ever define a variable in a header, only declare it there
using that extern specifier (telling the compiler: "there is an int called
Val somewhere, trust me") and make sure it is defined exaclty once in some c
file. If it is not defined, the linker will complain, if it is defined more
than once the linker may warn.

Bye, Jojo
Mar 24 '07 #2
Joachim Schmitz wrote, On 24/03/07 14:04:
"iCoder" <iF****@gmail.comschrieb im Newsbeitrag
news:11**********************@d57g2000hsg.googlegr oups.com...
>hi All,
This is my first post.. I am trying to understand how #include
works... I wrote a simple multi file project like this in C::B.

///// MyFunc.h ///////////

#ifndef MYFUNC_H_INCLUDED
#define MYFUNC_H_INCLUDED

int Val;
void PrintVal();

#endif // MYFUNC_H_INCLUDED

////////////// MyFunc.c ///////////////
#include <stdio.h>

#include "myfunc.h"

void PrintVal()
{
printf("%d",Val);
}

/////////////////// main.c //////////////////
#include <stdio.h>
#include <stdlib.h>

#include "myfunc.h"

int main()
{
Val = 30;
PrintVal();
return 0;
}

I "think" #include "xyz.pqr" dumbly replaces the line with contents of
file xyz.pqr.. Then it compiles and links.
With this "assumption" there should be a 'int Val' local to both
MyFunc.c and main.c. So whatever modifications i do for 'Val' should
No, you should have ended up with 2 global ints called Val and your Linker
should have warned about it.
There is no requirement for the linker to warn about it since it is
undefined behaviour. A common extension is for the linker to merge such
objects into a single object when no initialisation is specified. Some
systems will produce an error and some can be made to produce an error,
but not all systems can be made to error on it.
>not be reflected in MyFunc.c and PrintVal should print garbage. To my
surprise it prints the value of 'Val' with exact value i have
modified.
Also tell me how "extern" works exactly.. For me, in the above
program, i should declare void PrintVal() as extern i suppose so that
main.c understands it properly.

extern in Val; in the header
int Val; in either of the the c files but not in both.

You should never ever define a variable in a header, only declare it there
using that extern specifier (telling the compiler: "there is an int called
Val somewhere, trust me") and make sure it is defined exaclty once in some c
file.
This is good advice.
If it is not defined, the linker will complain, if it is defined more
than once the linker may warn.
Note the may, there are common systems that do and common systems that
by default do not.
--
Flash Gordon
Mar 24 '07 #3

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

Similar topics

11
by: lawrence | last post by:
I asked a lot of questions in May about how to organize OO code. I didn't get great answers here, but someone here suggested that I look the Eclipse library, which was a good tip. Looking at its...
4
by: nib | last post by:
What kind of problems is visual basic best at solving?
5
by: Doc | last post by:
I bought the Complete Idiot's Guide to Creating a Web Page to teach myself HTML & website creation but while going through the learning curve want to get a page up and running. So, I also got...
1
by: manish | last post by:
Hi, I am a fresher in the programming field i.e although I have done programming at the basic level but at professional level I am very new and I am facing many problems. These probllems are...
43
by: J.K. Becker | last post by:
Hi there, I am trying to multiply doubles with floats (actually I tried every possible combination by now) and it never works (well, it does something but it is always wrong). I have no idea...
13
by: Roy Gourgi | last post by:
Hi, How do I invoke the random number generator that was suggested by a few people. Ideally, what I would like to do is to instantiate the random no. generator with a seed value that does not...
14
by: chitu | last post by:
Hi pals, Hope u all of r intersted in C programming.Let start from d basics.
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
3
Mague
by: Mague | last post by:
Hey, I need help to some how open a exe file with Microsoft Visual basics. You mite think im trying to steal someone elses project but im not i wont to no how they did it and upgrade it if...
0
by: babu8880 | last post by:
www.convensysglobal.com/elearn You can login and attend courses and similar resources ask your doubts from experienced engineers in iphone and Objective C Domain You can attend live classes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.