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
110 views

Returning void from a function specified to return int [on hold]

int count_to_ten ( int count ) { /* we only keep counting if we have a value less than ten*/ if ( count < 10 ) { printf("%d",count); count_to_ten( count + 1 ); } return; } int ...
-3
votes
0answers
34 views

Output of a program [migrated]

{ int a; a=4; cout<<a++<<++a; } The output of the above C++ program is 56 but why it is not 46 As a++ first print the value then increment a.
0
votes
3answers
101 views

Looking for a certain datatype if it exists

I'm looking for the kind of a certain datatypes. The main idea of this datatype is to describe a productionline containing several steps. Each step should be represented as a "vertex" containing an ...
1
vote
2answers
327 views

Is there a better way to organize my module tests that avoids an explosion of new source files?

I've got a neat (so I thought) way of having each of my modules produce a unit-test executable if compiled with the -DTESTMODULE flag. This flag guards a main() function that can access all static ...
-2
votes
0answers
139 views

How to call a function with argc & argv? [migrated]

I'm currently trying to create a GUI for an existing command line program using QT, as a learning exercise. The original code was written in c, and I have included the c wrapper for the c++ bit. I ...
0
votes
0answers
6 views

Pointer to Array of Strings as CONST? [migrated]

i program a application with mikroC PRO for AVR on the Hardware mikromedia for XMEGA. I have different languages to show on a TFT Display. I must switch the languages at runtime. This works the ...
0
votes
3answers
86 views

Does it make sense to have a separate init function?

I'm created an opaque type and I have two options for its management. The first looks like this: type_t *a = type_init(); int err; err = type_do(a, "foo", "bar", FLAGBIT1|FLAGBIT2, NULL); if (!err) ...
-6
votes
1answer
73 views

C program for Tree Traversal without Recursion [on hold]

I want to write a C function for Tree Traversal without Recursion. The program that involves recursion is too simple, I want to do the same thing without recursion. I know the algorithm but couldn't ...
5
votes
3answers
254 views

Undefined behavior, in principle

Whether in C or C++, I think that this illegal program, whose behavior according to the C or C++ standard is undefined, is interesting: #include <stdio.h> int foo() { int a; const int ...
1
vote
1answer
43 views

The advantage of using __attribute__((aligned( )))

I found in more than one SIMD program the instruction __attribute__((aligned(16))) . When I looked for an explanation I found The keyword attribute allows you to specify special attributes of ...
-3
votes
0answers
35 views

Developing linux p2p applications [on hold]

I have been tossing the idea around in the old noggin of creating a simple p2p chat application that would run in a linux environment and possibly be written in C. It's been awhile since I have ...
1
vote
2answers
120 views

How to abide the “allocate in caller” rule when the size is computed in the callee?

Let's say we have an opaque type handle_t that is managed through a simple interface with functions such as: handle_t *handle_init(void); int handle_do(handle_t *); void handle_free(handle_t *); ...
-3
votes
0answers
91 views

The bad habits formed in the process of learning the C programming language [closed]

In the Cambridge University Computer Science curriculum booklet of 2012-13 under the module entitled 'Foundations of Computer Science' it states: The main aim of this course is to present the ...
4
votes
2answers
82 views

Defining error codes

We are designing an error-handling framework for a cryptographic library written in C. The approach we are taking is that relatively few errors are propagated back to the user since on most occasions ...
1
vote
2answers
70 views

Microchip XC8: How to pack code against an upper limit?

I have a project for a PIC16F1454 that needs some custom linking. Basically, I've separated my code into two parts that can be downloaded separately in the field and thus update (almost) all of the ...
-3
votes
0answers
68 views

Precedence of operators [closed]

int x=2,y; y= x++ + x-- - ++x; printf("%d",y); When I compiled using Turbo C++, I got 3 as the output. For the GCC compiler, I got 1 as the output. Why does the output vary?
0
votes
1answer
163 views

Polymorphic engines in C and/or C++ [closed]

