Skip to content

Instantly share code, notes, and snippets.

@domiyanyue
Last active December 22, 2020 05:46
Show Gist options
  • Save domiyanyue/61fd064e16eca11dd74ca39c7cb0eda1 to your computer and use it in GitHub Desktop.
Save domiyanyue/61fd064e16eca11dd74ca39c7cb0eda1 to your computer and use it in GitHub Desktop.
example -D macro
#include <iostream>
#ifndef SIZE_ARRAY
#define SIZE_ARRAY 4
#endif
int main(){
int array[SIZE_ARRAY] = {0};
std::cout << "num of elements: " << sizeof(array)/sizeof(array[0]) << "\n";
return 0;
}
cpp_tutorial# g++ macro_example.cpp -o a
cpp_tutorial# ./a
num of elements: 4
cpp_tutorial# g++ macro_example.cpp -o a -DSIZE_ARRAY
cpp_tutorial# ./a
num of elements: 1
cpp_tutorial# g++ macro_example.cpp -o a -DSIZE_ARRAY=10
cpp_tutorial# ./a
num of elements: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment