Hot answers tagged

236

C predates many of the other languages you're thinking of. A lot of what we now know about how to make programming "safer" comes from experience with languages like C. Many of the safer languages that have come out since C rely on a larger runtime, a more complicated feature set and/or a virtual machine to achieve their goals. As a result, C has remained ...


93

The programmer is responsible for ensuring that objects they created via new are deleted via delete. If an object is created, but not destroyed before the last pointer or reference to it goes out of scope, it falls through the cracks and becomes a Memory Leak. Unfortunately for C, C++ and other languages which do not include a GC, this simply piles up over ...


78

C++ does not have garbage collection. C++ applications are required to dispose of their own garbage. C++ applications programmers are required to understand this. When they forget, the result is called a "memory leak".


40

First, C is a systems programming language. So, for example, if you write a Java virtual machine or a Python interpreter, you will need a systems programming language to write them in. Second, C provides performance that languages like Java and Python do not. Typically, high performance computing in Java and Python will use libraries written in a high-...


38

In C, C++ and other systems without a Garbage Collector, the developer is offered facilities by the language and its libraries to indicate when memory can be reclaimed. The most basic facility is automatic storage. Many times, the language itself ensures that items are disposed of: int global = 0; // automatic storage int foo(int a, int b) { static ...


29

Sorry to add yet another answer, but I don't think any of the existing answers directly address your first sentence stating: 'I am considering learning C' Why? Do you want to do the kinds of things C is usually used for today (e.g. device drivers, VMs, game engines, media libraries, embedded systems, OS kernels)? If yes, then yeah, sure learn C or C++ ...


24

"Besides the fact that a higher level language is easier to code in and therefore less error prone" I really think this is a good enough reason all by itself. If you have no compelling reason to work in a low level of abstraction (such as performance, knowledge in the team, etc), then there is no reason to do it. If all you want is a coffee, then you want ...


22

C++ has this thing called RAII. Basically it means garbage gets cleaned up as you go rather than leave it in a pile and let the cleaner tidy up after you. (imagine me in my room watching the football - as I drink cans of beer and need new ones, the C++ way is to take the empty can to the bin on the way to the fridge, the C# way is to chuck it on the floor ...


21

It should be noted that it is, in the case of C++, a common misconception that "you need to do manual memory management". In fact, you don't usually do any memory management in your code. Fixed-size objects (with scope lifetime) In the vast majority of cases when you need an object, the object will have a defined lifetime in your program and is created on ...


13

This is a HUGE question with tons of answers, but the short version is that each programming language is specialized for different situations. For example, JavaScript for web, C for low level stuff, C# for anything Windows, etc. It helps to know what you want to do once you know programming to decide what programming language to pick. To address your last ...


11

In C, NULL is a macro that expands either to 0 or (void*)0 (or something that has a similar effect). In the first case, you can not differentiate between NULL and 0, because they are literally the same. In the second case, your code will cause a compile error, because you can't compare an integer variable with a pointer.


11

It is funny that you claim C is unsafer because "it has pointers". The opposite is true: Java and C# have practically only pointers (for non-native types). The most common error in Java is probably the Null Pointer Exception (cf. https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare). The second most common error is ...


10

Don't do this. Using macros to reconfigure the language like this is like writing in slang. One or two instances might not seem so bad to someone has to read it (including yourself) but every time you do it you make it that much more likely that the 'well what does that mean' effect happens. You're needlessly obscuring your code, adding a layer of mental ...


9

Because "safety" costs speed, the "safer" languages perform at a slower speed. You ask why use a "dangerous" language like C or C++, have somebody write you a video driver or the like in Python or Java, etc. and see how you feel about "safety" :) Seriously though, you have to be as close to the core memory of the machine to be able to manipulate pixels, ...


8

A fundamental difficulty with C is that the name is used to describe a number of dialects with identical syntax but very different semantics. Some dialects are much safer than others. In C as originally designed by Dennis Ritchie, C statements would generally be mapped to machine instructions in predictable fashion. Because C could run on processors which ...


8

