Skip to main content
Filter by
Sorted by
Tagged with
10178 votes
26 answers
1.0m views

What is the '-->' operator in C/C++?

After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. ...
3388 votes
12 answers
505k views

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop ...
3158 votes
27 answers
1.7m views

How to set, clear, and toggle a single bit

How can I set, clear, and toggle a bit?
JeffV's user avatar
  • 54.2k
3118 votes
30 answers
923k views

What is the difference between #include <filename> and #include "filename"?

What is the difference between using angle brackets and quotes in an include directive? #include <filename> #include "filename"
quest49's user avatar
  • 50.7k
2803 votes
30 answers
395k views

Should I cast the result of malloc (in C)?

In this question, someone suggested in a comment that I should not cast the result of malloc. i.e., I should do this: int *sieve = malloc(sizeof(*sieve) * length); rather than: int *sieve = (int *) ...
Patrick McDonald's user avatar
2558 votes
4 answers
396k views

What does the ??!??! operator do in C?

I saw a line of C that looked like this: !ErrorHasOccured() ??!??! HandleError(); It compiled correctly and seems to run ok. It seems like it's checking if an error has occurred, and if it has, it ...
Peter Olson's user avatar
2161 votes
18 answers
1.3m views

What is the effect of extern "C" in C++?

What exactly does putting extern "C" into C++ code do? For example: extern "C" { void foo(); }
Litherum's user avatar
  • 23.2k
1839 votes
23 answers
750k views

What is the difference between const int*, const int * const, and int * const?

I always mess up how to use const int *, const int * const, and int * const correctly. Is there a set of rules defining what you can and cannot do? I want to know all the do's and all don'ts in terms ...
user avatar
1814 votes
20 answers
134k views

With arrays, why is it the case that a[5] == 5[a]?

As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5] == 5[a] Joel says that it's because of pointer ...
Dinah's user avatar
  • 53.8k
1810 votes
5 answers
204k views

What is ':-!!' in C?

