Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C - #define

Name: Anonymous 2011-02-15 17:37

Completely beginner question, so please keep answers as simple as possible:
In C programming, what is the purpose of using the #define directive (eg #define PI 3.14), when you can just declare a variable and initialize it to the same value?

Name: Anonymous 2011-02-15 17:38

variables are deprecated, use a #define.

Name: Anonymous 2011-02-15 17:58

>>2
How exactly are they deprecated? Are you suggesting I only define directives and avoid using variables altogether? A little bit of clarification would be nice :)

Name: Anonymous 2011-02-15 18:22

>>3 he's a troll

you can do complex things like template
#define RADTODEG(x) ((x) * 57.29578)

or include guard


#ifndef FILE_NAME_H
#define FILE_NAME_H

/* code */

#endif // #ifndef FILE_NAME_H

Name: Anonymous 2011-02-15 18:26

>>4

include guard has been deprecated, use #pragma once

Name: Anonymous 2011-02-15 18:30

Mostly historical reasons, but there are situations where you need constant expressions or values at preprocessing time, neither of which can be fulfilled by variables.

Name: Anonymous 2011-02-15 18:31

>>4,5
You wouldn't need them if you used a single file in first place.

multiple files projects considered harmful

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2011-02-15 18:38

>>7
Two files, void.h with all my defines and includes, and the file with the particular code.

_____________________________________________
http://bayimg.com/image/iafhbaacj.jpg
orbis terrarum delenda est
http://encyclopediadramatica.com/Portal:Furfaggotry Furry Drama Encyclopedia

Name: Anonymous 2011-02-15 18:50

>>8

Expected this from a GNU/neckbeard.

Name: Anonymous 2011-02-15 18:57

>>9
Expected this from a GNU/nokoboard.

Name: Anonymous 2011-02-15 19:06

Man, these threads have a tendency of straying off topic quickly. Now I think I'm more confused...is there a point in using variables at all, aside from when obtaining input?

Name: Anonymous 2011-02-15 19:08

first read this;

http://en.wikipedia.org/wiki/C_preprocessor

keep in mind that:

The language of preprocessor directives is agnostic to the grammar of C, so the C preprocessor can also be used independently to process other types of files.

next, man cpp and http://gcc.gnu.org/onlinedocs/cpp/

simplified:

the C preprocessor can be used to do transform your code during compile time: if you #define a variable then cpp will replace the variable by the value before the source is compiled.

if the use a regular variable then your code will look up the value of that variable during runtime every time you use it (excluding compiler optimizations or usage of const etc... if you know about those things this post isn't meant for you).

</thread> ?

Name: Anonymous 2011-02-15 19:08

>>11
#defines exist and get expanded at compile-time. Variables point some memory location at runtime. Thread over.

Name: Anonymous 2011-02-15 19:09

>>13
I misspelled sage, sorry

Name: Anonymous 2011-02-16 1:53

>>14
I misspelled noko, sorry

Name: Anonymous 2011-02-16 4:03

#define true rand()%2

Name: Anonymous 2011-02-16 4:16

>>16
#define true ((rand()&1))

We needd more parenthesis

Name: Anonymous 2011-02-16 8:06

#define true (((rand())&(1)))
EXPERT LISP PROGRAMMER

Name: Anonymous 2011-02-16 8:28

#define lisp ((((((((((lisp))))))))))

Name: Anonymous 2011-02-16 8:43

Completely beginner question, so please keep answers as simple as possible:
In LISP programming, what is the purpose of using the defmacro (eg (defmacro pi () 3.14)), when you can just defvar a variable with the same value?

Name: Anonymous 2011-02-16 9:04

>>20
There is actually a difference between that and the defvar. In your defmacro, 3.14 would be substituted by the compiler in place of pi when macro expansion is performed (actually, not in place of pi, but (pi), as pi is just a symbol, and not just (pi), but in places where (pi) would be macroexpanded or meant to be evaluated).
Anyways, defvar/defperameter isn't even the recommended solution to that, it's supposed to be a constant, which means defconstant, also the implementation suggests that it should be a long float. Here's the code my implementation has for it:

(defconstant pi
  #!+long-float 3.14159265358979323846264338327950288419716939937511l0
  #!-long-float 3.14159265358979323846264338327950288419716939937511d0)

defconstant defines a global variable which should not be modified, and which the compiler can expand upon compilation (and it usually does so). If you want to force the expansion in all implementations, you should instead use a symbol macro, although that's a bit abusive for constants.
For example:
(define-symbol-macro +pi+ 3.14159265358979323846264338327950288419716939937511)
Oh, and in a related note, while CL doesn't actually support lexically scoped globals, they can be implemented (in about a page or two of code) using symbol macros, macros and a few other language features.

Name: Anonymous 2011-02-16 9:43

Completely beginner question, so please keep answers as simple as possible:
``In LISP'' programming, what is the purpose of all that fucking syntax, when you can just use s-expressions?

Name: Anonymous 2011-02-16 9:46

>>22
I think that that ``in LISP'' guy doesn't actually like Lisp to use it. I never had any problem with S-Expressions after I got my copy of Emacs and Paredit, however without them, I can see how someone could find it painful - it's just a matter of knowing the right tools to use, and being informed of their existence early enough when learning the language.

Name: Anonymous 2011-02-16 9:59

>>23
Yeah, but he first had to write all the DSL in pure Lisp. And I still wonder how he indents/formats his DSL code.

Name: Anonymous 2011-02-16 10:19

>>24
He could write his DSL compiler or interpreter in the DSL, thus making the language self-hosting. Of course, assuming his DSL's implementation exists at all (he keeps giving silly excuses for why he doesn't want to post it), I do think it's possible he actually write it in Lisp.

Don't change these.
Name: Email:
Entire Thread Thread List