Tag Info

Hot answers tagged

20

Historically, everything that abstracts over assembly code was called high-level. C certainly does that. This definition is also relatively clear-cut, in contrast to what follows. Over time, we created more and more programming languages and invented more and more abstractions and tools. Compared to, say, Python, the C language is positively primitive in ...


10

It is a higher level language than machine code (assembly), which is the point of view that C programming books and tutorials come from. In that respect it is a high level programming language. However, it is still very close to the hardware - much more so than other, more modern languages (Java, C# and such) - when viewed from this point of view, it is a ...


9

Using macros to create code that's syntactically identical to C but semantically different is almost always evil, especially in an unsafe language like C where the fact that something compiles in no way guarantees it actually has defined behavior at run time. If you must use a macro for something, you generally want to make it clear that a macro is being ...


8

Simple objects are just a struct containing all the fields. Methods typically take a pointer to that struct as their first argument (like python does). This simple form of OOP is done all the time in C. Inheritance makes things more complicated. You can add a pointer to the parent object from the child object, or include all the parent's fields. Virtual ...


5

Since you specifically mentioned getchar, putchar and EOF, I am going to explain those. putchar is a function that prints a character to the console. Here is Hello World using putchar: #include <stdio.h> int main() { putchar('h'); putchar('e'); putchar('l'); putchar('l'); putchar('o'); putchar('\n'); putchar('w'); ...


5

In practice, you'll see int accumulate( int n, int *array) most often. It's the most flexible (it can handle arrays of different sizes) and most closely reflects what's happening under the hood. You won't see int accumulate( int (*array)[N] ) as often, since it assumes a specific array size (the size must be specified). If your compiler supports ...


5

According to the C++ standard, §4.5 ad. 6 (On integral promotions): A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one. According to the C++ standard, §4.7 ad. 4 (On integral conversions): If the destination type is bool, see 4.12. If the source type is bool, the value false is ...


4

What you suggest is possible with more modern languages. C was invented at a time when compiler technology was far too primitive to allow this kind of programmable compilation pipeline. Furthermore, most programmers knew of no use for such a mechanism. Even today it's difficult to construct a compiler that can permit such programming. It's easy to specify ...


3

English (and I presume other languages as well) has a lot of words which can be used in a generic sense, but are no longer correct in a context which requires more distinction. For example, you can use the word "goose" to refer to a goose of any gender or age when speaking in the more common generic sense, but if a distinction must be made, a male goose is ...


3

Learning C will only help you in learning languages which are very much like C. But then, what do you gain from you learning them? The further away from C the language you want to learn is, the less learning C will help you. In particular, C is missing a lot of concepts, paradigms and ideas that are present in more modern languages: first-class procedures, ...


3

C nowadays is used primarily in situations in which a fast compiled language is required that is not overly complicated (classes not required). This includes: Hardware drivers Pixel shaders (variant of C, Cg) In general, programs in which speed is critical Knowing C well is paramount for knowing how programming languages work with the operating system, ...


2

In C, when the array notation is used for a function parameter, it is automatically transformed into a pointer declaration, so declaring parameter as int* array and int array[] are equivalent. I tend to use second one because it is more clear that function expects an array as an argument. Declaring function parameter as int (*array)[] is not equivalent to ...


2

Learning C or something similar, such as Pascal, may help you to understand how the compiler, linker, and run-time libraries work together, so you can better understand the low level details of computer systems. This was my experience when comparing the output of a C compiler to assembly code. C will not help much with higher level language ideas, which are ...


2

I believe the second half of the mergesort algorithm is what you're looking for, since the first half is splitting the numbers into subranges, and the second half is merging those subranges. Mergesort as a whole has a complexity of O(n log n), and I believe the second half by itself would also be O(n log n). To get you started, here's a reasonably short and ...


1

What you need for that is called operator overloading, which is not supported in C. See the question here: http://stackoverflow.com/questions/3417413/operator-overloading-in-c You could write it in C++ though ...


1

The term "argument" is often used rather loosely to refer to either actual arguments or formal arguments without giving the actual or formal adjective. Most often, the context makes it clear if actual arguments or formal arguments/parameters is meant. In this case, it should be clear that the authors meant formal arguments or parameters. As to why they ...


1

From what you wrote, I don't see a reason why original information about the hashtable and the buckets has to be used - it just overcomplicates things. So flatten all buckets into a list (complexity O(N)) and apply an arbitrary sorting algorithm with complexity O(N * log(N)), for example, quick sort or merge sort. Then write the results into a file.


1

You can make a min-heap where you store all buckets sorted by the first element. Then you pop an element from the first bucket and re-heapify. If you are not allowed to change the buckets then keep an index per bucket of which element still has to be popped from it.


1

C may be selected as a development language for any number of reasons: Ecosystem C is the development language of choice on some operating systems - notably Unix platforms and their variants. Low level integration If you need to inline assembler for whatever reason, C would be a natural choice. Cross-platform C can be used to write platform independent ...


1

The first declaration also allows you write the function differently: int accumulate(int n, int *array) { int sum = 0; while (n-- > 0) sum += *array++; return sum; } so you don't need the variable i. Whatever's idiomatic to the code base should be preferred, followed by whatever's the easiest to understand, followed at some distance by ...


1

You say that there are 3 ways in C and C++, but C++ actually makes a fourth available: template<std::size_t n> void arrayFunction(std::array<int,n> &array) { ...} This has several advantages over the solutions you suggest: The parameter for the size of the array will be automatically determined on use by the compiler, meaning you don't ...


1

My question is: Would you recommend using this in a serious project? Yes! Here is something that is mostly true about library functions: they necessarily have to be generally applicable in order to accommodate many users with different requirements. If you know the underlying algorithm, the library function is going to be larger and slower than your ...


1

The short answer is C needs types because of history / representing the hardware. History: C was developed in the early 1970s and intended as a language for systems programming. Code is ideally fast and makes the best use of the capabilities of the hardware. Inferring types at compile time would have been possible, but the already slow compile times would ...


1

I see two reasons why a separate initializiation makes sense. First, there is the the "Single Level of Abstraction" principle, which helps to make code more readable and more evolvable. This means, if type_init and type_free are a pair of associated functions, it makes perfectly sense to call them at the same level whenever possible. The second reason is ...



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