I stumbled across polymorphic engines and I don't know anything about them. However, I am curious about how they are written. Every example that I've looked up writes them in assembly, my assembly is ...
-2
votes
1answer
117 views

Ambiguous If-else nested block in C without braces [closed]

Consider the following code in C: if(34) if(0) printf("Stat1"); else printf("Stat2"); Here,the first if condition is true,while the second if condition is ...
0
votes
1answer
62 views

Buffer-overflow vulnerabilities that a static code analyser won't pick up [closed]

I'll use FlawFinder in this example. FlawFinder is a static code "analyser" tool that examines C/C++ source files and outputs warnings/hits if a vulnerability was identified. The way it does this is ...
-2
votes
0answers
25 views

Problems Initializing a Struct at Declare Time Using Char Pointer [migrated]

I am trying to declare and initialize a struct using a char* pointer. Compiling the code below fails if I do anything but thing things[] = {{3,300},{4,*text}}; Linux gives me a core dump when it ...
3
votes
2answers
150 views

On concatenating adjacent string literals

C and C++ compiles adjacent string literals as a single string literal. For example this: "Some text..." "and more text" is equivalent to: "Some text...and more text" In other languages like C# ...
0
votes
0answers
49 views

Basic logical operator question [migrated]

#include <stdio.h> int main() { int k=5; if(++k < 5 && k++ / 5 || ++k <= 8) { printf("%d",k); } return 0; } Why is the output 7 and not 8?(I am a ...
-2
votes
1answer
109 views

Uniqueness of for loop [closed]

How does the for loop function is implemented so that it can accept ; as parameter separator rather than , which is trivial in normal functions.
8
votes
5answers
530 views

Are C static libraries frowned upon? [closed]

There are 2 arguments for having shared libraries: It helps reduce disk space. When a shared library is updated, all the binaries depending on it get the update. There is mainly one drawback for ...
3
votes
1answer
215 views

Why did GCC switch from Bison to a recursive descent parser for C++ and C?

Was there a language change that required it or some practical reason why Bison was no longer appropriate or optimal? I saw on wikipedia that they switched, referring to the GCC 3.4 and GCC 4.1 ...
2
votes
2answers
106 views

In which stage of compilation is “typedef” resolved by the compiler?

There are different stages of compilation Such as Preprocessing stage Syntax analysis Semantic analysis Intermediate Code generation Machine Code generation Optimization Linking My question here ...
-1
votes
0answers
12 views

Problem with C code. Raspberry PI, HD44780 LCD [migrated]

I have been writing a program for interfacing a raspberry pi with a HD44780 LCD. The code I have wrote does not seem to initialise the display, from the datasheet as far as I can tell I am doing the ...
4
votes
7answers
977 views

Why do we need to include the .h while everything works when including only the .cpp file?

Why do we need to include both the .h and .cpp files , while we can make it works with making just a .cpp file and then including it . For example, Creating a file.h containing declarations, then ...
2
votes
2answers
98 views

gcc -S seems a bit misshapen with shifting and ANDing bits

