c99

How is memory allocated for an implicitly defined multidimensional array in C99?

為{幸葍}努か 提交于 2019-11-27 07:26:06
问题 I'm trying to write a C99 program and I have an array of strings implicitly defined as such: char *stuff[] = {"hello","pie","deadbeef"}; Since the array dimensions are not defined, how much memory is allocated for each string? Are all strings allocated the same amount of elements as the largest string in the definition? For example, would this following code be equivalent to the implicit definition above: char stuff[3][9]; strcpy(stuff[0], "hello"); strcpy(stuff[1], "pie"); strcpy(stuff[2],

Good introduction to <inttypes.h> [closed]

倾然丶 夕夏残阳落幕 提交于 2019-11-27 07:16:18
I want to recommend the use of <inttypes.h> to someone doing printf with mixed 32/64 bit builds. I tried to Google an introduction or tutorial page with a few examples and usage guidelines, but I couldn't find one. Can someone recommend an introduction or tutorial for <inttypes.h> ? Edwin Buck Try http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/inttypes.h.html for a start. A better example of how to use the new portable formatting macros was found in avr-libc . I've included an example (from the link) to illustrate. QNX libraries also have a better human-readable description (if you

Is there any reason not to use fixed width integer types (e.g. uint8_t)?

别来无恙 提交于 2019-11-27 07:03:52
Assuming you're using a compiler that supports C99 (or even just stdint.h), is there any reason not to use fixed width integer types such as uint8_t? One reason that I'm aware of is that it makes much more sense to use char s when dealing with characters instead of using (u)int8_t s, as mentioned in this question . But if you are planning on storing a number, when would you want to use a type that you don't know how big it is? I.e. In what situation would you want to store a number in a unsigned short without knowing if it is 8, 16, or even 32 bits, instead of using a uint16t ? Following on

Why is SCHAR_MIN defined as -127 in C99?

核能气质少年 提交于 2019-11-27 06:54:04
问题 §5.2.4.2.1 of C99 defines SCHAR_MIN as -127 and SCHAR_MAX as 127. Should not the range for an 8 bit signed integer be -128 to +127? The limits.h for my compiler defines SCHAR_MIN as (-1 << ((CHAR_BIT)-1)) , which is -128 given CHAR_BIT is 8. Is there any reason why SCHAR_MIN was defined -127 and not -128 ? 回答1: It doesn't actually define SCHAR_MIN as -127, it defines the minimum range of signed characters to -127..127. It does this because it has to be able to handle the other two encoding

What is the use of the `inline` keyword in C?

三世轮回 提交于 2019-11-27 06:53:43
I read several questions in stackoverflow about inline in C but still am not clear about it. static inline void f(void) {} has no practical difference with static void f(void) {} . inline void f(void) {} in C doesn't work as the C++ way. How does it work in C? What actually does extern inline void f(void); do? I never really found a use of the inline keyword in my C programs, and when I see this keyword in other people's code, it's almost always static inline , in which I see no difference with just static . M.M Note: when I talk about .c files and .h files in this answer, I assume you have

Generating function declaration using a macro iteration

被刻印的时光 ゝ 提交于 2019-11-27 06:22:00
问题 I'm trying to generate a function declaration using a macro /* goal: generate int f(int a, float b) */ template<typename P> struct ptype; template<typename P> struct ptype<void(P)> { typedef P type; }; #define NAMEe #define COMMAe #define COMMA , #define NAME(N) N PARAMS #define PARAMS(P, ...) COMMA ## __VA_ARGS__ P NAME ## __VA_ARGS__ #define PARAM_ITER(P) P NAME #define PROTO(R, N, P) \ ptype<void R>::type N (PARAM_ITER P (,e)) PROTO((int), f, (int)(a)(float)(b)); It will iteratively

Allocating struct with flexible array member

冷暖自知 提交于 2019-11-27 06:06:47
问题 This is C99 code: typedef struct expr_t { int n_children; foo data; // Maybe whatever type with unknown alignment struct expr_t *children[]; } expr_t; Now, how do I allocate memory ? expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); or expr_t *e = malloc (offsetof (expr_t, children) + n * sizeof (expr_t *)); ? Is sizeof even guaranteed to work on an type with flexible array member (GCC accepts it) ? 回答1: expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); is well defined

Type of unsigned bit-fields: int or unsigned int

為{幸葍}努か 提交于 2019-11-27 05:58:10
问题 Section 6.3.1.1 of the C99 standard contains: The following may be used in an expression wherever an int or unsigned int may be used: [...] A bit-field of type _Bool , int , signed int , or unsigned int . If an int can represent all values of the original type, the value is converted to an int ; otherwise, it is converted to an unsigned int . It seems to me that this implies that unsigned int bit-fields are promoted to int , except when the width of the unsigned bit-field is equal to the

Printf long long int in C with GCC?

左心房为你撑大大i 提交于 2019-11-27 05:24:02
问题 How do I printf long long int and also unsigned long long int in C99 using GCC? I have searched the other posts which suggest to use %lld but it gives these warnings: warning#1: unknown conversion type character 'l' in format [-Wformat]| warning#2: too many arguments for format [-Wformat-extra-args]| For the following attempt: #include <stdio.h> int main() { long long int x = 0; unsigned long long int y = 0; printf("%lld\n", x); printf("%llu\n", y); } 回答1: If you are on windows and using

How to use compound literals to `fprintf()` multiple formatted numbers with arbitrary bases?

跟風遠走 提交于 2019-11-27 05:20:56
I'd like to convert multiple numbers into some representation and then use the flags, width and precision of *printf() specifiers. Preference would be to avoid global or static buffers. The key problem appears to be is how to provide a char[] for each of the converted numbers? fprintf(ostream, "some_format", foo(int_a, base_x), foo(int_b, base_y), ...); How to use C11 compound literals to solve this? How to use C99 (or later) compound literals to solve this? chux C99 C11 introduced compound literals which allow not only a complicated initialized structure, but also an "in-line" variable. Code