243 Answered Questions for the topic C

C

06/13/19

Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

Why is it that `scanf()` needs the `l` in "`%lf`" when reading a `double`, when `printf()` can use "`%f`" regardless of whether its argument is a `double` or a `float`?Example code: double d; ... more
C

06/12/19

What is a "static" function?

The question was about plain [tag:C] functions, not [tag:C++] `static` methods, as clarified in comments.Ok, I understand what a `static` variable is, but what is a `static` function?And why is it... more
C

06/12/19

Execution time of C program?

I have a C program that aims to be run in parallel on several processors. I need to be able to record the execution time (which could be anywhere from 1 second to several minutes). I have searched... more
C

06/12/19

Why do all the C files written by my lecturer start with a #?

Why do all the C files written by my lecturer start with a #?I'm going through some C course notes, and *every* C program source file begins with a single `#` on the first line of the program.Then... more
C

06/12/19

Why malloc+memset is slower than calloc?

It's known that `calloc` is different than `malloc` in that it initializes the memory allocated. With `calloc`, the memory is set to zero. With `malloc`, the memory is not cleared.So in everyday... more
C

06/11/19

Unit Testing C Code?

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... more
C

06/11/19

warning: implicit declaration of function?

My compiler (GCC) is giving me the warning:> warning: implicit declaration of functionPlease help me understand why is it coming.
C

06/11/19

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... more
C

06/10/19

Error "initializer element is not constant" when trying to initialize variable with const?

I get an error on line 6 (initialize my_foo to foo_init) of the following program and I'm not sure I understand why. typedef struct foo_t {  int a, b, c; } foo_t; const foo_t... more
C

06/10/19

Why does a function with no parameters (compared to the actual function definition) compile?

I've just come across someone's C code that I'm confused as to why it is compiling. There are two points I don't understand.First, the function prototype has no parameters compared to the actual... more
C

06/10/19

Why is 0 < -0x80000000?

I have below a simple program: #include <stdio.h> #define INT32_MIN (-0x80000000) int main(void) {  long long bal = 0;    if(bal < INT32_MIN )  { ... more
C

06/10/19

Realistic usage of the C99 'restrict' keyword?

I was browsing through some documentation and questions/answers and saw it mentioned. I read a brief description, stating that it would be basically a promise from the programmer that the pointer... more
C

06/10/19

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

06/08/19

What is the difference between a definition and a declaration?

The meaning of both eludes me.
C

06/06/19

Should I replace _outpw() function?

I have some problems when I do simulation for my Simulink program, it says > `_outpw() function not defined` Should i try to find some other function to replace it? I have put this head... more
C

06/06/19

How to split a string literal across multiple lines in C / Objective-C?

I have a pretty long sqlite query: const char *sql_query = "SELECT statuses.word_id FROM lang1_words, statuses WHERE statuses.word_id = lang1_words.word_id ORDER BY lang1_words.word ASC";How can... more
C

06/05/19

Why does GCC use multiplication by a strange number in implementing integer division?

I've been reading about `div` and `mul` assembly operations, and I decided to see them in action by writing a simple program in C:###File division.c #include <stdlib.h> #include... more
C

06/05/19

Why do I get a segmentation fault when writing to a string initialized with "char *s" but not "char s[]"?

The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; // could be also written as *str = 'z' printf("%s\ ", str);While this works perfectly well: char... more
C

06/04/19

Trapezoidal Riemann Sum in C?

I've been trying to do Riemann Sums to approximate integrals in C. In my code below, I'm trying to approximate by both the trapezoidal way and the rectangular way (The trapezoidal way should be... more
C

06/04/19

Is there a good Valgrind substitute for Windows?

I was looking into Valgrind to help improve my C coding/debugging when I discovered it is only for Linux - I have no other need or interest in moving my OS to Linux so I was wondering if there is a... more
C

06/03/19

Why is volatile needed in C?

Why is `volatile` needed in C? What is it used for? What will it do?
C

06/03/19

Understanding typedefs for function pointers in C?

I have always been a bit stumped when I read other peoples' code which had typedefs for pointers to functions with arguments. I recall that it took me a while to get around to such a definition... more
C

06/03/19

Can someone please tell me if my code has anything wrong with it?

I have this project for school where I have to fund a formula and make a program to calculate it. I chose the control formal for bullet ballistics, which is X = (v² / g) * Sin(2 theta) , where X =... more
C

06/03/19

How can I autoformat/indent C code in vim?

When I copy code from another file, the formatting is messed up, like this: fun() { for(...) { for(...) { if(...) { } } } }How can I autoformat this code in vim?
C

06/03/19

What do the parentheses around a function name mean?

In one of my project source files, I found this C function definition: int (foo) (int *bar) { return foo (bar); }Note: there is no asterisk next to `foo`, so it's not a function... more

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.