243 Answered Questions for the topic C

04/18/19

C: XNOR / Exclusive-Nor gate?

I am trying to find the most effective way of writing a XNOR gate in C. if(VAL1 XNOR VAL2) { BLOCK; } Any suggestions? Thanks.
C

04/16/19

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 5or enum { var = 5 };
C

04/13/19

Correct format specifier for double in printf?

What is the correct format specifier for `double` in printf? Is it `%f` or is it `%lf`? I believe it's `%f`, but I am not sure.###Code sample #include int main() { double d = 1.4; ... more
C

04/12/19

What is the difference between NULL, '\\0' and 0?

In C, there appear to be differences between various values of zero -- `NULL`, `NUL` and `0`.I know that the ASCII character `'0'` evaluates to `48` or `0x30`.The `NULL` pointer is usually defined... more

04/11/19

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

What is "ENT" short for? The `error` : >No such file or directory should just be named by "ENOFILE". Is there any story or reason?
C

04/11/19

How to print a int64_t type in C?

C99 standard has integer types with bytes size like int64_t. I am using the following code: #include #include int64_t my_int = 999999999999999999; printf("This is my_int: %I64d\ ",... more
C

04/11/19

Difference between malloc and calloc?

What is the difference between doing: ptr = (char **) malloc (MAXELEMS * sizeof(char *));or: ptr = (char **) calloc (MAXELEMS, sizeof(char*));When is it a good idea to use calloc over malloc... more
C

04/10/19

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)?
C

04/08/19

MIN and MAX in C?

Where are `MIN` and `MAX` defined in C, if at all?What is the best way to implement these, as generically and type safely as possible? (Compiler extensions/builtins for mainstream compilers... more
C

04/06/19

What exactly is a C pointer if not a memory address?

In a reputable source about C, the following information is given after discussing the `&` operator:> ... It's a bit unfortunate that the terminology *[address of]* remains, because it... more
C Logic

04/04/19

1 = false and 0 = true?

I came across an is_equals() function in a c API at work that returned 1 for non-equal sql tables (false) and 0 for equal ones (true). I only realized it after running test cases on my code, one... more
C

04/04/19

How to convert a string to integer in C?

I am trying to find out if there is an alternative way of converting string to integer in C.I regularly pattern the following in my code. char s[] = "45"; int num = atoi(s);So, is there a... more

04/04/19

What is Linux’s native GUI API?

I hope this doesn’t come across as a stupid question but it’s always something I have wondered. Both Windows (Win32 API) and OS X (Cocoa) have their own APIs to handle windows, events and other OS... more

04/03/19

Differences between fork and exec?

What are the differences between `fork` and `exec`?

04/02/19

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 int main(void) { int linux = 5; return 0; } Result of `$ gcc -E... more
C

04/02/19

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?
C

03/29/19

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?
C

03/27/19

How does free know how much to free?

In C programming, you can pass any kind of pointer you like as an argument to free, how does it know the size of the allocated memory to free? Whenever I pass a pointer to some function, I have to... more
C

03/27/19

How to know what the 'errno' means?

When calling `execl(...)`, I get an `errno=2`. What does it mean? How can I know the meaning of this `errno`?
C

03/27/19

Is it better to use C void arguments "void foo(void)" or not "void foo()"?

What is better: `void foo()` or `void foo(void)`?With void it looks ugly and inconsistent, but I've been told that it is good. Is this true?Edit: I know some old compilers do weird things, but if... more
C

03/26/19

How to find the 'sizeof' (a pointer pointing to an array)?

First off, here is some code: int main() { int days[] = {1,2,3,4,5}; int *ptr = days; printf("%u\ ", sizeof(days)); printf("%u\ ", sizeof(ptr)); ... more
C

03/19/19

Is bool a native C type?

I've noticed that the Linux kernel code uses bool, but I thought that bool was a C++ type. Is bool a standard C extension (e.g., ISO C90) or a GCC extension?
C

03/19/19

Difference between fprintf, printf and sprintf?

Can anyone explain in simple English about the differences between `printf`, `fprintf`, and `sprintf` with examples?What stream is it in?I'm really confused between the three of these while... more
C

03/19/19

Structure padding and packing?

Consider: struct mystruct_A { char a; int b; char c; } x; struct mystruct_B { int b; char a; } y;The sizes of the structures are 12 and 8... more
C

03/18/19

uint8_t vs unsigned char?

What is the advantage of using `uint8_t` over `unsigned char` in C?I know that on almost every system `uint8_t` is just a typedef for `unsigned char`,so why use it?

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.