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

learn more… | top users | synonyms

17
votes
8answers
2k 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 ...
3
votes
0answers
39 views

Ommiting “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 ...
-3
votes
0answers
40 views

Best book to learn OOP in Java if you know C/C++ at beginner level? [on hold]

Which is the Best book to learn OOP in Java if you know C/C++ at beginner level? Or What is the best way to learn JAVA and OOP in case one knows C/C++?
1
vote
3answers
61 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 ...
0
votes
0answers
25 views

How to load a dynamic array from file? [on hold]

I'll walk through my program. In C, I created my own array structure. /* dynamic array of type int */ typedef struct _ARRAY_I { int *p_array; int size; } ARRAY_I; In C++, I'm used to ...
1
vote
2answers
94 views

Precedence of operators

I recently read that increment operators have higher priority than ==. But if you write something like int i=8,j=9; if(i++ == j) printf("hi"); else printf("bye"); Now this outputs bye. ...
3
votes
4answers
449 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 ...
-1
votes
0answers
57 views

Declarations in C [on hold]

Hi I am new to programming. I just wanted to ask how to decipher the various kind of declarations which there are in C. For example: int (*a)[7] is array of 7 pointers which point to integer data ...
10
votes
8answers
5k views

Why does C dominate in the embedded software market?

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 ...
0
votes
2answers
287 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 ...
22
votes
10answers
6k views

Why are pointers not recommended when coding with C++

I read from somewhere that when using C++ it is recommended not to use pointers. Why is pointers such a bad idea when you are using C++. For C programmers that are used to using pointers, what is the ...
1
vote
2answers
124 views

What is the underlying mechanism behind va_list and where is it defined?

http://www.cplusplus.com/reference/cstdarg/va_list/ According to the above link, va_list is an argument or parameter used in some macros - va_start, va_arg, va_end. These macros are present in the ...
1
vote
3answers
142 views

Parsing mathematical expressions with two values that have parentheses and minus signs

I'm trying to parse equations like the ones below, which only have two values or the square root of a certain value, from a text file: 100+100 -100-100 -(100)+(-100) sqrt(100) by the minus ...
0
votes
0answers
40 views

How to build a strong base in programming? [closed]

I am currently pursuing a Computer Engineering degree more focused towards hardware (circuit design, microprocessors etc) but I also want to be good at programming. I just feel that I lack a strong ...
-2
votes
0answers
79 views

What's next after learning C programming language? [closed]

I have learned C programming language from K&R's Book and i really can't understand where to head to after all of this. I am in 2nd year of my college and in first semester we don't have to much ...
-3
votes
0answers
69 views

Hardware and the Programmer [closed]

