PDA

View Full Version : Simple C question




neutrino23
Sep 13, 2010, 03:51 AM
I'm working my way through Learn C on the Mac.

When he discusses variable names he says they should start with either and upper or lower case letter. But when he talks about globals he says you declare a global like this.


int gmyVar;

Does this imply that any name starting with a g is a global? So the following

int goodOne;

declares a global even though I didn't intend that?


Thanks



Cromulent
Sep 13, 2010, 04:27 AM
I'm working my way through Learn C on the Mac.

When he discusses variable names he says they should start with either and upper or lower case letter. But when he talks about globals he says you declare a global like this.


int gmyVar;

Does this imply that any name starting with a g is a global? So the following

int goodOne;

declares a global even though I didn't intend that?


Thanks

No.

It is just a stupid practice by some programmers to indicate that a particular variable is global in scope. Some also indicate the type of a variable by prefixing the variable name with i for instance (for int) or ui (unsigned int). Personally I dislike that approach.

C does not use the name of a variable to indicate its scope nor its type at all. That is purely based on the scope of the variable (whether file scope, external scope or local scope) and the type that it was declared to have.

MorphingDragon
Sep 13, 2010, 05:25 AM
Nvm, just read crom's post.

robvas
Sep 13, 2010, 09:12 AM
One common approach to this is Hungarian notation, it has it's advantages and disadvantages.

http://msdn.microsoft.com/en-us/library/aa260976%28VS.60%29.aspx

Sander
Sep 13, 2010, 10:32 AM
It is just a stupid practice by some programmers to indicate that a particular variable is global in scope.

That sounds like a value judgment. :)

notjustjay
Sep 13, 2010, 10:45 AM
Yup, as others have said, you can call a variable whatever you want. The recommended practice is a suggestion only, and it's designed to help you remember what the variables are while you're coding. It's meant to help make the code more readable, but it's entirely up to you whether to follow a (any) convention or not.

If you end up working as a software developer for a company, you might find that they have existing coding conventions that you are expected to follow. For example, at a company I worked for, class variables always started with c (cString), passed parameters started with p (pString), and globals started with g (gString). Constants were always uppercase to distinguish them from other variables (STRING_LENGTH). And so on.

Find a convention you like -- any one -- and stick with it. Consistency is the key to understandable code.

mags631
Sep 13, 2010, 12:03 PM
One common approach to this is Hungarian notation, it has it's advantages and disadvantages.

http://msdn.microsoft.com/en-us/library/aa260976%28VS.60%29.aspx

From the web-page in question:

