C is a general-purpose computer programming language used for operating systems, games and other high performance work.

learn more… | top users | synonyms

2
votes
2answers
269 views

Design decisions while porting a non object-oriented C program to Java

Background: My boss made a comment on porting a C program that acts as some a simulator that communicates with a remote process through sockets to Java. He didn't assign it to me, or to anyone for ...
-1
votes
2answers
58 views

why use postfix /prefix expression instead of infix?

I understand how to convert infix to postfix/prefix but I do not understand why postfix or prefix expression are used in computer system? What is the advantage of postfix prefix over infix ...
1
vote
3answers
99 views

When is an object passed to a function?

Trying to clearly state the semantics of a function call. In calling a function, are the arguments passed to the function the ones the calling code initially gives or the ones the function ...
16
votes
5answers
1k views

Why use an opaque “handle” that requires casting in a public API rather than a typesafe struct pointer?

I'm evaluating a library whose public API currently looks like this: libengine.h /* Handle, used for all APIs */ typedef size_t enh; /* Create new engine instance; result returned in handle */ ...
8
votes
1answer
211 views

Why are C string literals read-only?

What advantage(s) of string literals being read-only justify(-ies/-ied) the: Yet another way to shoot yourself in the foot char *foo = "bar"; foo[0] = 'd'; /* SEGFAULT */ Inability to elegantly ...
1
vote
3answers
82 views

Are file-scope `static` variables in C as bad as `extern` global variables?

In C, you'd often/sometimes (as a matter of style) use a file-scope static variable where you'd use a private class member variable in C++. When scaling to multithreaded programs, simply adding ...
1
vote
3answers
425 views

C: “this” vs “<instance name>”

I'm using C in a minimal, efficient OOP style to build a game engine. A problem I've begun to face as the engine code settles is my (erstwhile) choice to use this. For example, I have: void ...
0
votes
1answer
41 views

What is the most efficient way to discover devices on a network?

I've been programming a C web server from scratch as an interface for controlling ESP8266 WiFi modules that I plan to use to control appliances & lighting with relays. In an attempt to make a ...
-1
votes
0answers
31 views

Different variations of C [closed]

I've heard from a few sources that C is not too portable, because it is so close to the machine compared to higher level languages like Perl, Python, Java, etc. But, I was wondering how many ...
7
votes
3answers
1k views

Is macros support in a programming language considered harmful?

The first abuse that comes to my mind in C is: #define if while But at the same time it is extremely handy and powerful when used correctly. Something similar happens with Common Lisp macros. ...
-2
votes
1answer
149 views

Question about switch-case statement [closed]

What happens, and does code work properly if we write switch-case structure like this, why its not good to write code like this? I'm porting some firmware and have switch-case statement interrupted ...
-2
votes
1answer
113 views

Copying desired bits from variables [closed]

What's the best way to copy desired bits from char, int or any other variables? I want to see all possible and the most efficient techniques for this purpose. I have for example 16bit variable 0000 ...
3
votes
3answers
149 views

How to maintain modularity in C?

I am writing C code with multiple modules like LCD display, flash memory, and GSM module etc. The project consists of thousands of lines of code, in different files. The behavior of the system can be ...
63
votes
4answers
33k views

How is a Java reference different from a C pointer?

C has pointers and Java has what is called references. They have some things in common in the sense that they all point to something. I know that pointers in C store the addresses they point to. Do ...
-3
votes
0answers
41 views

Algorithm for base conversion of arbitrary length number [closed]

Is there an efficient algorithm to convert arbitrary length number (stored as an array of digits) from one base to another? For example, base-10 number {3, 1, 9} to base-9 number {3, 8, 4}? I can ...
4
votes
5answers
672 views

Checking array size in C/C++ to avoid segmentation faults

So it's well known that C does not have any array bounds checking when accessing memory. Nowadays, if you call myArray[7] when you initialised it as int myArray[3], your program will get a segfault ...
4
votes
0answers
73 views

How to abstract from a display?

