Hot answers tagged

19

The function "printf" was inherited by C from the B standard library. In B it wasn't the only such function, for example there was also a "printn" for printing numbers. See a reference for the language from an early unix version here: https://www.bell-labs.com/usr/dmr/www/kbman.html


18

I'm not very excited about either version. The second (single large expression in return statement) makes it hard to follow the control logic. The fact that the author has felt an urge to comment the code to make it clear what it does and that it doesn't fit on any reasonable line length are indicators that the expression is too complex and should be ...


16

Let's go back to the K&R roots: Origin In the tutorial chapter, on page 11 of original K&R, you'll find a hint on the origin of the function: By the way, printf is not part of the C language; there is no input or output defined in C itself. There is nothing magic about printf; it's just a useful function that is part of the standard ...


6

Most end-user applications are written in C or a close derivative of C, or another language, like Lua or BASIC or something. However, a lot of the really interesting jobs with microcontrollers require a thorough understanding of assembly, because you're writing or supporting the libraries, doing things with new parts that don't have support in a high-level ...


6

I'm not going to debug your code, there's not enough context to do this anyway, but I'm going to show you an idiom that you will probably find easier to use correctly. As a bonus, it will also be faster. Have a look at your loop body. You are allocating memory during each iteration and free it under certain circumstances depending on the overall control ...


5

Different types of UNIX have different architectures. In Linux and traditional monolithic UNIX systems the kernel is not a process. It's a block of code and data that is mapped into the memory space of every process (usually at addresses with the high bit set), but with a different I/O Privilege Level. When a process makes a system call, that triggers an ...


4

Today, the preprocessing is actually happening inside the compiler (e.g. inside the cc1plus executable started by g++ command). Use g++ -C -E to get the preprocessed form. Preprocessing and parsing is a well known art, and does not take that much time. However, the standard headers of C++11 (e.g. <vector> or <map>) are pushing a lot of stuff. ...


4

The whole point of compiling in "debug mode" is that it includes debug symbols in the executable to enable the use of a debugger, at the cost of foregoing some performance optimizations that aren't possible with debug symbols. If you don't plan on running that executable in a debugger, then "being in debug mode" doesn't gain you anything. It's likely that ...


4

