I come from a scripting background and the preprocessor in C has always seemed ugly to me. None the less I have embraced it as I learn to write small C programs. I am only really using the preprocessor for including the standard libraries and header files I have written for my own functions.  My question is why don't C programmers just skip all the includes and simply concatenate their C source files and then compile it? If you put all of your includes in one place you would only have to define what you need once, rather than in all your source files.  Here's an example of what I'm describing. Here I have three files:  // includes.c #include &lt;stdio.h&gt;     // main.c int main() {     foo();     printf("world\n");     return 0; }     // foo.c void foo() {     printf("Hello "); }   By doing something like cat *.c &gt; to_compile.c &amp;&amp; gcc -o myprogram to_compile.c in my Makefile I can reduce the amount of code I write.  This means that I don't have to write a header file for each function I create (because they're already in the main source file) and it also means I don't have to include the standard libraries in each file I create. This seems like a great idea to me!  However I realise that C is a very mature programming language and I'm imagining that someone else a lot smarter than me has already had this idea and decided not to use it. Why not?
11434
I recently had cause to work with some Visual Studio C++ projects with the usual Debug and Release configurations, but also 'Release All' and 'Debug All', which I had never seen before. It turns out the author of the projects has a single ALL.cpp which #includes all other .cpp files. The *All configurations just build this one ALL.cpp file. It is of course excluded from the regular configurations, and regular configurations don't build ALL.cpp I just wondered if this was a common practice? What benefits does it bring? (My first reaction was that it smelled bad.) What kinds of pitfalls are you likely to encounter with this? One I can think of is if you have anonymous namespaces in your .cpps, they're no longer 'private' to that cpp but now visible in other cpps as well? All the projects build DLLs, so having data in anonymous namespaces wouldn't be a good idea, right? But functions would be OK?
74152
You can structure a C++ program so that (almost) all the code resides in Header files. It essentially looks like a C# or Java program. However, you do need at least one .cpp file to pull in all the header files when compiling. Now I know some people would absolutely detest this idea. But I haven't found any convincing downsides of doing this. I can list some advantages:  [1] Faster compile times. All header files only get parsed once, because there is only one .cpp file. Also, one header file cannot be included more than once, otherwise you will get a build break. There are other ways of achieving faster compiles when using the alternate approach, but this is so simple.  [2] It avoids circular dependencies, by making them absolutely clear. If ClassA in ClassA.h has a circular dependency on ClassB in ClassB.h, I have to put a forward reference &amp; it sticks out. (Note that this is unlike C# &amp; Java where the compiler automatically resolves circular dependencies. This encourages bad coding practices IMO). Again, you can avoid circular dependencies if your code was in .cpp files, but in a real-world project, .cpp files tend to include random headers until you can't figure out who depends on whom.  Your thoughts?
2571
What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? And how about the arguments? If int main() then return 1 or return 0?  There are numerous duplicates of this question, including:       — For C++, with a very good answer indeed.     Related:
26641
I have heard of some methods, but none of them have stuck. Personally I try to avoid complex types in C and try to break them into component typedef.  I'm now faced with maintaining some legacy code from a so called 'three star programmer', and I'm having a hard time reading some of the ***code[][].  How do you read complex C declarations?
107627
Is it the same convention as those used in known programming languages such as Java and C? I'm trying to create a lot of variables and I want it to start with a number, but I'm not sure if it's okay.
16043
I've been having a problem with compiling.   I used the following test C code below and saved it as test.c in Home folder.  #include &lt;stdio.h&gt; int main() {   printf("Bruh!");   return 0; }   When I run in terminal the code   gcc test.c -o test   I get  nothing.  Thank you
45531
For example, I rarely need:  using System.Text;   but it's always there by default. I assume the application will use more memory if your code contains unnecessary . But is there anything else I should be aware of?  Also, does it make any difference whatsoever if the same using directive is used in only one file vs. most/all files?    Edit: Note that this question is not about the unrelated concept called a , designed to help one manage resources by ensuring that when an object goes out of scope, its  method is called. See .
13904
I'm doing some architectural cleanup that involves moving a bunch of classes into different projects and/or namespaces. Currently I'm moving the files by hand, building, and then manually adding using Foo statements as needed to resolve compilation errors. Anyone know of a smarter way of doing this? (We're a CodeRush and Refactor! shop, but I'd be interested to hear if Resharper has support for this)
51505
In the process of learning tinyos I have discovered that I am totally clueless about makefiles.  There are many optional compile time features that can be used by way of declaring preprocessor variables.  To use them you have to do things like:  CFLAGS="-DPACKET_LINK"        this enables a certain feature.  and    CFLAGS="-DPACKET_LINK" "-DLOW_POWER" enables two features.  Can someone dissect these lines for me and tell me whats going on? Not in terms of tinyos, but in terms of makefiles!
82685
I checked out a copy of a C++ application from SourceForge (HoboCopy, if you're curious) and tried to compile it.  Visual Studio tells me that it can't find a particular header file. I found the file in the source tree, but where do I need to put it, so that it will be found when compiling?    Are there special directories?
124139
I have a class which has many small functions. By small functions, I mean functions that doesn't do any processing but just return a literal value. Something like:  string Foo::method() const{     return "A"; }   I have created a header file "Foo.h" and source file "Foo.cpp". But since the function is very small, I am thinking about putting it in the header file itself. I have the following questions:   Is there any performance or other issues if I put these function definition in header file? I will have many functions like this. My understanding is when the compilation is done, compiler will expand the header file and place it where it is included. Is that correct?
5812
Why does the  exist in modern languages such as C# and Java? Is it purely a self documenting code feature, or does it serve any actual purpose?  For instance the following example:  Class1 obj = new Class1();  Class1 foo() {     return new Class1(); }   Is as easy to read as the more Pythonesque way of writing it:  Class1 obj = Class1();  Class1 foo() {     return Class1(); }   EDIT:  hit the nail on the head with the clarification of the question: Why did they choose this syntax?
56146
I long thought that in C, all variables had to be declared at the beginning of the function. I know that in C99, the rules are the same as in C++, but what are the variable declaration placement rules for C89/ANSI C?  The following code compiles successfully with gcc -std=c89 and gcc -ansi:  #include &lt;stdio.h&gt; int main() {     int i;     for (i = 0; i &lt; 10; i++) {         char c = (i % 95) + 32;         printf("%i: %c\n", i, c);         char *s;         s = "some string";         puts(s);     }     return 0; }   Shouldn't the declarations of c and s cause an error in C89/ANSI mode?
45776
I'm trying to do something like the following:  enum E;  void Foo(E e);  enum E {A, B, C};   which the compiler rejects.  I've had a quick look on Google and the consensus seems to be "you can't do it", but I can't understand why.  Can anyone explain?  Clarification 2: I'm doing this as I have private methods in a class that take said enum, and I do not want the enum's values exposed - so, for example, I do not want anyone to know that E is defined as  enum E {     FUNCTIONALITY_NORMAL, FUNCTIONALITY_RESTRICTED, FUNCTIONALITY_FOR_PROJECT_X }   as project X is not something I want my users to know about.  So, I wanted to forward declare the enum so I could put the private methods in the header file, declare the enum internally in the cpp, and distribute the built library file and header to people.  As for the compiler - it's GCC.
26941
I have the following makefile that I use to build a program (a kernel, actually) that I'm working on. Its from scratch and I'm learning about the process, so its not perfect, but I think its powerful enough at this point for my level of experience writing makefiles.  AS  =   nasm CC  =   gcc LD  =   ld  TARGET      =   core BUILD       =   build SOURCES     =   source INCLUDE     =   include ASM         =   assembly  VPATH = $(SOURCES)  CFLAGS  =   -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions \             -nostdinc -fno-builtin -I $(INCLUDE) ASFLAGS =   -f elf  #CFILES     =   core.c consoleio.c system.c CFILES      =   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) SFILES      =   assembly/start.asm  SOBJS   =   $(SFILES:.asm=.o) COBJS   =   $(CFILES:.c=.o) OBJS    =   $(SOBJS) $(COBJS)  build : $(TARGET).img  $(TARGET).img : $(TARGET).elf     c:/python26/python.exe concat.py stage1 stage2 pad.bin core.elf floppy.img  $(TARGET).elf : $(OBJS)     $(LD) -T link.ld -o $@ $^  $(SOBJS) : $(SFILES)     $(AS) $(ASFLAGS) $&lt; -o $@  %.o: %.c     @echo Compiling $&lt;...     $(CC) $(CFLAGS) -c -o $@ $&lt;  #Clean Script - Should clear out all .o files everywhere and all that. clean:     -del *.img     -del *.o     -del assembly\*.o     -del core.elf   My main issue with this makefile is that when I modify a header file that one or more C files include, the C files aren't rebuilt. I can fix this quite easily by having all of my header files be dependencies for all of my C files, but that would effectively cause a complete rebuild of the project any time I changed/added a header file, which would not be very graceful.  What I want is for only the C files that include the header file I change to be rebuilt, and for the entire project to be linked again. I can do the linking by causing all header files to be dependencies of the target, but I cannot figure out how to make the C files be invalidated when their included header files are newer.  I've heard that GCC has some commands to make this possible (so the makefile can somehow figure out which files need to be rebuilt) but I can't for the life of me find an actual implementation example to look at. Can someone post a solution that will enable this behavior in a makefile?  EDIT: I should clarify, I'm familiar with the concept of putting the individual targets in and having each target.o require the header files. That requires me to be editing the makefile every time I include a header file somewhere, which is a bit of a pain. I'm looking for a solution that can derive the header file dependencies on its own, which I'm fairly certain I've seen in other projects.
6641
How far do you go with const?  Do you just make functions const when necessary or do you go the whole hog and use it everywhere?  For example, imagine a simple mutator that takes a single boolean parameter:  void SetValue(const bool b) { my_val_ = b; }   Is that const actually useful?  Personally I opt to use it extensively, including parameters, but in this case I wonder if it's worthwhile?  I was also surprised to learn that you can omit const from parameters in a function declaration but can include it in the function definition, e.g.:  .h file  void func(int n, long l);   .cpp file  void func(const int n, const long l)   Is there a reason for this? It seems a little unusual to me.
56863
In   asserted:     I would advice you not to use prototypes. They have their uses, but not for most cases and definitely not in this one.   Why might this be true (or otherwise)? I almost always supply prototypes for my Perl functions, and I've never before seen anyone else say anything bad about using them.