Firstly, i know this question is a bit more like hardware-oriented, but i guess if i asked in some hardware community i would not have the programmers-perspective. My PC has just recently crashed ( ...
4
votes
4answers
381 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 ...
-1
votes
2answers
104 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 ...
0
votes
3answers
72 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
0answers
67 views

Sizeof Structure in C [migrated]

To find the size of a structure in C struct student { char name; int age; float weight; }; main () { int i,j,k,l; struct student s1; i=sizeof(s1.name); ...
16
votes
10answers
17k views

Why would I learn C++11, having known C and C++? [closed]

I am a programmer in C and C++, although I don't stick to either language and write a mixture of the two. Sometimes having code in classes, possibly with operator overloading, or templates and the oh ...
0
votes
0answers
13 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 ...
14
votes
13answers
3k 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 ...
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 ...
4
votes
6answers
6k views

When is C a better choice than C++? [closed]

I can't imagine that there's any reason not to use the OOP features of C++. It's is just as fast as C, and - what's more important to me - it's transparent, just as C. (I mean "transparent" that I ...
-2
votes
1answer
101 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.
2
votes
3answers
279 views

Understanding how variable assignment works

When I started learning C programming a few years ago, my tutor taught me similar to most of the tutors around the world. She said me the very basic things like any int datatype is of 2 bytes memory. ...
1
vote
4answers
590 views

Why would a C executable be smaller when compared to C++ executable

I'm trying to understand why the output file sizes are significantly different when using a C and a C++ compiler. I was writing a small hello world program in C and C++, I noticed that in C version, ...
2
votes
0answers
148 views

What's wrong with this pointer? [migrated]

I though I had finally understood pointers but then I encountered this issue: typedef struct { unsigned int a; unsigned int b; } Bar; Bar *foo; foo->a = 3; This last instruction ...
2
votes
4answers
1k views

C/C++: Who uses the logical operator macros from iso646.h and why? [closed]

There has been some debate at work about using the merits of using the alternative spellings for C/C++ logical operators in iso646.h: and && and_eq &= bitand & bitor ...
17
votes
4answers
539 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 ...
1
vote
2answers
68 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 ...
-2
votes
4answers
201 views

Benefits of porting C library to C++ [closed]

Say we take any C library such as libpng or libvorbis and convert it so that it compiles as C++ (only the minimum changes to make it compile as C++ code). Can the compiler do extra optimizations when ...
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 ...
6
votes
1answer
218 views

Suggested method for extracting a standalone C library from an existing R package?

My group has been developing an R package to simulate plant growth (see GitHub repository). The R package uses .Call to interface with C. We have decided that it would be worthwhile to create a ...
0
votes
3answers
135 views

usage of double pointers and n pointers?

I am familiar with basic C pointers. Just wanted to ask what is the actual use of double pointers or for that matter n pointer? #include<stdio.h> int main() { int n = 10 , *ptr , **ptr_ptr ; ...
-2
votes
0answers
71 views

How can I improve the following code in terms of faster execution? [migrated]

The problem is described here - Aaah!. My source code for the problem is below. I one of the fast running codes, but not the best. What changes can I make so as to improve the efficiency of the code. ...
0
votes
0answers
4 views

What happens when there is multiple expressions in the condition part of a for loop seperated by commas? [migrated]

I have an infinite loop here, but why? int end = 5; for(int i = 0; i < end, printf("at condition i=%d\n",i); ++i) { printf("inside i=%d\n",i); }
0
votes
0answers
74 views

Different ways of addressing pointers C/C++ [migrated]

I'm a little confused about accessing specific addresses using c style pointers. I have a 32 bit chip which I'm tryin to program. I need to flip some bits or whatever in ram. The following solution ...
2
votes
3answers
243 views

Transitioning from Java & C to C++

I learned Java and I'm pretty competent programming in it. I also learned C recently. I looked at a comparison between Java and C++ and I like C++'s features like operator overloading and its speed. ...
1
vote
2answers
144 views

Understanding Arithmetic In C

I have an embarrassingly simple question, but I need to make sure I understand this correctly. I have a *.c file with the following line: CSRConstant = ...
9
votes
2answers
598 views

Why can't arrays be passed as function arguments in C?

Following this comment, I've tried to google why, but my google-fu failed. Comment from link: [...] But the important thing is that arrays and pointers are different things in C. Assuming ...
0
votes
3answers
140 views

Use of malloc in C

Is it necessary to call free function every time we use malloc in C. I am asking this because I have seen many times that it is not called . Thank you
2
votes
1answer
51 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 /* ...
55
votes
10answers
6k 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 ...
1
vote
1answer
74 views

How to check the space complexity of this program?

I have written my version of strstr function in c. I am using a temporary array of size 26. Then is the space complexity O(1) or O(n)? This is my code : void strcheck(char t[], int n, char p[], int ...
0
votes
2answers
250 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
101 views

Memory read/write access efficiency

I've heard conflicting information from different sources, and I'm not really sure which one to believe. As such, I'll post what I understand and ask for corrections. Let's say I want to use a 2D ...
7
votes
2answers
1k views

Questions about linking libraries in C

I am learning C (still very much a beginner) on Linux using the GCC compiler. I have noticed that some libraries, such as the library used with the math.h header, need to be linked in manually when ...
0
votes
1answer
128 views

Global variable in a Linux shared library

Suppose we have the following setup under Linux, .so library named "libcnt.so" and 3 user space apps: "app1", "app2", "app3". This library does 1 simple thing, it says to the app (app dynamically ...