473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(D MP_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 1853
In article <11************ **********@f14g 2000cwb.googleg roups.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(D MP_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(D MP_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.c om, 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_Keit h) 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(D MP_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 "technicall y correct" English; but since when was rock & roll "technicall y 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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #7

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

Similar topics

3
1802
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. The user is allowed to specify one to three data fields from a list and up to three text fields to use as the pattern for creating chapter names: <ChapterNameDefaults>
9
6904
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 titles, I found a total of about five pages that covered js files. For all practical purposes, these pages said, "You can use js files. But we won't tell you how." Therefore, I hope someone can answer a few questions about js files... 1....
12
3947
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
9905
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
64801
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 #define TIFF_LITTLEENDIAN 0x4949 C# supports #define but not adding a value to it.
2
1017
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 myStruct.myFirstvalie, MyStruct.SecondValue, how should I do this here in asp.net? I guess I should use class, can you give me example to handle it.?
16
3284
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 that I'm not very experienced with pointers, pointers to pointers and the like and I got 4 compiler warnings + I don't completely understand how to build this "compact matrix" (see later).
3
1928
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'); //db connection parameters class DBRegion{ var $db;
5
3234
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 #define with const. example: 2 header file StringGrid1.h StringGrid2.h
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.