For C, the first edition of The C Programming Language (a.k.a. K&R) suggests that your intuition about preprocessor macros is correct: Symbolic constant names are commonly written in upper case so they can be readily distinguished from lower case variable names. In many ways, this was a holdover from assembly language, where macros were defined ...


7

# means its a command for the preprocessor. C programs are run through a preprocessor before they are compiled. In this case, the preprocessor is going to include stdio.h file. <> specifies the type of include the preprocessor will do. You can also use "" here. See http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-...


7

I think what you’ve picked up on is that, in a referentially transparent language, everything has value semantics. And if you don’t read or write globals, or do I/O, then indeed this is enough to give you referential transparency. If, in addition, you don’t use local mutation, then you’ll also get more of the benefits of equational reasoning. And, for ...


7

Besides all the above, there is also one pretty common use case, which is using C as a common library for other languages. Basically, nearly all the languages have an API interface to C. Simple example, try to create a common application for Linux/IOS/Android/Windows. Besides all the tools that are out there, what we ended up was doing a core library in C, ...


7

With respect to C specifically, the language gives you no tools to manage dynamically-allocated memory. You are absolutely responsible for making sure every *alloc has a corresponding free somewhere. Where things get really nasty is when a resource allocation fails midway through; do you try again, do you roll back and start over from the beginning, do ...


5

Because the C language is designed to be implementable on any platform, no matter what set of integer sizes it natively provides. In fact, none of the intN_t types are guaranteed to exist at all. Only an implementation which provides a two's complement signed integer of exactly N bits (no padding bits allowed) will define the corresponding intN_t type (see ...


5

Historical reasons. I don't often get to write brand new code, mostly I get to maintain and extend the old stuff which has been running for decades. I'm just happy it's C and not Fortran. I can get irritated when some student says, "but why on earth do you do this awful X when you could be doing Y?". Well, X is the job I've got and it pays the bills very ...


5

Short answer: the same way any other compiler works. Long answer: A program that takes programming code input in one language and transforms it to output in a different language is called a compiler. (An assembler is a special type of compiler whose input language is assembly language and whose output language is machine code.) A compiler's work can be ...


4

Pointers are orthogonal to referential transparency. Move semantics works fine in C, although with more boilerplate than C++. No mutable global state + no function permitted to mutate it's arguments would suffice for referential transparency, even when passing by pointer. Just don't hack up anything passed in. Mutating local variables is fine - the caller ...


4

What is "dangerous"? The claim that C is "dangerous" is a frequent talking point in language flame wars (most often in comparison to Java). However, the evidence for this claim is unclear. C is a language with a particular set of features. Some of these features may allow certain types of errors that are not allowed by other types of languages (the risk ...


3

This sounds like the old CFront compiler - which compiled C++ into C: Cfront was the original compiler for C++ (then known as "C with Classes") from around 1983, which converted C++ to C; developed by Bjarne Stroustrup. The preprocessor did not understand all of the language and much of the code was written via translations. Cfront had a complete ...


3

To add to the existing answers, it's all well and good saying that you're going to choose Python or PHP for your project, because of their relative safety. But somebody's got to implement those languages and, when they do, they are probably going to do it in C. (Or, well, something like it.) So that's why people use C — to create the less dangerous ...


3

I am considering learning C There is no specific reason not to learn C but I would suggest C++. It offers much of what C does (since C++ is a super set of C), with a large amount of "extras". Learning C prior to C++ is unnecessary -- they are effectively separate languages. Put another way, if C were a set of woodworking tools, it would likely be: ...


3

If you really want to understand malloc internals, look into the source code. On Linux systems it is likely to be in GNU libc but there are other implementations of the C standard library (the musl-libc source code is nice to read) and of malloc (e.g. tcmalloc). Here is a very fast and simple malloc, but completely useless since always failing. Grossly ...


3

To answer your question, yes you could write a macro to accomplish the for loop. However, this is not a very good idea. As another user pointed out, this adds difficulty in understanding the program. Also, I have seen first hand where this can become a problem. I once witnessed a situation where a contractor used 4 or 5 levels of macros on top of one ...



Only top voted, non community-wiki answers of a minimum length are eligible