c - Include a file multiple times with different macros -
i got file contains generic methods working arrays of different number types (the basic ideas described in pseudo-generics in c). type can specified setting type
macro. looks (just part of it):
array_analyzers.c
:
#ifndef type #error type isn't defined #endif #define concat(x, y) x ## y #define get_name(base, type) concat(base, type) type get_name(get_minimum_, type) (type nums[static 1], int len){ type min = nums[0]; (int = 1; < len; i++) { if (nums[i] < min) { min = nums[i]; } } return min; } #undef concat #undef get_name #undef type
now, created header file looks this:
array_analyzers.h
:
#ifndef type #error type isn't defined #endif #define concat(x, y) x ## y #define get_name(base, type) concat(base, type) type get_name(get_minimum_, type) (type nums[static 1], int len); #undef concat #undef get_name #undef type
finally, want use main.c
:
#include <stdio.h> #define type int #include "array_analyzers.h" #define type double #include "array_analyzers.h" int main(void){ int nums[] = {1, 2, 3}; printf("%i\n", get_minimum_int(nums, 3)); }
however, i'm getting following error message:
array_analyzers.c:2:2: error: #error type isn't defined
what wrong here? works when run preprocessor first , create separate files right contents, awful.
you don not declare int in loop @ beginning of scope, in loop. if using gcc can add -std=c99 compiler option accept code.
in function standard loop should alway accepted. in function int declared @ beginning of scope.
int ansi_c (){ int i; ( = 0 ; < 10; i++ ){ } }
and in function loop accepted when -std=c99 or std=gnu99 added. function declares int inside loop.
c99(){ (int = 0; < 10; i++ ){ } }
Comments
Post a Comment