mysterious #define


 
Thread Tools Search this Thread
Top Forums Programming mysterious #define
# 1  
Old 05-25-2007
mysterious #define

in the header file orville.h, outside of the #ifdef #endif , there is the following

#define JOB_CONTROL /* support job-control */

As you can see, the JOB_CONTROL macro has no value associated with it. Here is what I go when I ran grep on the entire source code.

[cda@localhost orville-write-2.55]$ grep -iR JOB_CONTROL ~/orville-write-2.55
/home/cda/orville-write-2.55/orville.h:#define JOB_CONTROL /* support job-control */
/home/cda/orville-write-2.55/wrt_sig.c:#ifdef JOB_CONTROL
/home/cda/orville-write-2.55/wrt_sig.c:#endif /*JOB_CONTROL*/
/home/cda/orville-write-2.55/wrt_sig.c:#ifdef JOB_CONTROL
/home/cda/orville-write-2.55/wrt_sig.c:#endif /*JOB_CONTROL*/
/home/cda/orville-write-2.55/wrt_sig.c:#ifdef JOB_CONTROL
/home/cda/orville-write-2.55/wrt_sig.c:#endif /*JOB_CONTROL*/

in, wrt_sig.c, there is only stuff like this
/* SIGINIT -- Set up the signal handler routines.
*/

void siginit()
{
signal(SIGTERM,(RETSIGTYPE (*)())intr);
signal(SIGINT,(RETSIGTYPE (*)())intr);
signal(SIGHUP,(RETSIGTYPE (*)())intr);
#ifdef JOB_CONTROL
signal(SIGTSTP,(RETSIGTYPE (*)())susp);
#endif /*JOB_CONTROL*/

}

/* SIGOFF -- Turn off all signals the signal handler routines.
*/

void sigoff()
{
signal(SIGINT,SIG_IGN);
signal(SIGQUIT,SIG_IGN);
signal(SIGTERM,SIG_IGN);
#ifdef JOB_CONTROL
signal(SIGTSTP,SIG_IGN);
#endif /*JOB_CONTROL*/
}

So does the OS just assign some value to JOB_CONTROL?
# 2  
Old 05-25-2007
This is a common technique.

#define SOMETHING

does not give SOMETHING a value but it causes SOMETHING to become defined. Later you can test if SOMETHING is defined with:
#ifdef SOMETHING
or
#if defined(SOMETHING)

By not giving SOMETHING a value and simply defining you provide a clue to expect SOMETHING to be used in this manner.
# 3  
Old 05-25-2007
That response reminds me of the day I decided I wasn't cut out to be a Math Major. In my Linear Algebra class, my Professor said "There is something in the vector space. That something is nothingness."

Okay, so now, let me get this right.

When I go
#define SOMETHING

Something is being defined, but we aren't too sure what that something is? And then later on, when I see

#ifdef JOB_CONTROL
signal(SIGTSTP,(RETSIGTYPE (*)())susp);
#endif /*JOB_CONTROL*/

We can use that something, which aren't too sure of what it is, to test to see if the shell supports job control?
# 4  
Old 05-25-2007
Quote:
Originally Posted by frequency8
That response reminds me of the day I decided I wasn't cut out to be a Math Major. In my Linear Algebra class, my Professor said "There is something in the vector space. That something is nothingness."

Okay, so now, let me get this right.

When I go
#define SOMETHING

Something is being defined, but we aren't too sure what that something is? And then later on, when I see

#ifdef JOB_CONTROL
signal(SIGTSTP,(RETSIGTYPE (*)())susp);
#endif /*JOB_CONTROL*/

We can use that something, which aren't too sure of what it is, to test to see if the shell supports job control?
Never mind. I looked over the code again and thought about it. What you said just sank in.
# 5  
Old 05-26-2007
I was just using SOMETHING as an sample macro name. Since that freaked you out, let's just use your example.

#ifdef JOB_CONTROL
signal(SIGTSTP,(RETSIGTYPE (*)())susp);
#endif /*JOB_CONTROL*/

One of two things will happen as a result of the above code.

case 1.
Somewhere earlier we encountered a "#define JOB_CONTROL" line. If so we will get that signal line.

case 2.
We never encountered any "#define JOB_CONTROL" line. If so we get nothing.


So now we can control whether or not the program supports job control by the presense or absence of the "#define JOB_CONTROL" line. Choosing a name like SOMETHING would have been bad. But JOB_CONTROL is a good choice for this macro.
# 6  
Old 05-26-2007
#define is an old part of C.
Code:
int i;

i is defined as an integer here. It's value is unspecified, which is a bad idea for variables by the way.

Code:
#define I

I is defined but the value is unspecified. The difference here is that #define merely defines something that is a unique series of characters. It has no real meaning.
int i; has more meaning for humans, because integers are a class.

If this helps at all.
# 7  
Old 05-26-2007
Hi.

When we are up to our knees either in alligators or learning a new language, we don't usually look at all of the details -- for the latter, we're usually just interested in getting results. I'll risk bludgeoning the topic to death with my view.

