Why are #ifndef and #define used in C++ header files?

Those are called #include guards. Once the header is included, it checks if a unique value (in this case HEADERFILE_H) is defined. Then if it’s not defined, it defines it and continues to the rest of the page. When the code is included again, the first ifndef fails, resulting in a blank file. That prevents … Read more

Why should we typedef a struct so often in C?

As Greg Hewgill said, the typedef means you no longer have to write struct all over the place. That not only saves keystrokes, it also can make the code cleaner since it provides a smidgen more abstraction. Stuff like becomes cleaner when you don’t need to see the “struct” keyword all over the place, it looks more … Read more

What is the effect of extern “C” in C++?

extern “C” makes a function-name in C++ have C linkage (compiler does not mangle the name) so that client C code can link to (use) your function using a C compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format (that was compiled by your … Read more

How to print in C

Very much a beginner to C, in fact this is my first tester program. I can’t actually figure out how to print this number out to the terminal. I am sure that in Java I could just replace the printf with System.out and it would have worked. I tried searching the answer earlier but if … Read more

Categories C Tags

typedef struct vs struct definitions [duplicate]

The common idiom is using both: They are different definitions. To make the discussion clearer I will split the sentence: In the first line you are defining the identifier S within the struct name space (not in the C++ sense). You can use it and define variables or function arguments of the newly defined type … Read more

How to convert an int to string in C?

EDIT: As pointed out in the comment, itoa() is not a standard, so better use sprintf() approach suggested in the rivaling answer! You can use itoa() function to convert your integer value to a string. Here is an example: If you want to output your structure into a file there is no need to convert … Read more

Using boolean values in C

From best to worse: Option 1 (C99 and newer) Option 2 Option 3 Option 4 Explanation Option 1 will work only if you use C99 (or newer) and it’s the “standard way” to do it. Choose this if possible. Options 2, 3 and 4 will have in practice the same identical behavior. #2 and #3 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)