c-preprocessor

iOS Writing Macro detect 3.5 inch or 4 inch display [duplicate]

喜你入骨 提交于 2019-12-03 02:02:16
This question already has an answer here: How to detect iPhone 5 (widescreen devices)? 24 answers I am trying to write a macro to determine the device is 3.5 inch or 4 inch. Some thing similar below. #define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 ) #define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 ) Can someone help me. Please Nitin Gohel you can detect iphopne 3.5 inch or 4 inch using bellow:- #define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE you can check it using

Is it reasonable to use enums instead of #defines for compile-time constants in C?

为君一笑 提交于 2019-12-03 01:41:54
I'm coming back to some C development after working in C++ for a while. I've gotten it into my head that macros should be avoided when not necessary in favor of making the compiler do more work for you at compile-time. So, for constant values, in C++ I would use static const variables, or C++11 enum classes for the nice scoping. In C, static constants are not really compile-time constants, and enums may (? or may not?) behave slightly differently. So, is it reasonable to prefer using enums for constants rather than #defines? For reference, here's an excellent list of pros and cons of enums,

Does gcc define anything when -g is specified?

别来无恙 提交于 2019-12-03 00:55:59
Shortly, I want to know if gcc (or g++. I need it in C , but also am curious about c++) defines any special symbols if -g is enabled. Does it? If so, what symbols? In the search process I found out that: _DEBUG is defined manually (by manually I mean -D_DEBUG ) and is a habit taken from Visual C programmers (because VC defines _DEBUG when compiling in debug mode) NDEBUG is defined if NOT in debug mode. Although I found a few places saying this, I tried with my gcc and g++ in .c and .cpp files and in none of them with or without -g no such symbol was defined! Edit : Let me demonstrate why I don

C++ Macros: manipulating a parameter (specific example)

▼魔方 西西 提交于 2019-12-03 00:38:58
I need to replace GET("any_name") with String str_any_name = getFunction("any_name"); The hard part is how to trim off the quote marks. Possible? Any ideas? How about: #define UNSAFE_GET(X) String str_##X = getFunction(#X); Or, to safe guard against nested macro issues: #define STRINGIFY2(x) #x #define STRINGIFY(x) STRINGIFY2(x) #define PASTE2(a, b) a##b #define PASTE(a, b) PASTE2(a, b) #define SAFE_GET(X) String PASTE(str_, X) = getFunction(STRINGIFY(X)); Usage: SAFE_GET(foo) And this is what is compiled: String str_foo = getFunction("foo"); Key points: Use ## to combine macro parameters into

Why does the NULL de-reference in this C snippet not cause undefined behaviour

六月ゝ 毕业季﹏ 提交于 2019-12-03 00:14:45
问题 I came across a piece of code where NULL is typecast to an structure pointer type (foo *) 0 , and with that pointer de-referencing a member ((foo *)0)->m , and using address of that &(((foo *)0)->m)) and type casting it to integer to get the memory index of that member with in the structure. ((unsigned int)(&(((foo *)0)->m))) . To my knowledge NULL pointer dereference should always result a segmentation fault in C. But I don't understand how NULL pointer can be de-referenced like this and

algorithm behind the generation of the reverse bits lookup table(8 bit)

*爱你&永不变心* 提交于 2019-12-02 23:53:11
I found the lookup table here. The table is generated as a reverse bits table of 8 bits. I can not figure out why it works. Please explain the theory behind it. Thanks static const unsigned char BitReverseTable256[256] = { # define R2(n) n, n + 2*64, n + 1*64, n + 3*64 # define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16) # define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 ) R6(0), R6(2), R6(1), R6(3) }; First off a comment: This kind of thing is normally only done in the IOCCC . Code like this should not be used in production-environments because it is non-obvious . The

cpp preprocessor output not able to understand?

走远了吗. 提交于 2019-12-02 23:41:42
问题 Sorry if my question is very basic. I would like to understand the output produced by the preprocessor cpp. Let's say i have a very basic following program. #include <stdio.h> #include <stdlib.h> int x=100; int main () { printf ("\n Welcome..\n"); } I execute the following command. cpp main.c main.i in main.i # 1 "/usr/include/stdio.h" 1 3 4 What is the meaning of the above line ?.. 回答1: The gcc documentation explains the C preprocessor output aptly. Here are the relevant sections: The output

Preprocessor token expansion [duplicate]

本秂侑毒 提交于 2019-12-02 23:07:19
This question already has an answer here: How to concatenate twice with the C preprocessor and expand a macro as in “arg ## _ ## MACRO”? 2 answers My mental model of how the preprocessor works is apparently incomplete, and this is driving me crazy. I want to concatenate two tokens, but the second token should be expanded first. #define ANSWER 42 #define FOO foo_ ## ANSWER Here, FOO expands to foo_ANSWER , but I want it to be foo_42 . So I define a MERGE macro in the hopes that this would somehow expand the arguments before concatenation: #define MERGE(x, y) x ## y #define BAR MERGE(bar_,

Swift alternative for #pragma clang diagnostic

此生再无相见时 提交于 2019-12-02 22:56:30
Problem I recently encountered a warning in a third party utility (WEPopover) in this piece of code: _effectivePopoverContentSize = _contentViewController.contentSizeForViewInPopover; This was generating the following warning: warning: 'contentSizeForViewInPopover' is deprecated: first deprecated in iOS 7.0 - Use UIViewController.preferredContentSize instead. [-Wdeprecated-declarations] _effectivePopoverContentSize = _contentViewController.contentSizeForViewInPopover; One temporary fix for this in Objective-C is to use pragma clang diagnostic to silence the error (I'll let the code author deal

Redundant macro usage in initializer lists

隐身守侯 提交于 2019-12-02 22:52:54
问题 After I were able to successfully reduce code duplication with this I want to further reduce my code duplication with totally different problem #include <iostream> #include <array> #define A1 #define B2 #define C3 #define D4 #define E5 enum Enum { #ifdef A1 one, #endif #ifdef B2 two, #endif #ifdef C3 three, #endif #ifdef D4 four, #endif #ifdef E5 five, #endif end }; const char* map(Enum e) { switch(e) { #ifdef A1 case one : return "one"; #endif #ifdef B2 case two: return "two"; #endif #ifdef