I see the macro and inclusion facility as a piece separate from the compiler itself. One may place standard c code in a file name.i and use gcc to compile it without running the separate preprocessor, cpp. One, could, in fact, use a separate macro facility like m4 to do the work.

The function of cpp is quite limited, but like most symbol manipulators, it will keep track of symbols and their properties. You can test for the property of existence of a symbol in its memory -- that's really what ifdef and ifndef do. One could argue that the name of the query should be if_in_symbol_table, but the existence is often less important than the value, which is simply another property.

Often the history can illuminate the issues:
Quote:
BCPL, B, and C all fit firmly in the traditional procedural family typified by Fortran and Algol 60. They are particularly oriented towards system programming, are small and compactly described, and are amenable to translation by simple compilers. They are `close to the machine' in that the abstractions they introduce are readily grounded in the concrete data types and operations supplied by conventional computers, and they rely on library routines for input-output and other interactions with an operating system. With less success, they also use library procedures to specify interesting control constructs such as coroutines and procedure closures. At the same time, their abstractions lie at a sufficiently high level that, with care, portability between machines can be achieved.

...

Many other changes occurred around 1972-3, but the most important was the introduction of the preprocessor, partly at the urging of Alan Snyder [Snyder 74], but also in recognition of the utility of the the file-inclusion mechanisms available in BCPL and PL/I. Its original version was exceedingly simple, and provided only included files and simple string replacements: #include and #define of parameterless macros. Soon thereafter, it was extended, mostly by Mike Lesk and then by John Reiser, to incorporate macros with arguments and conditional compilation. The preprocessor was originally considered an optional adjunct to the language itself. Indeed, for some years, it was not even invoked unless the source program contained a special signal at its beginning. This attitude persisted, and explains both the incomplete integration of the syntax of the preprocessor with the rest of the language and the imprecision of its description in early reference manuals.

...

C is quirky, flawed, and an enormous success. While accidents of history surely helped, it evidently satisfied a need for a system implementation language efficient enough to displace assembly language, yet sufficiently abstract and fluent to describe algorithms and interactions in a wide variety of environments.

much more at: http://cm.bell-labs.com/cm/cs/who/dmr/chist.html by Dennis M. Ritchie
At the same place:
Quote:
None of BCPL, B, or C supports character data strongly in the language; each treats strings much like vectors of integers and supplements general rules by a few conventions. In both BCPL and B a string literal denotes the address of a static area initialized with the characters of the string, packed into cells. In BCPL, the first packed byte contains the number of characters in the string; in B, there is no count and strings are terminated by a special character, which B spelled `*e'. This change was made partially to avoid the limitation on the length of a string caused by holding the count in an 8- or 9-bit slot, and partly because maintaining the count seemed, in our experience, less convenient than using a terminator.
which goes to addressing an issue that Jim and I responded to involving c and perl strings at: http://unix.com/showthread.php?p=302...#post302119024
cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Debugging mysterious perl script problem

the attached perl script is a deamon that, once kicked off from the command line, it runs in the background and waits for the master server to tell it what plugins to run. the script works well. but the problem is, whenever i start it, after about a few seconds of starting it, i start getting... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Programming

#define in c

Hi, I had a head file, looks like #define MIN_NUM 10 #define MAX_NUM 10 is there any way to get "MAX_NUM" from 10? thanks. peter (9 Replies)
Discussion started by: laopi
9 Replies

3. Programming

help with #define in C

if i do this in C #define NUM 1234512345 then how come i cant print it out using int main(int argc, char **argv) { printf("%d\n", NUM); return 0; } well the result is -1219236538, why isnt it 1234512345 ? (7 Replies)
Discussion started by: omega666
7 Replies

4. Programming

#define

Hello, I would like to conditionaly comment in my code source some fields from arrays. So I use the property ## from the #define definition. my code: ... #define slet /##* #define etsl *##/ ... const T_SVT_ADLL_A653_DESC A_DESC = { { slet qwerty etsl SLICING,... (3 Replies)
Discussion started by: cypleen
3 Replies

5. Programming

mysterious execution failure and core dump generation

Posting again, as previous query had a typo. ======================================================= Hi, I am running a c++ program in unix AIX machine. There are two functions in a file which are being used by a third function in the same file. the two functions being used are of the same type.... (1 Reply)
Discussion started by: suresh_kb211
1 Replies

6. UNIX for Dummies Questions & Answers

#define in perl

Hi friends, I am not sure if perl questions can be raised here. :rolleyes: But I have a doubt if there is a way to do "#define" in perl, like in C. Does anyone know if it is feasible (without CPAN modules)? Thanks, Srini (7 Replies)
Discussion started by: srinivasan_85
7 Replies

7. UNIX for Advanced & Expert Users

Mysterious Server Shutdown

Virtually no UNIX admin experience. Any admin duties are shared by several folks with no special training. Today we had our Sun v880 server, running Solaris 5.8, shutdown for no apparent reason. When we checked on server we found it completely powered down, yet still connected to a fully... (6 Replies)
Discussion started by: buechler66
6 Replies
Login or Register to Ask a Question