I'm building an embedded text editor consisting of a keyboard, an LCD display and a PIC32 microcontroller, to be programmed in C. The application should look, for example, like the GNU nano editor. ...
0
votes
0answers
3 views

Beginner in C needing help with strings and memory [migrated]

So this course I'm doing wants us to play around with memory management and pointers. I'm not really fully understanding them. I keep getting a "Segmentation fault (Core dumped)" error. Apparently I ...
1
vote
2answers
132 views

Complex iterators in C

note: this was originally asked on SO. Part of my current project involves iterating over sequences that don't necessarily exist. For example, I have something similar to a relational database in ...
0
votes
3answers
196 views

Understanding the concept of “arguments” and “parameters”

In the third last paragraph at page number 26 of the ebook "The C Programming Language" the author(s) say, "We will generally use parameter for a variable named in the parenthesized list in a ...
3
votes
0answers
81 views

How to define type-less string of 1-bits [migrated]

I'm writing code for a flash driver and I sometimes have to check whether or not certain locations are empty i.e. contain 0xff. Sometimes I check bytes, sometimes longer values. So I wanted to have a ...
2
votes
1answer
145 views

Endianness at bit level

I am learning the union and struct and I wrote the code below. What I do not understand is why the output is different when I change from a little endian to a big endian machine. My understanding is ...
-2
votes
2answers
2k views

Assigning strings to pointer in C Language

