c99

Printing null pointers with %p is undefined behavior?

痞子三分冷 提交于 2019-12-03 02:32:40
问题 Is it undefined behavior to print null pointers with the %p conversion specifier? #include <stdio.h> int main(void) { void *p = NULL; printf("%p", p); return 0; } The question applies to the C standard, and not to C implementations. 回答1: This is one of those weird corner cases where we're subject to the limitations of the English language and inconsistent structure in the standard. So at best, I can make a compelling counter-argument, as it's impossible to prove it :) 1 The code in the

When to use restrict and when not to

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:22:43
I have a general understanding of restrict but I'm hoping to clarify some fine points. I have a function that reads a null-terminated string from one buffer and writes out a URL encoded version in another buffer. The function has this signature (currently without restrict ): char const *StringUrlEncode(char const *unencoded, char *encoded, char *encodedEnd); unencoded is my null-terminated source string. The destination buffer is represented by encoded and encodedEnd , where encoded points to the first char in the buffer and encodedEnd points to the first char after the buffer, i.e. the

How does the below program output `C89` when compiled in C89 mode and `C99` when compiled in C99 mode?

北慕城南 提交于 2019-12-03 01:06:24
问题 I've found this C program from the web: #include <stdio.h> int main(){ printf("C%d\n",(int)(90-(-4.5//**/ -4.5))); return 0; } The interesting thing with this program is that when it is compiled and run in C89 mode, it prints C89 and when it is compiled and run in C99 mode, it prints C99 . But I am not able to figure out how this program works. Can you explain how the second argument of printf works in the above program? 回答1: C99 allows // -style comments, C89 does not. So, to translate: C99:

Reasons not to use _Bool in Objective-C?

帅比萌擦擦* 提交于 2019-12-02 20:35:31
Since C99, C now has a proper Boolean type, _Bool . Objective-C, as a strict superset of C, inherits this, but when it was created back in the 1980s, there was no C Boolean type, so Objective-C defined BOOL as signed char . All of Cocoa uses BOOL , as does all non-NeXT/Apple Cocoa code that I've seen. Obviously, for compatibility with existing protocols (e.g., -applicationShouldTerminateAfterLastWindowClosed: from NSApplicationDelegate ), matching the already-declared type is preferable, if for no other reason than to avert a warning. For cleanliness/readability purposes, stdbool.h defines

Simulater/Generated switch statement range in c

两盒软妹~` 提交于 2019-12-02 19:01:14
问题 Is there a hack to support range case in a c(99?) or objective C switch statement ? I know this is not supported to write something like this: switch(x) case 1: case 2..10: case 11: But I was thinking there should be a way to generate code with a #define macro. Of course I can define a macro with the list of cases but I was hoping for a more elegant way like CASERANGE(x,x+10) which would generate: case x case x+1 case x+2 is it even possible ? 回答1: GCC has an extension to the C language that

What's the proper use of printf to display pointers padded with 0s

帅比萌擦擦* 提交于 2019-12-02 17:53:14
In C, I'd like to use printf to display pointers, and so that they line up properly, I'd like to pad them with 0s. My guess was that the proper way to do this was: printf("%016p", ptr); This works, but this gcc complains with the following message: warning: '0' flag used with ‘%p’ gnu_printf format I've googled a bit for it, and the following thread is on the same topic, but doesn't really give a solution. http://gcc.gnu.org/ml/gcc-bugs/2003-05/msg00484.html Reading it, it seems that the reason why gcc complains is that the syntax I suggested is not defined in C99. But I can't seem to find any

subexpressions evaluation order

我是研究僧i 提交于 2019-12-02 17:32:08
问题 I've looked at SO/IEC 9899:201x under J.1 Unspecified behavior: "The order in which subexpressions are evaluated and the order in which side effects take place, except as specified for the function-call (), &&, ||, ?:, and comma operators (6.5)." Does this means that in func1() + func2(); func2() may be preformed before func1(), or even during func1() ? 回答1: In the current standard (ISO/IEC 9899:1999) there is a sequence point between function calls but the order of evaluation of the operands

Converting an int to char using printf

帅比萌擦擦* 提交于 2019-12-02 17:28:21
问题 I'm just wondering if following is the right way to convert int to display it as a char #include <stdio.h> int main() { int x = 500; printf("%hhd\n", x); } Also, from above I wonder if I should do the following to display the value of character. #include <stdio.h> int main() { char c = 'a'; printf("%hhd\n", c); } Or would just printf("%d\n", c); be fine? So, basically I'm trying to output the first byte of integer through printf without any casting. 回答1: Using %hhd in your first example

Printing null pointers with %p is undefined behavior?

纵然是瞬间 提交于 2019-12-02 16:02:32
Is it undefined behavior to print null pointers with the %p conversion specifier? #include <stdio.h> int main(void) { void *p = NULL; printf("%p", p); return 0; } The question applies to the C standard, and not to C implementations. This is one of those weird corner cases where we're subject to the limitations of the English language and inconsistent structure in the standard. So at best, I can make a compelling counter-argument, as it's impossible to prove it :) 1 The code in the question exhibits well-defined behaviour. As [7.1.4] is the basis of the question, let's start there: Each of the

The most useful user-made C-macros (in GCC, also C99)? [closed]

老子叫甜甜 提交于 2019-12-02 14:10:43
What C macro is in your opinion is the most useful? I have found the following one, which I use to do vector arithmetic in C : #define v3_op_v3(x, op, y, z) {z[0]=x[0] op y[0]; \ z[1]=x[1] op y[1]; \ z[2]=x[2] op y[2];} It works like that: v3_op_v3(vectorA, +, vectorB, vectorC); v3_op_v3(vectorE, *, vectorF, vectorJ); ... for-each loop in C99: #define foreach(item, array) \ for(int keep=1, \ count=0,\ size=sizeof (array)/sizeof *(array); \ keep && count != size; \ keep = !keep, count++) \ for(item = (array)+count; keep; keep = !keep) int main() { int a[] = { 1, 2, 3 }; int sum = 0; foreach(int