Signedness of enum in C/C99/C++/C++x/GNU C/GNU C99
An enum is guaranteed to be represented by an integer, but the actual type (and its signedness) is implementation-dependent.
You can force an enumeration to be represented by a signed type by giving one of the enumerators a negative value:
enum SignedEnum { a = -1 };
In C++0x, the underlying type of an enumeration can be explicitly specified:
enum ShortEnum : short { a };
(C++0x also adds support for scoped enumerations)
For completeness, I'll add that in The C Programming Language, 2nd ed., enumerators are specified as having type int
(p. 215). K&R is not the C standard, so that's not normative for ISO C compilers, but it does predate the ISO C standard, so it's at least interesting from a historical standpoint.
Which header should I include for `size_t`?
Assuming I wanted to minimize the functions and types I was importing I'd go with cstddef
as it doesn't declare any functions and only declares 6 types. The others focus on particular domains (strings, time, IO) that may not matter to you.
Note that cstddef
only guarantees to define std::size_t
, that is, defining size_t
in namespace std
, although it may provide this name also in the global namespace (effectively, plain size_t
).
In contrast, stddef.h
(which is also a header available in C) guarantees to define size_t
in the global namespace, and may also provide std::size_t
.
What is the difference between g++ and gcc?
gcc
and g++
are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler).
Even though they automatically determine which backends (cc1
cc1plus
...) to call depending on the file-type, unless overridden with -x language
, they have some differences.
The probably most important difference in their defaults is which libraries they link against automatically.
According to GCC's online documentation link options and how g++ is invoked, g++
is equivalent to gcc -xc++ -lstdc++ -shared-libgcc
(the 1st is a compiler option, the 2nd two are linker options). This can be checked by running both with the -v
option (it displays the backend toolchain commands being run).
How do I find a factorial?
This will work for the factorial (although a very small subset) of positive integers:
unsigned long factorial(unsigned long f)
{
if ( f == 0 )
return 1;
return(f * factorial(f - 1));
}
printf("%i", factorial(5));
Due to the nature of your problem (and level that you have admitted), this solution is based more in the concept of solving this rather than a function that will be used in the next "Permutation Engine".
Using true and false in C
Just include <stdbool.h>
if your system provides it. That defines a number of macros, including bool
, false
, and true
(defined to _Bool
, 0, and 1 respectively). See section 7.16 of C99 for more details.
Related Topics
Library Is Linked But Reference Is Undefined
Make a File Pointer Read/Write to an In-Memory Location
Is Stl Vector Concurrent Read Thread-Safe
C++11: the Difference Between Memory_Order_Relaxed and Memory_Order_Consume
Passing Member Functions to Std::Thread
C++ Concatenate Two Int Arrays into One Larger Array
How to Serialize Derived Template Classes with Boost.Serialize
How to Add Static Libraries to a Visual Studio Project
Convert a C++ Program to a Windows Service
What Is the Ndebug Preprocessor MACro Used for (On Different Platforms)
Why Does Valgrind Say Basic Sdl Program Is Leaking Memory
Switch "Transfer of Control Bypasses Initialization Of:" When Calling a Function
No Matching Function for Call to Class Constructor
What's the Easiest Way to Generate Xml in C++
Do Stl Iterators Guarantee Validity After Collection Was Changed