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

learn more… | top users | synonyms

1
vote
2answers
53 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 ...
-2
votes
1answer
91 views

Implementing keyword “const” [on hold]

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

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

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
129 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
0answers
34 views

Generate Same Sequence of Random Numbers in IDL and C [on hold]

I am in the process of developing a C version of a program originally written in IDL. The programs generate arrays with random number generators. I use gsl_rng_uniform and gsl_ran_poisson in the C ...
1
vote
1answer
180 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 ...
0
votes
0answers
16 views

Replacing a string in C [migrated]

The following only works with numbers, but it won't replace words with spaces. Any suggestions? // Program to find one string in another, delete the found string, and replace it with another ...
-2
votes
0answers
115 views

How to define common function in c [migrated]

I've got two c programs: program1.c and program2.c Both of the two programs, have three or more same functions. How can I define them one time and use it inside both? An example: I've got two ...
7
votes
3answers
185 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 ...
-4
votes
0answers
64 views

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

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++?
0
votes
0answers
31 views

How to load a dynamic array from file? [closed]

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 ...
24
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
64 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 ...
1
vote
2answers
101 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
486 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
82 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); ...
-1
votes
2answers
108 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
391 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
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 ...
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
103 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
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 ...
1
vote
2answers
70 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
553 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 ...
-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. ...
1
vote
4answers
598 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, ...
0
votes
3answers
137 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 ; ...
6
votes
1answer
220 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 ...
2
votes
3answers
248 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. ...
2
votes
3answers
280 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
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
608 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 ...
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 /* ...
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
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
253 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
103 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 ...
0
votes
1answer
144 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 ...
4
votes
1answer
252 views

Why is the function called lseek(), not seek()?

The C function for seeking in a file is called lseek(). Why ins't it called just seek()?
3
votes
3answers
156 views

Organization of DLL linked functions

This is a code organization question. I got my basic code working but when I expand it, it will be terrible. I have a DLL which I don't have a .lib for. Therefore I have to use the whole ...
1
vote
3answers
143 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 ...
1
vote
1answer
190 views

Are there theoretical reasons why arrays in C take less RAM than Java?

My experience is that Java requires about twice as much RAM compared to C (comparing char arrays or other comparisons). I also read in a hardware book that Java takes about twice RAM than C. Is is ...
2
votes
3answers
166 views

Why are there two different kinds of linking, i.e. static and dynamic?

I've been bitten for the n-th time now by a library mismatch between a build and deployment environment. The build environment had libruby.so.2.0 and the deployment environment had libruby.a. One ruby ...
4
votes
1answer
549 views

Why are most GNU's software written in C [closed]

I am a Java developer, and I rarely write GUI program in C. However, I noticed that many GNU's projects, such as PSPP, R, Dia, etc., are written in C, instead of Java or C++. I personally don't mind ...
-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 ...
1
vote
2answers
311 views

“bug” in C++11 text by Stroustrup?

I found an apparent contradiction in the c++ text having to do with the result of the c_str() function operating on std:strings (in my copy, the definition and contradiction are on p1040). First it ...
1
vote
1answer
118 views

Should I implement a function or a method?

Once in a while I encounter a C function in my colleagues' code. Mostly it is in some helpful objective categories, and those functions are mostly an internal calculation of something or a ...
0
votes
3answers
133 views

How are negative signed values stored? [duplicate]

I was watching this video on the maximum and minimum values of signed integers. Take an example of a positive signed value - 0000 0001 The first bit denotes that the number is positive and the last ...