473,320 Members | 1,876 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,320 software developers and data experts.

Using define with variable values

The following line of code works, however, since my professor is a real
purist of c, I would like to know if this code is valid (is it good
code or a piece of crap?):
#define DMP_FILE argv[argc-1]

This would be use to do something like this:
void main(int argc,char *argv[])
{
FILE *p2file=fopen(DMP_FILE,"w");
}

Is this a macro? Cause in the examples of macro I've studied, variables
were involved in the macro itself. This would be like a regular define,
but with variable values.

Nov 14 '05 #1
6 1824
In article <11**********************@f14g2000cwb.googlegroups .com>,
<ms******@gmail.com> wrote:
The following line of code works, however, since my professor is a real
purist of c, I would like to know if this code is valid (is it good
code or a piece of crap?):
#define DMP_FILE argv[argc-1]
It is completely legitimate C. There are two kinds of #define,
one "object like" and the other "function like". Either one is
permitted to include a wide range of syntax, not limited to
constants or variables or simple expressions. It is valid C to
use something like #define BEGIN {
or to otherwise appear to distort the syntax of C.

Whether it is good -style- is a different question.

This would be use to do something like this:
void main(int argc,char *argv[])
{
FILE *p2file=fopen(DMP_FILE,"w");
}


What happens if no arguments were passed? Are you going to
overwrite the binary executable itself?
--
Warning: potentially contains traces of nuts.
Nov 14 '05 #2
ms******@gmail.com wrote:
The following line of code works, however, since my professor is a real
purist of c, I would like to know if this code is valid (is it good
code or a piece of crap?):
#define DMP_FILE argv[argc-1]

This would be use to do something like this:
void main(int argc,char *argv[])
{
FILE *p2file=fopen(DMP_FILE,"w");
}

Is this a macro? Cause in the examples of macro I've studied, variables
were involved in the macro itself. This would be like a regular define,
but with variable values.


The macro definition is valid, and the expansion is
what you intended. However, it's "fragile" because it
relies on the particular names `argc' and `argv' -- and
although these are very often used as the names of the
arguments of main(), other names are permissible:

int main(int zaphod, char **just_this_guy) {
time_t argv = time(NULL);
clock_t argc = clock();
...

is perfectly valid, but would play havoc with your macro.
If you're sure that no one who uses your macro will ever
do anything so stupidly perverse, fine -- but it's usually
best not to "build in" such dependencies. Your macro will
work, but it's probably not Best Practice.

However, your purist professor may not even bother with
the possible shortcomings of the macro, since you've given
him two count them two bona-fide errors to fixate on. Walter
Roberson already pointed out one of them; you may discover
the other by a careful examination of the differences between
your main() and the main() in my reply.

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 14 '05 #3
The fopen line was just an example, the actual use of the macro is
diferent.
If no arguments were passed, the actual program will never reach the
dmpfile line.

Nov 14 '05 #4
ms******@gmail.com writes:
The fopen line was just an example, the actual use of the macro is
diferent. If no arguments were passed, the actual program will
never reach the dmpfile line.


Just an example of what? Please provide some context when posting a
followup.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Also, go back to your original program and ask yourself what the
main() function returns.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Groovy hepcat ms******@gmail.com was jivin' on 29 Apr 2005 21:23:41
-0700 in comp.lang.c.
Using define with variable values's a cool scene! Dig it!
The following line of code works, however, since my professor is a real
purist of c, I would like to know if this code is valid (is it good
code or a piece of crap?):
Piece of crap. (Well, you did ask.)
#define DMP_FILE argv[argc-1]

This would be use to do something like this:
void main(int argc,char *argv[])
It has been stated time and time again in this newsgroup that main()
must return an int, not void. Inspite of us saying this until we're
blue in the face, you still define main() with a return type of void.
Not a good start!
Of course, as well as making main() return an int, you should
actually have it return something. Portable return value for main()
are 0, EXIT_SUCCESS and EXIT_FAILURE (the latter two being macros
defined in stdlib.h).

int main(int argc, char **argv){
FILE *p2file=fopen(DMP_FILE,"w");
There are a couple of problems here. Firstly, your macro DMP_FILE
assumes that argc > 0. This may not be always be the case. You
shouldn't take it for granted.
Secondly, you have not included stio.h, and so you don't have a
valid declaration of fopen() in scope.

return 0;}

Is this a macro?
No. This (the thing in the code directly above) is a function. Its
name is main(). It uses a macro, whose name is DMP_FILE, which is
defined above it.
Cause in the examples of macro I've studied, variables
were involved in the macro itself. This would be like a regular define,
but with variable values.


Macros do not contain variables. However, function-like macros may
have parameters. This allows the user to pass values (from variables
or otherwise) to macros.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #6
Groovy hepcat Peter "Shaggy" Haywood was jivin' on Sun, 01 May 2005
02:49:17 GMT in comp.lang.c.
Re: Using define with variable values's a cool scene! Dig it!
Groovy hepcat ms******@gmail.com was jivin' on 29 Apr 2005 21:23:41
-0700 in comp.lang.c.
Using define with variable values's a cool scene! Dig it!

Secondly, you have not included stio.h, and so you don't have a

^^^^^^
Sorry! Typo. Should read "stdio.h".

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #7

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

Similar topics

3
by: axial | last post by:
Is there a way to define a variable by resolving other variables? I'm trying to build an "uber-variable" from user-selected components. I have control over the XML and can change it as needed. ...
9
by: Charley Kyd | last post by:
I'm a newbie who needs advice about how to use external files of JavaScript code. I spent an hour this afternoon browsing through JavaScript books at the local book store. In about 15 different...
12
by: bollod | last post by:
Is there any benefit to declaring a variable: x = (1<<12); instead of: x = 4096; (besides the geek appeal of course ;-) )
42
by: Prashanth Badabagni | last post by:
Hi, Can any body tell me how to print "hello,world" with out using semicolon Thanks in advance .. Bye Prashanth Badabagni
1
by: Scott | last post by:
What is the equivalent way to #define a variable and value in C#? I am porting a program with multiple #define's as such: #define TIFF_VERSION 42 #define TIFF_BIGENDIAN 0x4d4d...
2
by: JIM.H. | last post by:
Hello, In my C# asp.net web application, I need to define a few variable that is reachable from all pages. If it was C I would define it in the .h file as struct and use it in the...
16
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
3
by: mich dobelman | last post by:
I cannot reference the values in side of config.php $dbhost,$dbuser ,$dbpasswd ,$dbname why? <?php require_once('includes/db.php'); //db connection functions require_once('config.php'); ...
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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: 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.