I bumped into this strange macro code in /usr/include/linux/kernel.h: /* Force a compilation error if condition is true, but also produce a result (of value 0 and type size_t), so the expression ...
chmurli's user avatar
  • 15.1k
1776 votes
15 answers
152k views

Is < faster than <=?

Is if (a < 901) faster than if (a <= 900)? Not exactly as in this simple example, but there are slight performance changes on loop complex code. I suppose this has to do something with generated ...
Vinícius's user avatar
  • 15.7k
1630 votes
23 answers
467k views

Compiling an application for use in highly radioactive environments

We are compiling an embedded C++ application that is deployed in a shielded device in an environment bombarded with ionizing radiation. We are using GCC and cross-compiling for ARM. When deployed, our ...
rook's user avatar
  • 66.8k
1513 votes
12 answers
1.0m views

How do function pointers in C work?

I had some experience lately with function pointers in C. So going on with the tradition of answering your own questions, I decided to make a small summary of the very basics, for those who need a ...
1431 votes
22 answers
1.2m views

What does "static" mean in C?

I've seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?
user avatar
1410 votes
22 answers
3.3m views

How do I determine the size of my array in C?

How do I determine the size of my array in C? That is, the number of elements the array can hold?
Mark Harrison's user avatar
1263 votes
19 answers
1.0m views

How do I use extern to share variables between source files?

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope? This is related to sharing variables across source ...
user avatar
1180 votes
27 answers
2.3m views

How to initialize all members of an array to the same value?

I have a large array in C (not C++ if that makes a difference). I want to initialize all members of the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, ...
Matt's user avatar
  • 87.3k
1178 votes
21 answers
1.7m views

What is the difference between ++i and i++?

In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?
The.Anti.9's user avatar
  • 44.4k
1109 votes
5 answers
136k views

Why does the C preprocessor interpret the word "linux" as the constant "1"?

Why does the C preprocessor in GCC interpret the word linux (small letters) as the constant 1? test.c: #include <stdio.h> int main(void) { int linux = 5; return 0; } Result of $...
ahmedaly50's user avatar
  • 7,825
1058 votes
27 answers
487k views

What is the difference between a definition and a declaration?

The meaning of both eludes me.
Maciek's user avatar
  • 19.8k
1042 votes
12 answers
1.0m views

typedef struct vs struct definitions [duplicate]

I'm a beginner in C programming, but I was wondering what's the difference between using typedef when defining a structure versus not using typedef. It seems to me like there's really no difference, ...
user69514's user avatar
  • 27.5k
1001 votes
11 answers
308k views

What is the strict aliasing rule?

When asking about common undefined behavior in C, people sometimes refer to the strict aliasing rule. What are they talking about?
Benoit's user avatar
  • 38.8k
996 votes
4 answers
85k views

Obfuscated C Code Contest 2006. Please explain sykes2.c

How does this C program work? main(_){_^448&&main(-~_);putchar(--_%64?32|-~7[__TIME__-_/8%8][">'txiZ^(~z?"-48]>>";;;====~$::199"[_*2&8|_/64]/(_&2?1:8)%8&1:10);} It ...
corny's user avatar
  • 7,832
964 votes
31 answers
558k views

Unit Testing C Code [closed]

I worked on an embedded system this summer written in straight C. It was an existing project that the company I work for had taken over. I have become quite accustomed to writing unit tests in Java ...
958 votes
14 answers
660k views

Difference between malloc and calloc?

What is the difference between doing: ptr = malloc(MAXELEMS * sizeof(char *)); And: ptr = calloc(MAXELEMS, sizeof(char*)); When is it a good idea to use calloc over malloc or vice versa?
user105033's user avatar
  • 19.4k
948 votes
9 answers
120k views

Why use apparently meaningless do-while and if-else statements in macros?

In many C/C++ macros I'm seeing the code of the macro wrapped in what seems like a meaningless do while loop. Here are examples. #define FOO(X) do { f(X); g(X); } while (0) #define FOO(X) if (1) { f(...
jfm3's user avatar
  • 37.6k
921 votes
15 answers
104k views

Why are these constructs using pre and post-increment undefined behavior?

#include <stdio.h> int main(void) { int i = 0; i = i++ + ++i; printf("%d\n", i); // 3 i = 1; i = (i++); printf("%d\n", i); // 2 Should be 1, no ? volatile int u = 0; u ...
PiX's user avatar
  • 9,875
881 votes
17 answers
1.1m views

What is a segmentation fault?

What is a segmentation fault? Is it different in C and C++? How are segmentation faults and dangling pointers related?
Rajendra Uppal's user avatar
871 votes
19 answers
1.6m views

Using Boolean values in C

C doesn't have any built-in Boolean types. What's the best way to use them in C?
neuromancer's user avatar
  • 55.1k
867 votes
13 answers
301k views

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
Kevin's user avatar
  • 25.8k
865 votes
19 answers
975k views

makefile:4: *** missing separator. Stop

This is my makefile: all:ll ll:ll.c gcc -c -Wall -Werror -02 c.c ll.c -o ll $@ $< clean : \rm -fr ll When I try to make clean or make make, I get this error: :makefile:4: *** ...
Rahul Reddy's user avatar
  • 13.1k
847 votes
19 answers
469k views

What should main() return in C and C++?

What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? And how about the arguments? If int main() then return 1 or return 0?
Joel's user avatar
  • 15.5k
823 votes
10 answers
825k views

How do you pass a function as a parameter in C?

I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C?
andrewrk's user avatar
  • 30.9k
820 votes
15 answers
1.0m views

What is size_t in C?

I am getting confused with size_t in C. I know that it is returned by the sizeof operator. But what exactly is it? Is it a data type? Let's say I have a for loop: for(i = 0; i < some_size; i++) ...
Vijay's user avatar
  • 66.8k
818 votes
33 answers
1.3m views

How can I get the list of files in a directory using C or C++?

How can I determine the list of files in a directory from inside my C or C++ code? I'm not allowed to execute the ls command and parse the results from within my program.
foobarfuzzbizz's user avatar
773 votes
10 answers
457k views

How to determine CPU and memory consumption from inside a process

I once had the task of determining the following performance parameters from inside a running application: Total virtual memory available Virtual memory currently used Virtual memory currently used ...
Lanzelot's user avatar
  • 16.5k
742 votes
17 answers
161k views

Speed comparison with Project Euler: C vs Python vs Erlang vs Haskell

I have taken Problem #12 from Project Euler as a programming exercise and to compare my (surely not optimal) implementations in C, Python, Erlang and Haskell. In order to get some higher execution ...
Hyperboreus's user avatar
  • 32.3k
740 votes
30 answers
465k views

How do I detect unsigned integer overflow?

I was writing a program in C++ to find all solutions of ab = c, where a, b and c together use all the digits 0-9 exactly once. The program looped over values of a and b, and it ran a digit-counting ...
Chris Johnson's user avatar
731 votes
5 answers
724k views

Why are #ifndef and #define used in C++ header files?

I have been seeing code like this usually in the start of header files: #ifndef HEADERFILE_H #define HEADERFILE_H And at the end of the file is #endif What is the purpose of this?
Asad Khan's user avatar
  • 11.8k
730 votes
4 answers
432k views

Why does ENOENT mean "No such file or directory"?

What does the ENT mean in ENOENT? Shouldn't the error: No such file or directory just be named by ENOFILE? Is there any story or reason?
mingchaoyan's user avatar
  • 8,446
718 votes
19 answers
53k views

Can code that is valid in both C and C++ produce different behavior when compiled in each language?

C and C++ have many differences, and not all valid C code is valid C++ code. (By "valid" I mean standard code with defined behavior, i.e. not implementation-specific/undefined/etc.) Is there any ...
user541686's user avatar
  • 209k
718 votes
34 answers
2.4m views

How to generate a random int in C?

Is there a function to generate a random int number in C? Or will I have to use a third party library?
Kredns's user avatar
  • 37k
714 votes
8 answers
1.3m views

What is the printf format specifier for bool?

Since ANSI C99 there is _Bool or bool via stdbool.h. But is there also a printf format specifier for bool? I mean something like in that pseudo code: bool x = true; printf("%B\n", x); which would ...
maxschlepzig's user avatar
  • 38.2k
703 votes
48 answers
155k views

Divide a number by 3 without using *, /, +, -, % operators

How would you divide a number by 3 without using *, /, +, -, %, operators? The number may be signed or unsigned.
703 votes
10 answers
515k views

Why does printf not flush after the call unless a newline is in the format string?

Why does printf not flush after the call unless a newline is in the format string? Is this POSIX behavior? How might I have printf immediately flush every time?
Crazy Chenz's user avatar
698 votes
6 answers
826k views

What does 'dereferencing' a pointer mean in C/C++?

Please include an example with the explanation.
asir's user avatar
  • 13.2k
694 votes
9 answers
307k views

Difference between static and shared libraries?

What is the difference between static and shared libraries? I use Eclipse and there are several project types including Static Libraries and Shared Libraries? Does one have an advantage over the ...
Mohit Deshpande's user avatar
681 votes
6 answers
298k views

Why is “while( !feof(file) )” always wrong?

What is wrong with using feof() to control a read loop? For example: #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char *path = "stdin"; FILE ...
William Pursell's user avatar
678 votes
11 answers
740k views

What is a "static" function in C?

The question was about plain c functions, not c++ static methods, as clarified in comments. I understand what a static variable is, but what is a static function? And why is it that if I declare a ...
Slava V's user avatar
  • 17.2k
673 votes
17 answers
390k views

"static const" vs "#define" vs "enum"

Which one is better to use among the below statements in C? static const int var = 5; or #define var 5 or enum { var = 5 };
Vijay's user avatar
  • 66.8k

1
2 3 4 5
8122