Example: int c = 4; int p = 5; if (p & (1 << c)) printf("ok\n"); else printf("nop\n"); gcc -S: movl -4(%rbp), %eax /* eax holds the variable c */ movl -8(%rbp), %edx /* ...
19
votes
12answers
5k 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 ...
3
votes
1answer
123 views

How to fix legacy code that uses <string.h> unsafely?

We've got a bunch of legacy code, written in straight C (some of which is K&R!), which for many, many years has been compiled using Visual C 6.0 (circa 1998) on an XP machine. We realize this is ...
0
votes
10answers
945 views

Why does the instruction “do” require a “while”? [closed]

Since this statement is so common: while (true) (Java) or while (1) (C) or sometimes for (;;) Why is there not a single instruction for this? I could think that an instruction that could do it ...
0
votes
1answer
42 views

Macro guard in Objective-C

I have noticed that many famous libraries written in Objective-C (eg. AFNetworking) use the macro guard inside their header files. Im aware that the #import directive, which works exactly like ...
-2
votes
1answer
159 views

What are steps in making an operating system in C ? [duplicate]

I am trying to make an my own OS. This is for educational purpose only, so that I get to understand the internals as well as get a good idea of low level programming. I have some prior application ...
16
votes
13answers
4k views

Why do operating systems do low level stuff in C and C++? Why not just C++?

On the Wikipedia page for Windows, it states the Windows is written in Assembly for the bootloader and task switcher, and C and C++ for kernel routines. IIRC, you can call C++ functions from an ...
0
votes
1answer
104 views

comparison of an unsigned variable to 0

When I execute the following loop : unsigned m; for( m = 10; m >= 0; --m ){ printf("%d\n",m); } the loop doesn't stop at m==0, it keeps executing interminably, so I thought that ...
9
votes
8answers
6k views

Why does C dominate in the embedded software market? [closed]

Almost everyone will now say the blessing: performance! Okay, C does allow to write athletic code. But there are other languages that can do so, after all! And the optimising power of modern ...
53
votes
5answers
32k 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 ...
-4
votes
0answers
43 views

Allocating memory inside a function and returning it back [migrated]

I want to pass a pointer to my function and allocate the memory to which this pointer points. I've read in other posts that I should pass a double pointer to this function and I did so, but I keep ...
1
vote
2answers
191 views

Is goto to improve DRY-ness OK?

My code has many checks to detect errors in various cases (many conditions would result in the same error), inside a function returning an error struct. Instead of looking like this: err_struct ...
3
votes
3answers
113 views

Why are packed structures not part of the C language?

Every C compiler offers the option to "pack" C structures (e.g. __attribute__ ((__packed__)), or #pragma pack()). Now, we all know that packing is required, if we'd like to send or store data in a ...
-3
votes
3answers
376 views

More Accurate Random in C [closed]

I have 3 IPs and every IP has a weight, I want to return the IP's according to its weights using the random function. For example if we have 3 IP's X with weight 3 Y with weight 3 and Z with weight ...
-4
votes
1answer
110 views

Calculate Pi to N number of places [closed]

I am trying to work out how to calculate Pi to N decimal places, I think the default DP's for a float/double is 5 and then you have setprecision() however these are obviously inadequate, I need ...
2
votes
1answer
126 views

How to store satellite data in C data structutres

I've been reading through Introduction To Algorithms 3rd Ed, and am really enjoying the material; however, I am having difficulty in implementing some practical situations. It's not the theory, or ...
1
vote
3answers
73 views

Reconstructing a huffman tree using minimal information in the header

I'm writing a huffman encoding program in C. I'm trying to include the least amount of information in the header as possible, I know the simplest way to decompress the file in the header would be to ...
2
votes
3answers
186 views

C/C++: Which conversion warnings make sense in practice?

C/C++ implicit conversions can cause bugs. Here's one example: int foo, bar; scanf("%d", &foo); scanf("%d", &bar); float foobar = foo / bar; If I input 7 and 2, it's not 3.5 as expected - ...
6
votes
2answers
327 views

C programming practice, passing a pointer to a function [closed]

Consider the following C function which takes as argument a string, which is then stored inside a struct: struct mystruct* usestring(char* string) { struct mystruct *struct; struct = ...
51
votes
10answers
2k views

Why do we have postfix increment?

Disclaimer: I know perfectly well the semantics of prefix and postfix increment. So please don't explain to me how they work. Reading questions on stack overflow, I cannot help but notice that ...
1
vote
2answers
113 views

Implementing a simple controller in embedded C

Is there a known method or pattern to implement a simple controller for an MVC design in pure C or the switch case approach is the standard? Background : I have an embedded application and I'm ...
0
votes
2answers
115 views

boolean operations in C using bitfields

I am trying to implement boolean data type in C. Basically, I am working with sets. The following code can be used to access each bit but I am unsure whether I can represent sets using this method. ...