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

learn more… | top users | synonyms

-1
votes
0answers
28 views

Possible memory issue in linked list program [on hold]

I am trying to write a program which will count the occurrence of words in a paragraph. The logic I am following : I am using a linked list for the purpose. And I am searching sequentially - if new ...
2
votes
2answers
119 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
96 views

Uniqueness of for loop [on hold]

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

Are C static libraries frowned upon? [on hold]

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 ...
1
vote
1answer
30 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
1answer
196 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 ...
-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 ...
1
vote
2answers
98 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 ...
3
votes
1answer
120 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 ...
4
votes
7answers
957 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 ...
21
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 ...
0
votes
1answer
41 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
148 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 ...
-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
187 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 ...
0
votes
1answer
102 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 ...
3
votes
3answers
108 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 ...
-4
votes
1answer
99 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
125 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
70 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
183 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
314 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 = ...
-1
votes
0answers
11 views

Examining a C source code easily [migrated]

In Java, just by clicking the classes in Eclipse, I can go to that specific class being referenced. In C, how can I do that? As far as I can remember I wasn't able to do that in Eclipse. I had hard ...
1
vote
2answers
110 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
113 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. ...
1
vote
1answer
162 views

What is this boolean law?

A friend of mine has shown sometime ago the name of the below boolean technique/law but I forget that name unfortunately. Does someone know what it's called? example in C language: !(a || b) It's ...
-1
votes
2answers
116 views

Reading assembly useful for insights when writing C/C++? [closed]

While in most cases its not necessarily, recently I have taken to reading the generated assembly of my C code every so often, when I'm curious as to the kinds of optimizations my compiler is making. ...
5
votes
2answers
218 views

Processor architectures for testing C/C++ portability

Currently I'm working on a C/C++ code-base which is fairly portable, it can compile on most Unix like systems as well as MS-Windows (MSVC), using various popular compilers. Previously I've found ...
4
votes
1answer
350 views

Why does C use the asterisk for pointers?

I'm just now learning about C. I find it odd that the creators chose the asterisk (*) as the symbol for pointers rather than a symbol that actually looks like a pointer (->). Considering how ...
0
votes
0answers
37 views

Typecasting int to char in printf() in C [migrated]

int *int_pointer = malloc(10); *int_pointer = 53200; printf("The integer at byte #0 is set to: %d \n", (char) *int_pointer); RESULT: -48 So I was just trying this out to see what would happen and ...
20
votes
3answers
3k views

Why Increment Pointers?

I just recently started learning C++, and as most people (according to what I have been reading) I'm struggling with pointers. Not in the traditional sense, I understand what they are, and why they ...
1
vote
2answers
68 views

Is there a stricter strtoull() in any ubiquitous C library?

I want a function that will interpret a string as a strictly unsigned integer, failing when the string overflows, represents a negative number, or does not represent a number. strtoull() does set ...
-3
votes
1answer
106 views

Implementing keyword “const” [closed]

A constant pointer is a pointer which points to a fixed memory location throughout.The question is how do you ensure that this behavior is ensured without using the keyword constant while declaring ...
1
vote
4answers
160 views

What should I do and know before I start writing C? [closed]

I have a college degree in CS, and learned about Java and OCaml. Java was used as introduction, and in most CS classed, except the theoretical ones. OCaml was part of learning a different programming ...
2
votes
2answers
150 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 ...
1
vote
1answer
188 views

Which is the more testable C function implementation?

I write code in C. I have been striving to write more testable code but I am a little confused on deciding between writing pure functions that are really good for testing but require smaller functions ...
7
votes
4answers
284 views

Omitting “destructors” in C is taking YAGNI too far?

I'm working on a medium embedded application in C using OO-like techniques. My "classes" are .h/.c modules using data structs and function pointers structs to emulate encapsulation, polymorphism, and ...
25
votes
10answers
4k views

When I test out the difference in time between shifting and multiplying in C, there is no difference. Why?

I have been taught that shifting in binary is much more efficient than multiplying by 2^k. So I wanted to experiment, and I used the following code to test this out: #include <time.h> #include ...
1
vote
3answers
71 views

Handling leaves in tree-based menus

Tl;Dr Given a language with no polymorphism how to you elegantly modify the behavior of a leaf in a tree data structure. Context I'm programming an application in C for a microcontroller and I'm ...
3
votes
4answers
509 views

Static vs. non-static? (with non-OOP functions)

Although this question has come up before, mine is different because it is not supposed to cover anything OOP but only plain (ANSI) C. Especially in open-source software, I usually encounter a ...
0
votes
3answers
131 views

Implementing a priority queue with a circular array

[If this fits more to stackoverflow then transfer it there please.] Hi, I'm trying to implement a priority queue in C. The easiest way I found to do it is with a circular array (rather than using a ...
-1
votes
2answers
126 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 ...
4
votes
4answers
410 views

Why can't C arrays have 0 length?

The C11 standard says the arrays, both sized and variable length "shall have a value greater than zero." What is the justification for not allowing a length of 0? Especially for variable length ...
0
votes
0answers
14 views

Defining property and find substitution for copy function of python in cython

I want to cythonize a code in python to speed up the code. In the following you can see my attempt to make my python class understandable for cython: import numpy as np cimport numpy as np ctypedef ...
10
votes
4answers
1k views

Pointer indexing

I am currently reading a book titled "Numerical Recipes in C". In this book, the author details how certain algorithms inherently work better if we had indices starting with 1 (I don't entirely follow ...
-2
votes
1answer
108 views

How can we make a certain thing inbuilt in c language with our own header file?

I want to make the basic trigonometric functions in maths inbuilt functions using my own header file . so as the users can do trigonometry in c language compilers.
1
vote
2answers
72 views

How to handle enums in an indirection with functionpointers at ANSI-C?

Moinsen, I am somehow stucked in a design problem. Language is ANSI-C. Lets assume we have a tinkerbox of software-modules: one module for the logic Logic (at least) one module doing some logging ...
17
votes
4answers
575 views

How bad is it calling println() often than concatenating strings together and calling it once?

I know output to the console is a costly operation. In the interest of code readability sometimes it is nice to call a function to output text twice, rather than having a long string of text as an ...
2
votes
4answers
1k views

Going through The C Programming Language K&R in Visual Studio

From what I have read, K&R seems to be a good place to start learning programming in general, and C programming specifically. However, I've just started the first chapter and I have a few ...