I am a new learner of C language, my question is about pointers. As far i learned and searched pointers can only store addresses of other variables, but cannot store the actual values(like integers or ...
1
vote
0answers
30 views

C decompress Bitmask source [migrated]

This may be somewhat of an odd question as well as my first one ever on this site and a pretty complicated thing to ask basically I have this decompresser for a very specific archived file, I barely ...
53
votes
13answers
6k views

Are data type declarators like “int” and “char” stored in RAM when a C program executes?

When a C program is running, the data is stored on the heap or the stack. The values are stored in RAM addresses. But what about the type indicators (e.g., int or char)? Are they also stored? ...
0
votes
0answers
49 views

Using the readahead syscall in C in the linux environment [migrated]

I am interested in using the readahead syscall in a c program. Currently I have opened a file for reading with a call to open(). Once I find a character, say the letter 'b', I want to test whether the ...
6
votes
3answers
442 views

How to extract the active code path from a complex algorithm

I have been puzzled lately by an intruiging idea. I wonder if there is a (known) method to extract the executed source code from a large complex algorithm. I will try to elaborate this question: ...
0
votes
2answers
77 views

Proper way to interpret this dereference operation?

I've seen this example in a text book and am a little confused how to interpret the operator precedence rules. Given this struct: typedef struct { char *data; size_t start, end; } ...
-2
votes
1answer
60 views

What's an effective threading structure for NIO and server performance in C/C++? [closed]

I run into this issue where if I do a read to check for data too often, my CPU skyrockets, and if I don't check often enough, it's slow for client performance. What's an effective way to deal with ...
5
votes
4answers
311 views

When did Undefined Behavior in C jump the causality barrier

Some hyper-modern C compilers will infer that if a program will invoke Undefined Behavior when given certain inputs, such inputs will never be received. Consequently, any code which would be ...
2
votes
0answers
73 views

Best practice for documenting ports/hardware for embedded project? [migrated]

I've recently taken an interest in embedded development and am wondering how most people document their projects. For example: Each pin on PORTE is wired to an LED and is aliased as LEDPORT. ...
27
votes
5answers
2k views

Should I initialize C structs via parameter, or by return value? [closed]

The company I work at is initializing all of their data structures through an initialize function like so: //the structure typedef struct{ int a,b,c; } Foo; //the initialize function ...
2
votes
3answers
121 views

API to get data, caller may not know how much? How to allocate buffer size

I am writing a C API for retrieving data from a hardware device. The data will be returned as a string with approximately 30 bytes of text per item. The problem is there may any number of items. ...
3
votes
5answers
608 views

Why do we have to tell printf() the type of data in C?

Let's consider this C code: #include <stdio.h> main() { int x=5; printf("x is "); printf("%d",5); } In this, when we wrote int x=5; we told the computer that x is an integer. The ...
2
votes
2answers
156 views

Why dynamic memory allocation functions in C returns void*?

Consider the prototypes of C's dynamic allocation functions malloc - void* malloc(size_t size); calloc - void* calloc(size_t n,size); realloc - void* realloc(void* ptr,size_t newsize); Now a ...
2
votes
6answers
729 views

Replacing C with JavaScript as an introductory programming language [closed]

I am a new teacher at polytechnic where we teach web development and basic software programming. For years, the institution where I teach have taught C as their introductory programming language to ...
13
votes
10answers
1k views

Does low latency code sometimes have to be “ugly”?

(This is mainly aimed at those who have specific knowledge of low latency systems, to avoid people just answering with unsubstantiated opinions). Do you feel there is a trade-off between writing ...
2
votes
0answers
243 views

Fast fixed-size (256 bit) integer modulo/division

What is the fastest way to divide two 256-bit integers? They are represented as an array of 26-bit words, each stored in 32-bit integers. I have access to a 64-bit type if that's useful. It seems ...
20
votes
5answers
94k views

Is initializing a char[] with a string literal bad practice?

I was reading a thread titled "strlen vs sizeof" on CodeGuru, and one of the replies states that "it's anyways [sic] bad practice to initialie [sic] a char array with a string literal." Is this ...
0
votes
0answers
55 views

A callback for when data is received?

So, my understanding is that the Kernel could receive TCP at any time, and that generally a C program asks the Kernel synchronously/asynchronously if it has data available. Would it be possible for a ...
1
vote
1answer
156 views

What would be a reason not to embed JS in C? [closed]

I'm working on an opensource application that processes JSON in C, because I would like to make it easier for people to help me in the project and because JSON is so close to JS. I want to embed a JS ...
1
vote
2answers
63 views

What should JITed bytecode do exactly?

I'm working on a VM (and a scripting language for it) that I plan to implement JITing for. I'm only working on the "plumbing" of it now, but I don't want the JIT compiler to be an afterthought. ...
2
votes
1answer
150 views

What is the motivation for casting a pointer into a integer?

I'm doing some changes in the Linux kernel code and have noticed a pointer being cast into integer. Check out buf below (full code): snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream ...
21
votes
12answers
6k views

Why does void in C mean not void?

In strongly-typed languages like Java and C#, void (or Void) as a return type for a method seem to mean: This method doesn't return anything. Nothing. No return. You will not receive anything from ...
2
votes
2answers
408 views

How small is the footprint of a small C compiler?

This week I could optimize using a reduced C library that allowed a drastic shrinkage in code size - from about 60 K to about 6 K and then we could load the code in the 8 K on-chip memory of an FPGA ...
0
votes
2answers
171 views

In C, how are functions accessible if they're not inline or called by #include? [closed]

I'm looking at code that somehow calls a library but I don't see it calling any of the library's headers at any point. I've done searches within the code and I don't see anywhere the functions, ...
1
vote
2answers
82 views

Multi-pattern matching

I have a given byte array in C and I need to match it against several byte arrays and return true if there is any match. I can make various memcmp's but I feel that it is very inefficient. Do you ...
12
votes
1answer
360 views

Why using sizeof of type to calculate the number of elements in an array is considered inferior to using sizeof of an element?

I'm reading "King K.N's C programming" and I'm reading the following statement: We discussed using the expression sizeof(a)/sizeof(a[0]) to calculate the number of elements in an array. The ...
3
votes
3answers
368 views

A list of the most important areas to examine when moving a project from x86 to x64? [closed]

I know to check for/use asserts and carefully examine any assembly components, but I didn't know if anyone out there has a fairly comprehensive or industry standard check-list of specific things at ...
70
votes
15answers
25k views

What makes C so popular in the age of OOP? [closed]

I code a lot in both C and C++, but did not expect C to be the second most popular language, slightly behind Java. TIOBE Programming Community Index I'm curious as to why, in this age of OOP, C is ...