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
4answers
1k views

Can a C struct behave like it had a function?

I use C and structs where a struct can have members but not functions. Assume for simplicity that I want to create a struct for strings that I name str and I want to be able to do str.replace(int i, ...
1
vote
2answers
174 views

Are global variables okay to use in asynchronous programs?

This may be a stupid question, sorry. I've read about the drawbacks of global variables a lot on this site. I'm finally trying to increase my code quality for a large project that'll be reviewed by ...
1
vote
1answer
57 views

Using GPLed headers in LGPLed library

For a specific device there exists a Linux kernel module. This module only initialized the device and provides control mechanisms to user land via ioctls and mmap. To use this the device properly one ...
2
votes
2answers
98 views

algorithm for shell language interpreter to find if a char is between quotes

Assume we have a string s (a C char *) that is a program in a language L. I want to parse L and know the following from the specification The following characters must be quoted if they are to ...
-4
votes
0answers
27 views

Strassen Recursive call not working [on hold]

This is my code for strassen recursive matrix multiplication. the code works fine for (2x2)matrix multipication as well as 6x6 but for even sized matrix such as 4,8,and so on, the last(or the ...
5
votes
0answers
2k views

Why would someone use this type of cast in c? The reference of a float is cast to int pointer and then dereferenced [migrated]

I was reading about Carmack's fast inverse square root algorithm, and noticed this: float x; // ... // int i = *(int*)&x; Why would someone choose to use this weird type of casting instead of ...
3
votes
6answers
238 views

How efficient is malloc and how do implementations differ?

If I use malloc, does malloc always use the same algorithm regardless of what it is allocating or does it look at the data and select an appriopriate algorithm? Can we make malloc faster or smarter ...
-2
votes
0answers
98 views

How to rate your programming skills [closed]

I am an Electrical Electronic Engineering student and I started programming 2 years ago. I am about to graduate from University and I cant tell how skillful I am in programming. I have written a ...
9
votes
1answer
143 views

Idiomatic wrapping of C++ template type API in C

I'm working on wrapping a C++ API which provides access to a data store (Hazelcast) in C functions, so that the data store can also be accessed from C-only code. The Hazelcast C++ API for the Map ...
-3
votes
0answers
41 views

Assignment makes pointer from integer without a cast. C [closed]

I have this error: warning: assignment makes pointer from integer without a cast in line 6. Inicial is a string and x a character char top; void push (char* stack[], char x) { top=top+1; ...
2
votes
1answer
33 views

Configuration related defines in public header?

Suppose you're building some library, C or C++ doesn't really matter for this question, IMO. The features (or implementation thereof) depend on capabilities of the target system. A simple, probably ...
2
votes
2answers
120 views

memory allocation in C while calling function

I have a really basic question regarding how memory gets allocated in a code written in C. Let's say that I have something like this: int pointless(int a); int main(){ int num1,num2; ...
-1
votes
5answers
124 views

Range of values that can be stored in an integer type in C

C has family of integer types i.e {short, int, long, long long}. Any new programmer is likely to use int to represent integer variable in the application and since int type has 32 bit space with range ...
1
vote
1answer
142 views

Unit testing C code?

I learnt about the check testing framework today that seems good. This far I've scripted tests that uses valgrind so that the tests both display output from the tests and from valgrind. Is there a ...
1
vote
3answers
66 views

Difference between passing by values/address and returning a values through functions?

According to a book I read you can pass variables from one function to another by passing by value/address. When you pass by address it will no longer preserve the variable if it is changed in a ...
0
votes
0answers
27 views

Help me understand this flow for recursive function [duplicate]

This recursive function blows my mind a bit because it executes the recursive calls until it hits 1. It returns n, but afterwards it does the multiplication below it four times and returns the final ...
0
votes
0answers
37 views

GUI - engine data exchange, design with C, getters or struct pointers passing?

Having quite big codebase and external libraries, in C application, what would be pros and cons of two approaches (or suggest other): (assume that there are NONE api provided calls for this) Edit ...
3
votes
2answers
565 views

Why do binary files load quicker than alphanumeric text files?

I've noticed that when I load/store large data files in a binary format, the program runs much faster than if I load data from an ASCII encoded file. Why is this the case? The data in my case is ...
0
votes
1answer
27 views

DNS-SD implementation

I am trying to implement DNS-SD, and I understand how it works, but don't really understand the purpose, so I have a few questions about it. Let us assume that we have two devices on local network, ...
7
votes
3answers
1k views

Why isn't C's most basic printing function named `print` instead of `printf`? [duplicate]

AFAIK, C does not have a print function. The most basic we can get is printf. So, is there a reason why this function is not simply named print, instead of printf? Note that this question is about ...
14
votes
3answers
266 views

Supporting development for older OS

I am maintaining a large portion of legacy code, written in C. This code was initially written to be comiled against Windows 3 for Workgroups, and later a version for NT was created. This legacy ...
2
votes
1answer
204 views

When to malloc and free?

Valgrind does not report a memory leak during my actual usage, only during my scripted test that I scripted with a shell script to test my own shell. I found that I didn't have to use malloc every ...
1
vote
2answers
163 views

What are microcontrollers programmed in most in industry? C or Assembly? [closed]

So recently in school we have started programming klm25z boards using ARM Assembly. I was wondering if in most industry jobs, microcontrollers are really programmed in assembly over C. It seems to ...
2
votes
0answers
82 views

What are the benefits of an input/output component design?

For the company I work at, all of our projects, including a new one started last year, are written in C89. We write for vxWorks (a real time embedded operation system). Our software runs ...
-1
votes
0answers
67 views

Please let me know more refactoring strategies for my codebase?

I have written a shell command-line interpreter which uses loops and recursion (but maybe not "recursive decent", that usually solves this problem) I want to refactor the function to 4 or 5 smaller ...
28
votes
7answers
3k views

C++ skills higher than C skills? [closed]

I feel that the often seen C/C++ doesn't really describe my skills in my CV. So I'm planning to separate it into advanced C++ knowledge and mediocre C skills. Do you think this is confusing for the ...
8
votes
4answers
4k views

GO instead of C/C++ with cgo [closed]

Is it possible to replace C and C++ with Go + cgo and interface frameworks such as Qt or DirectX with Go? Any potential pitfalls I should be aware of? UPDATE: I see that my question was not precise ...
-1
votes
0answers
6 views

How to declare parameters in this case [migrated]

If I have a variable that is declared like this char *string[z][z]; And I want to factor out a for loop from a very large function where the above variable is included, how should the call look ...
-1
votes
1answer
55 views

During fork system call which execute first parent or child

As i am executing a one pipe example pgm from libc.pdf,I noticed that the parent process first completed then only child process completed.From my knowledged about os the child process must complete ...
-1
votes
2answers
59 views

Run C program in debug mode (without debugger)

If I want my program to be able to run in debug mode, is it a good idea to make it accept a flag such as -D=DEBUG when I run the program? I currently have a DEBUG variable in a .h file but that I ...
-2
votes
1answer
154 views

For loop and recursion for a new shell in C [closed]

I code a new shell in C, that could be done in several ways: Flex/bison, finite state machine, abstract syntax tree or just a tokenizer in C. Now I've written a for-loop that changes the condition of ...
128
votes
7answers
153k views

When do you use float and when do you use double

Frequently in my programming experience I need to make a decision whether I should use float or double for my real numbers. Sometimes I go for float, sometimes I go for double, but really this feels ...
8
votes
2answers
1k views

Why use typedefs for structs?

in C (ANSI, C99, etc.), structs live in their own namespace. A struct for a linked list might look something like this: struct my_buffer_type { struct my_buffer_type * next; struct ...
0
votes
1answer
307 views

One font OCR for meme images

I'm coding an Optical Character Recognition specifically for the Internet meme images. This is a school project and it should be coded in C. I'm currently having trouble with the method/algorithms I ...
1
vote
5answers
6k views

How to share memory between applications written in C/C++

I'm going through a program written in C/C++ for control in robotics. Basically, three different programs run at the same time, and they communicate via shared memory. Google-ling around I found ...
2
votes
1answer
160 views

Is it impossible to write proper, const correct callbacks in C?

In C, a non const object implicitly converts, without warning, to its const counterpart, so you can write a function, and call it as: void foo( const char *p ); char bar[] = "bar"; foo( bar ); ...
2
votes
4answers
326 views

Best and safest API for a function which fills a buffer with variable-length data

I have a function which receives a buffer and returns some data in the buffer. The data can be smaller than the buffer capacity. Which is the best and safest API for this? int fn(void *buffer, ...
2
votes
4answers
494 views

Why is it called a memory leak?

I am a hobbyist programmer, bit of a stickler for terminology, currently learning C and recently came across the concept of Memory Leak. Now, I do understand what it means. Dynamic memory allocated to ...
1
vote
1answer
140 views

Does having so many macros (#define) increase compilation time due to prolonged pre-processing?

In a very simplistic way, I understand: "Compilation" = "Pre-processing" + "Parsing" + "Linking" + "Executable" All the macros and other such pre-processing directives are taken care at the ...
0
votes
5answers
1k views

Algorithm for recursive evaluation of postfix expressions

I'm reading Sedgewick's book on algorithms in C and I'm looking for an algorithm to evaluate postfix expressions (addition and multiplication only) without using a stack. I tried to implement one but ...
4
votes
4answers
339 views

Coding style: Binary logic or multiple if()s?

Something that often comes up in code reviews is the structure of the code, particularly related to the use of control structures, where individuals can be fairly opinionated on what is "right" and ...
9
votes
2answers
327 views

Why were variable length arrays made optional in C 2011?

When VLAs were introduced in C 1999, I thought it was a great innovation to the language. However, having learned it was made optional in C 2011, I am wondering what led to its change in status, and ...
2
votes
3answers
129 views

Is there any low level way to get shifted or unshifted bits which results from bitwise operations?

I was playing with bitwise operations and a question about counting true bits of any positive integer value, so I solved the problem with bit shifting, so I just thought if there would be some way to ...
2
votes
4answers
231 views

Are static global variables as bad as global variables? How to avoid using them?

In general, I know that global variables are bad and their should be avoided. Are static global variables equally bad? In all my projects, I have heavily relied on static global variables. From ...
65
votes
10answers
10k views

Why do C arrays not keep track of their length?

What was the reasoning behind not explicitly storing an array's length with an array in C? The way I see it, there are overwhelming reasons to do so but not very many in support of the standard ...
4
votes
1answer
81 views

Kernel operation

I'm trying to grasp the concept of an operating system at the moment (on Unix-like machines) The kernel is the process with PID 0. Of course the Kernel is not really "just another process" because a ...
1
vote
1answer
97 views

Memory usage of global versus local variables

I am a beginner in C coding. I'm writing a C code where a variable is required to be used by different functions. I want to ask whether to use a global variable and manipulate it through functions or ...
19
votes
1answer
456 views

Where did “exit(-1)” come from?

I see in a lot of legacy software and bad tutorials on the Internet that recommend using exit(-1), return -1 or similar to represent "abnormal termination". The problem is, in POSIX at least, -1 has ...
38
votes
3answers
132k views

Android development using C and C++ [closed]

I am a C, C++ developer. I am interested in mobile development. I want to know how can I develop Android apps using C and C++, I have read that they are providing a kit for C, C++ developers but it ...
9
votes
3answers
328 views

When should a trig function, with a degree argument, return -0.0?

In creating trig functions my_sind(d), my_cosd(d), my_tand(d), that used a degree argument rather than a radian one and provided exact answers at multiples of 90, I noticed that the result was ...