What is the worst real-world macros/pre-processor abuse you've ever come across?
From memory, it looked something like this:
#define RETURN(result) return (result);}
int myfunction1(args) {
int x = 0;
// do something
RETURN(x)
int myfunction2(args) {
int y = 0;
// do something
RETURN(y)
int myfunction3(args) {
int z = 0;
// do something
RETURN(z)
Yes that's right, no closing braces in any of the functions. Syntax highlighting was a mess, so he used vi to edit (not vim, it has syntax coloring!)
He was a Russian programmer who had mostly worked in assembly language. He was fanatical about saving as many bytes as possible because he had previously worked on systems with very limited memory. "It was for satellite. Only very few byte, so we use each byte over for many things." (bit fiddling, reusing machine instruction bytes for their numeric values) When I tried to find out what kinds of satellites, I was only able to get "Orbiting satellite. For making to orbit."
He had two other quirks: A convex mirror mounted above his monitor "For knowing who is watching", and an occasional sudden exit from his chair to do a quick ten pushups. He explained this last one as "Compiler found error in code. This is punishment".
What is the worst real-world macros/pre-processor abuse you've ever come across?
From memory, it looked something like this:
#define RETURN(result) return (result);}
int myfunction1(args) {
int x = 0;
// do something
RETURN(x)
int myfunction2(args) {
int y = 0;
// do something
RETURN(y)
int myfunction3(args) {
int z = 0;
// do something
RETURN(z)
Yes that's right, no closing braces in any of the functions. Syntax highlighting was a mess, so he used vi to edit (not vim, it has syntax coloring!)
He was a Russian programmer who had mostly worked in assembly language. He was fanatical about saving as many bytes as possible because he had previously worked on systems with very limited memory. "It was for satellite. Only very few byte, so we use each byte over for many things." (bit fiddling, reusing machine instruction bytes for their numeric values) When I tried to find out what kinds of satellites, I was only able to get "Orbiting satellite. For making to orbit."
He had two other quirks: A convex mirror mounted above his monitor "For knowing who is watching", and an occasional sudden exit from his chair to do a quick ten pushups. He explained this last one as "Compiler found error in code. This is punishment".
What is the worst real-world macros/pre-processor abuse you've ever come across?
From memory, it looked something like this:
#define RETURN(result) return (result);}
int myfunction1(args) {
int x = 0;
// do something
RETURN(x)
int myfunction2(args) {
int y = 0;
// do something
RETURN(y)
int myfunction3(args) {
int z = 0;
// do something
RETURN(z)
Yes that's right, no closing braces in any of the functions. Syntax highlighting was a mess, so he used vi to edit (not vim, it has syntax coloring!)
He was a Russian programmer who had mostly worked in assembly language. He was fanatical about saving as many bytes as possible because he had previously worked on systems with very limited memory. "It was for satellite. Only very few byte, so we use each byte over for many things." (bit fiddling, reusing machine instruction bytes for their numeric values) When I tried to find out what kinds of satellites, I was only able to get "Orbiting satellite. For making to orbit."
He had two other quirks: A convex mirror mounted above his monitor "For knowing who is watching", and an occasional sudden exit from his chair to do a quick ten pushups. He explained this last one as "Compiler found error in code. This is punishment".
Preprocessor error when defining =
The ==
is a single token, it cannot be split in half. You should run gcc -E
on your code
From GCC manual pages:
-E
Stop after the preprocessing stage; do not run the compiler proper. The output is in
the form of preprocessed source code, which is sent to the standard output.Input files that don't require preprocessing are ignored.
For your code gcc -E
gives the following output
if(x= =6)
printf("X == 6\n");
if(y= =6)
printf("Y==6\n");
The second =
is what causes the error message expected expression before ‘=’ token
See expanded C Macros
Compile your code from the terminal, with the -E
flag: gcc my_code.c -E
.
This will print the preprocessed code directly to the terminal. If you want to save it to a file instead, add something like -o result.txt
at the end.
Simplify C++ inclusion with macros
The right way to do it is to pass SOMEPROJ_SRC_PATH
as a search path of include files with -I
option.
main.cpp:
#include <iostream>
#include "some.h"
int main() {
std::cout << HELLO << std::endl;
}
/some/path/some.h:
#define HELLO "Hello, world!"
And then compile it:
g++ -I /some/path -o main main.cpp
Tasm macros default value
subs MACRO x,y
IFB <y>
mov ax,x
sub ax,1
ELSE
mov ax,x
sub ax,y
ENDIF
ENDM
You need a reference: http://www.bitsavers.org/pdf/borland/turbo_assembler/
playing with Macros
header file
#include <math.h>
extern double peri;
extern double area;
#define AREA_C(x) (x*x* M_PI)
#define PERI_C(x) (x*2*M_PI)
#define PERI(length, depth, polygone){\
if((polygone == "square")||(polygone == "rectangle"))\
peri = length * 2 + depth * 2;\
}
#define AREA(a,b,poly){\
if((poly == "square")||(poly == "rectangle"))\
area = a * b;\
else if(poly == "triangle")\
area = a * b / 2;\
puts("");\
}
file.c
#include "areaperi.h"
#include <stdio.h>
double area;
double peri;
int main(){
double Carea = AREA_C(3);
double Cperi = PERI_C(3);
printf("area: %.2lf\n", Carea);
printf("peri: %.2lf\n", Cperi);
AREA(2,2,"square");
printf("Area of the square: %.2lf\n", area);
return 0;
}
errors
after having DEFINED the variable I got rid of the main error, but there was still the
"24: warning: backslash-newline at end of file"
, with that one I just addedputs("");
and fix it.
If want to learn more about that error I find the answer on: this stock overflow page
Macroexpand for parenscript
In parenscript's compiler.lisp
file, there are the functions ps-macroexpand-1
and ps-macroexpand
. Unfortunately, they are not exported by the parenscript package. You can call them anyway using a double colon.
For example,
(defpsmacro aif (test true &rest false)
`(let ((it ,test))
(if it ,true ,@false)))
(ps::ps-macroexpand-1 '(aif 3 it))
;;=>
(LET ((IT 3))
(IF IT
IT))
T
Related Topics
How to Easily Format My Data Table in C++
Determining 32 VS 64 Bit in C++
Demote Boost::Function to a Plain Function Pointer
Initializing a Two Dimensional Std::Vector
C++ Dll Export: Decorated/Mangled Names
Difference Between a Pointer and Reference Parameter
Access Friend Function Defined in Class
In C/C++ What's the Simplest Way to Reverse the Order of Bits in a Byte
Does Const-Correctness Give the Compiler More Room For Optimization
How Is Std::String Implemented
Removing Leading and Trailing Spaces from a String
Initialize a Const Array in a Class Initializer in C++
What New Capabilities Do User-Defined Literals Add to C++
What Are the Different Calling Conventions in C/C++ and What Do Each Mean
String Literal Address Across Translation Units
Do I Have to Acquire Lock Before Calling Condition_Variable.Notify_One()