1 #include "sy.h"
2 extern int *rgwDic;
3 extern int bsyMac;
4 struct SY *PsySz(char sz[])
6 {
7 char *pch;
8 int cch;
9 struct SY *psy, *PsyCreate();
10 int *pbsy;
11 int cwSz;
12 unsigned wHash=0;
13 pch=sz;
14 while (*pch!=0
15 wHash=(wHash<>11+*pch++;
16 cch=pch-sz;
17 pbsy=&rgbsyHash[(wHash&077777)%cwHash];
18 for (; *pbsy!=0; pbsy = &psy->bsyNext)
19 {
20 char *szSy;
21 szSy= (psy=(struct SY*)&rgwDic[*pbsy])->sz;
22 pch=sz;
23 while (*pch==*szSy++)
24 {
25 if (*pch++==0)
26 return (psy);
27 }
28 }
29 cwSz=0;
30 if (cch>=2)
31 cwSz=(cch-2/sizeof(int)+1;
32 *pbsy=(int *)(psy=PsyCreate(cwSY+cwSz))-rgwDic;
33 Zero((int *)psy,cwSY);
34 bltbyte(sz, psy->sz, cch+1);
35 return(psy);
36 }

This makes my eyes bleed.

PinkyMacGodess
Sep 13, 2010, 12:16 PM
It's a 'convention', an element of style. If you program yourself, you can use what you like but if you program for or with others they very often have directives that you have to follow.

Well, and if a program gets very complicated and long, anything can help get to the end point. Inline comments are something that you should get used to doing also. If not for others, then for yourself...

Documentation is like sex. A little is better than none...

Cromulent
Sep 13, 2010, 12:19 PM
That sounds like a value judgment. :)

And? The vast majority of things on programming sites are going to be based on opinion rather than objective or rational thought processes.

firewood
Sep 13, 2010, 12:41 PM
If I wanted goodOne to be a global, I would name the variable gGoodOne.

notjustjay
Sep 13, 2010, 02:24 PM
This makes my eyes bleed.

BLEH! Me too.

I'd lose the type prefixes on the variables. If I declare x as an int, then it shouldn't be too hard to remember that or look up a few lines in the code -- no need to call it intX or iX. Even better, call it a more descriptive name than x, such as "month" or "numOfWidgets".

I do, however, like the convention of using c and g to prefix variables that are of class member or global scope.

SidBala
Sep 13, 2010, 03:08 PM
You can name a variable anything you want. The name will not determine if it is a global or a local variable.

However, where you define it in the code and what keywords you use along with it will determine what kind of variable it is.

milbournosphere
Sep 13, 2010, 06:15 PM
From the web-page in question:

1 #include "sy.h"
2 extern int *rgwDic;
3 extern int bsyMac;
4 struct SY *PsySz(char sz[])
6 {
7 char *pch;
8 int cch;
9 struct SY *psy, *PsyCreate();
10 int *pbsy;
11 int cwSz;
12 unsigned wHash=0;
13 pch=sz;
14 while (*pch!=0
15 wHash=(wHash<>11+*pch++;
16 cch=pch-sz;
17 pbsy=&rgbsyHash[(wHash&077777)%cwHash];
18 for (; *pbsy!=0; pbsy = &psy->bsyNext)
19 {
20 char *szSy;
21 szSy= (psy=(struct SY*)&rgwDic[*pbsy])->sz;
22 pch=sz;
23 while (*pch==*szSy++)
24 {
25 if (*pch++==0)
26 return (psy);
27 }
28 }
29 cwSz=0;
30 if (cch>=2)
31 cwSz=(cch-2/sizeof(int)+1;
32 *pbsy=(int *)(psy=PsyCreate(cwSY+cwSz))-rgwDic;
33 Zero((int *)psy,cwSY);
34 bltbyte(sz, psy->sz, cch+1);
35 return(psy);
36 }

This makes my eyes bleed.

I hope to god that code was computer generated by some form of design program...otherwise that programmer needs to be fired.

iggypod
Sep 13, 2010, 06:20 PM
One common approach to this is Hungarian notation, it has it's advantages and disadvantages.

http://msdn.microsoft.com/en-us/library/aa260976%28VS.60%29.aspx

^Agreed. The last software company I worked for required we coded via this.

MorphingDragon
Sep 13, 2010, 06:22 PM
I hope to god that code was computer generated by some form of design program...otherwise that programmer needs to be fired.

A, Its called a contrived example, and most if not all tutorials, books and examples suffer from it.

B, its visual basic. Its near impossible to get nice looking code with VB.

Hansr
Sep 13, 2010, 06:26 PM
A, Its called a contrived example, and most if not all tutorials, books and examples suffer from it.

B, its visual basic. Its near impossible to get nice looking code with VB.

That's not VB. VB can be made to look nice.

MorphingDragon
Sep 13, 2010, 06:28 PM
That's not VB. VB can be made to look nice.

Oops, read Studio as Basic.

zimv20
Sep 13, 2010, 10:06 PM
prefixing variable names is archaic. with the widespread use of IDEs like Eclipse, you have tools that let you easily find out the type and scope (and comments) of anything.

best practice, imho, remains using descriptive names, useful comments, and whitespace for readability.

neutrino23
Sep 13, 2010, 10:40 PM
Thanks. Now I get it. In the first reading I thought that the "g" prefix had the effect of making the variable global. Now I see it is more subtle. If you declare the variable like this:

int MyVar;
int main (int argc, const char * argv[]) {
MyVar = 10;

then it is global.

If you declare it like this


int main (int argc, const char * argv[]) {
int MyVar;
MyVar = 10;

Then it is not global. Kind of subtle.

I've worked a lot with some machine language, DEC Macro-11, Fortran-11 and Forth and Applescript. Now I'm going to make the run through C, Objective C and Cocoa.

Sander
Sep 14, 2010, 03:08 AM
Kind of subtle.

Just remember that a variable is available from the point it's declared to the end of the current scope. If it's declared outside of any function body, the "current scope" extends to the end of the compilation unit (over-simplified: to the end of the file).

gnasher729
Sep 14, 2010, 04:26 AM
prefixing variable names is archaic. with the widespread use of IDEs like Eclipse, you have tools that let you easily find out the type and scope (and comments) of anything.

It's nice to just look at source code and know what it is doing without having to use any tools. Especially nice if you know something is wrong in a few thousand lines of code and you don't know where. I'd say using clear variable names that don't require me to use any tools speed up reading source code by a factor ten at least.


Just remember that a variable is available from the point it's declared to the end of the current scope. If it's declared outside of any function body, the "current scope" extends to the end of the compilation unit (over-simplified: to the end of the file).

Scope is not the only thing, lifetime is another. extern and static variables exist from the moment the program starts to the moment it finishes. Local variables in a function exist only while the function is running; and they can exist multiple times if a function is recursive (but you wouldn't usually care about that) or is used in multiple threads. So returning the address of a local variable from a function is asking for trouble - because the variable stops existing when the function returns. extern and static variables exist only once which is important for multithreaded code - if you have a global variable "int i" and two threads execute a loop "for (i = 0; i < 1000; ++i) ..." then surprising things will happen.

Sydde
Sep 14, 2010, 06:19 PM
Just remember that a variable is available from the point it's declared to the end of the current scope. If it's declared outside of any function body, the "current scope" extends to the end of the compilation unit (over-simplified: to the end of the file).

To expand, if you declare a variable in a .c or .m file, the variable will only be available to the code that is in that file. It will not be strictly global unless it is declared in a .h file, and then only global within other .c or .m files that import that header (directly or indirectly).

firewood
Sep 14, 2010, 08:35 PM
prefixing variable names is archaic. with the widespread use of IDEs like Eclipse, you have tools that let you easily find out the type and scope (and comments) of anything.

Some of us still prefer hand tools, home grown veggies, and using vi in a monochrome terminal window to code in C and ARM assembly language (on our MacBook Pros for our iPhone 4's).

zimv20
Sep 14, 2010, 09:04 PM
Some of us still prefer hand tools, home grown veggies, and using vi in a monochrome terminal window to code in C and ARM assembly language (on our MacBook Pros for our iPhone 4's).

fair enough. i still use vi, and pre-IDE, i still hated hungarian notation.

gnasher729
Sep 15, 2010, 01:43 AM
To expand, if you declare a variable in a .c or .m file, the variable will only be available to the code that is in that file. It will not be strictly global unless it is declared in a .h file, and then only global within other .c or .m files that import that header (directly or indirectly).

Danger ahead! A variable declared as global in a file _is_ global, whether it is in a header file or not, and whether it is intended or not. If you (or someone else) tries to declare another variable with the same name in another file, then you don't get two variables, you have _one_ variable. Great source of bugs.

If you want a variable with application life time, and you don't want it accessed from another file by accident, make it static.

Sander
Sep 15, 2010, 07:34 AM
Danger ahead! A variable declared as global in a file _is_ global, whether it is in a header file or not, and whether it is intended or not. If you (or someone else) tries to declare another variable with the same name in another file, then you don't get two variables, you have _one_ variable. Great source of bugs.

I think the linker will normally catch this and complain about duplicate defined symbols.