You are better off to make a clear distinction between "compile time debug" mode (which is what you control by #define DEBUG), and specific "debug features" which should be available even when you compile with #define DEBUG false, and could be enabled or disabled at run time. Better call the latter differently, name the features (like "logging mode", ...


4

As far as I can determine from the public committee documents (in particular N1395), one of the major reasons for making VLA's (along with complex arithmetic and threading) optional was to make it possible to create conforming C compilers for small embedded processors. The trend was that the compiler vendors that target embedded systems were staying on the ...


4

Whenever the key is changed(by some other threads), my call back notifier is executed in which I update my static global variable with the new key. This key is used everywhere in the program to decrypt some data. What is trapping you in this kind of thinking is the concept of "the key". It should be "a key". You think of the key as global so anything ...


4

This code: return ((FD_ISSET(socket_desc, &fdset)) /* Select returned the file descriptor (socket connected or failed) */ && (getsockopt(socket_desc, SOL_SOCKET, SO_ERROR, &result, &result_len) >= 0) /* We can get the error code (if any) from the connect call */ && ...


4

It's not the most basic printing function. The most basic printing functions would be puts and putchar which print a string and char respectively. f is for formatted. printf (unlike puts or putchar) prints formatted output, hence printf. For example it can print an int in hexadecimal, or a float rounded to three decimal places, or a string left padded. ...


4

drivers for some of the custom hardware belonging to the solution are not compatible with later Windows This is the crux of the matter. You can recompile your legacy C program with a newer Visual Studio, fix up all the compiler warnings and errors, and generally turn an old system into an identical one that runs on Windows 7 (or later if you must) but ...


3

I think this is more of an issue about getting virtualbox or vmware to provide access to the hosts serial port than it is about virtualization OS or even the guest OS. I would start with a host system running a modern OS (win 7/8/10 or Linux distro) with a USB to serial port adapter that is based on a real rs-232 implementation, such as the prolific pl2303. ...


3

If you are shifting x right by b bits (x >> b), then the least significant b bits will be lost. To catch them before they're lost, you can use the bitwise and operation with a mask that contains the same number of 1 bits as you are shifting (i.e. 1 for 1 bit, 3 for 2 bits, 7 for 3 bits, 0xF for 4 bits, ... ((1 << b) - 1) for b bits). E.g lost = num ...


3

The bitwise shift-left (<<) and shift-right (>>) in higher level languages such as PHP, C or C++ lose the most or least significant bit. However, with many CPUs, the assembly instruction for shifting doesn't really lose this bit but shift it into the carry forward flag. So you could then react on this carry flag, as explained in this SO ...


2

A static global variable is the same global variable, just with a limited compile-time visibility, as @5gon12eder notices above. Global static state is prone to race conditions. If you have multiple threads accessing this state, you might need locking. If you only have one thread allowed to change the value, you may still have race conditions unless other ...


2

The priority when writing code is maintainability / readability. There are a large number of factors that can affect this. Breaking it down by example: Example 1: Positives: named variables: by naming each variable with a readable name, it is easier to understand the intent behind the usage of the variable braced structures: It is clear what ...


2

do you agree this is a good way of solving the problem? No. When you parse a shell command line, you work your way through the string, and so at any point you should already know whether you are inside quotes. Either because your parsing state machine is in some "between quotes" state, or because your recursive descent parser is in some "parse quoted ...


2

The fact that you can use a wrapper to avoid the warning should show that there isn't a deep technical reason: void bar(const char *p) { /* ... */ } void bar_w(char *p) { bar(p); } /* wrapper */ foo(bar_w); /* instead of foo(bar) */ This is based on then well known fact that you can use a pointer-to-T (for any type T) where a pointer-to-const-T is ...


2

Most of the embedded solutions are written in C. The reason being, C is a very powerful language and the user has a lot of control on hardware. On the other hand it also helps you create abstractions, however the development has to be done by the team. This is the reasons most semiconductor companies provide C/C++ compiler with their toolset. One resorts ...


1

The POSIX standard exists so that programs and utilities written by others have a chance at being transportable. Writing portable C code up until the mid 1990s was a convoluted mess of #if defined this, #elif defined that, etc. Portability back then had to be built in rather than being something you could assume. You cannot say you are POSIX-compliant, for ...


1

Your code will be compliant with POSIX if you only use functions from the POSIX standard library rather than from other non-POSIX compliant standard implementations. How you actually write and structure your code such as in your question about your loop is completely irrelevant.


1

It's a management decision. Your management should be in a position to determine how much money they are making by supporting Windows 3.11. If they have any brains they will realise that customers who complain if you don't support them for free don't actually make you money. You can support them by telling them what the cost of supporting an old version is. ...


1

You cannot know, and on a multi-core system both processes could run simultaneously (on different cores). (you should think as if (when fork succeeds) both parent and child are running exactly simultaneously; what is actually happening is an unimportant, and difficult to observe, implementation detail) Read wikipage on fork, then read carefully man page ...


1

First, C99 introduces stdbool.h and support for bool; I think you can make your code better read-/understandable by using it. 5gon12eder did already draft an appealing approach using subsequent if statements; using bool you could rephrase that like the following snippet - a pattern I apply quite often in my C++ code. bool ok = false; ok = ...


1

In principle, you could use something like your isBetweenQuotes function, but you're (probably) better served by a more structured way of parsing arguments pipelines. However, something like the following might work: int isBetweenQuotes(int pos, char *str) { return IBQplain(pos, str, 0); } int IBQplain(int pos, char *str, int offset) { char ch; ...


1

I think that between compound lists (which involve parenthesized expressions) and various operators (like |, && and ||, ...) the shell grammar is fundamentally recursive in nature, so yes, you should consider recursion up front. There are many ways to parse language, but for this grammar, any way you do it your parser needs to handle recursive ...


1

The biggest problem with a global variable is that it is global, so anyone can access and modify it at any time. That problem is immensely reduced with a static variable, because all code accessing and modifying the variable is in that one file, and hopefully under your control. (There is no such thing as a "static global" variable, that's self ...



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