C compile error: “Variable-sized object may not be initialized”

I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length is not … Read more

Is there a printf converter to print in binary format?

Hacky but works for me: For multi-byte types You need all the extra quotes unfortunately. This approach has the efficiency risks of macros (don’t pass a function as the argument to BYTE_TO_BINARY) but avoids the memory issues and multiple invocations of strcat in some of the other proposals here.

How to check if a string is a number?

Forget about ASCII code checks, use isdigit or isnumber (see man isnumber). The first function checks whether the character is 0–9, the second one also accepts various other number characters depending on the current locale. There may even be better functions to do the check – the important lesson is that this is a bit more complex than it looks, because … Read more

How to convert integer to char in C?

A char in C is already a number (the character’s ASCII code), no conversion required. If you want to convert a digit to the corresponding character, you can simply add ‘0’: The ‘0’ is a character in the ASCll table.

Char Comparison in C

A char variable is actually an 8-bit integral value. It will have values from 0 to 255. These are almost always ASCII codes, but other encodings are allowed. 0 stands for the C-null character, and 255 stands for an empty symbol. So, when you write the following assignment: It is the same thing as this on an ASCII system. So, you can compare two char variables using … Read more

Difference between scanf() and fgets()

There are multiple differences. Two crucial ones are: fgets() can read from any open file, but scanf() only reads standard input. fgets() reads ‘a line of text’ from a file; scanf() can be used for that but also handles conversions from string to built in numeric types. Many people will use fgets() to read a line of data and then use sscanf() to dissect it.

Difference between int32, int, int32_t, int8 and int8_t

Between int32 and int32_t, (and likewise between int8 and int8_t) the difference is pretty simple: the C standard defines int8_t and int32_t, but does not define anything named int8 or int32 — the latter (if they exist at all) is probably from some other header or library (most likely predates the addition of int8_t and int32_t in C99). Plain int is quite a bit different from the others. Where int8_t and int32_t each have a specified size, int can be … Read more

MIN and MAX in C

Where are MIN and MAX defined in C, if at all? They aren’t. What is the best way to implement these, as generically and type safe as possible (compiler extensions/builtins for mainstream compilers preferred). As functions. I wouldn’t use macros like #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan to deploy your code. Either … Read more

How do I create an array of strings in C?

If you don’t want to change the strings, then you could simply do When you do it like this you will allocate an array of two pointers to const char. These pointers will then be set to the addresses of the static strings “blah” and “hmm”. If you do want to be able to change the actual string content, … Read more