Skip to content

Instantly share code, notes, and snippets.

@AssafTzurEl
Last active June 14, 2019 09:51
Show Gist options
  • Save AssafTzurEl/2e44aeeff229969047727c6bf351041b to your computer and use it in GitHub Desktop.
Save AssafTzurEl/2e44aeeff229969047727c6bf351041b to your computer and use it in GitHub Desktop.
#define
#define DEBUG_MODE
#ifdef UNICODE
#define TCHAR wchar_t
#else
#define TCHAR char
#endif
#ifdef _DEBUG
#define LOG(format, val) printf(format, val)
#else
#define LOG(format, val)
#endif
TCHAR string[10];
void main()
{
int val = 10;
// in real life, use "_DEBUG" instead of "DEBUG_MODE" (prefdefined in project properties)
#if defined DEBUG_MODE
printf("val = %d\n", val);
#endif
val += 20;
// ...
--val;
// ...
#ifdef DEBUG_MODE
printf("val = %d\n", val);
#endif
LOG("val = %d\n", val);
#ifndef DEBUG_MODE // #if !defined
// Release mode code
#endif
}
#include <string.h>
#define PI 3.14
#define MAX(a, b) a > b ? a : b
#define MAX_FIXED(a, b) (((a) > (b)) ? (a) : (b))
#define MULTI_LINE int variable = 0; \
variable++; \
variable += 5;
#define INT_VAR(name) int int_##name;
#define STRUCT(name) typedef struct \
{ \
int name##_id; \
char name##_value[10]; \
} ##name;
int Max(int a, int b)
{
return a > b ? a : b;
}
int f()
{
return 10;
}
int g()
{
static s = 20;
s -= 7;
return s;
}
void main()
{
double radius = 10.0;
double circumference = 2 * PI * radius;
double x = 1.1, y = 2.2;
int maxint = Max(x, y); // converts to int, not good enough
maxint = MAX(17, -37);
// replaced with:
maxint = 17 > -37 ? 17 : 37;
double maxdouble = MAX(x, y);
// replaced with:
maxdouble = x > y ? x : y;
maxint = MAX(10, 5) + 2; // returns 10 instead of 12!?
// replaced with:
maxint = 10 > 5 ? 10 : 5 + 2; // plus is stronger than ternary operator
maxint = MAX_FIXED(10, 5) + 2; // works fine
maxint = MAX_FIXED(f(), g()); // max of 10 and 13 is 6 !?
MULTI_LINE;
INT_VAR(myVar);
int_myVar = 10;
STRUCT(Object);
Object o;
o.Object_id = 10;
strcpy(o.Object_value, "something");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment