Questions tagged [c]

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

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

It was developed in 1972 by Dennis Ritchie for use with the Unix operating system.

Reading list:

1314 questions
152
votes
6 answers

Why hasn't a faster, "better" language than C come out?

With all the new "modern" languages out today, how is it that C is still heralded as the fastest and "closest to the machine"? I don't really believe in there ever being only one correct way to do things, and C has been around for a really long time…
Jason
  • 2,037
83
votes
8 answers

Why use trailing newlines instead of leading with printf?

I heard that you should avoid leading newlines when using printf. So that instead of printf("\nHello World!") you should use printf("Hello World!\n") In this particular example above it does not make sense, since the output would be different, but…
klutt
  • 1,428
82
votes
10 answers

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 (C89). For instance: Having length available in a…
VF1
  • 1,891
24
votes
8 answers

Should I assume data passed to my function is accurate?

Assuming I have the following struct (just an example) struct string{ int len; char*str; } And I have the function int init_str(struct string*s, int len); which will perform s->len=len; s->str=malloc(len*sizeof(char)) And then I have the…
Harf
  • 368
15
votes
5 answers

Is it bad to write object oriented C?

I always seem to write code in C that is mostly object oriented, so say I had a source file or something I would create a struct then pass the pointer to this struct to functions (methods) owned by this structure: struct foo { int x; }; struct…
mosmo
  • 177
  • 1
  • 4
13
votes
2 answers

Why error codes are negated?

Very often I see in C code negation of returned error codes, e.g. return -EINVAL instead of return EINVAL. Why used negation?
user55777
13
votes
9 answers

Array or Malloc?

I'm using the following code in my application, and it's working fine. But I'm wondering if it's better to make it with malloc or to leave it as is? function (int len) { char result [len] = some chars; send result over network }
Dev Bag
  • 147
12
votes
3 answers

Why are packed structures not part of the C language?

Every C compiler offers the option to "pack" C structures (e.g. __attribute__ ((__packed__)), or #pragma pack()). Now, we all know that packing is required, if we'd like to send or store data in a reliable way. This must also have been a requirement…
12
votes
1 answer

Storing the EOF (End of File) character in a char type

I read in the Dennis Ritchie's The C Programming Language book that int must be used for a variable to hold EOF – to make it sufficiently large so that it can hold EOF value – not char. But following code works fine: #include main() { …
user1369975
  • 1,279
12
votes
1 answer

programming PID loops in C

I'm an electrical engineer that was kind of thrust into the digital world and learning as I go. I'm programming a TI processor to do a PID (proportional-integral-derivative) loop, illustrated by this diagram: I'll also describe it: Negative…
10
votes
3 answers

Why does an unsigned int compared with a signed character turn out with an unexpected result?

Why does the following code output y>x when clearly 1>-1? unsigned x=1; signed char y=-1; if(x>y){ printf("x>y"); } else { printf("y>x"); } Please explain this result.
Sudhir
  • 217
10
votes
1 answer

Optimal buffer size for fread/fwrite

What buffer size should I choose for read/write files via POSIX fread/fwrite functions?
user55777
9
votes
1 answer

Macro vs. Static functions in Header

for a lot of quick tasks where one could employ a function f(x,y), in plain C, macros are used. I would like to ask specifically about these cases, that are solvable by a function call (i.e. macros used for inlining functions, not for code expansion…
wirrbel
  • 3,028
9
votes
5 answers

Is there an advantage for a C programmer to have read K&R?

This question has been haunting me because I'm now reading Kernighan & Ritchie's: The C Programming Language (K&R) but I meet a lot, and I mean A LOT, of C programmers that have never read it. So, my question is: Is there an advantage to reading…
7
votes
4 answers

How can I increase my programming skills?

I program in C and I believe I know the language well. I understand all the concepts and my problems never come from misuse of the language. I have problems because I always forget or oversee things. Setting a variable to zero, or generating a…
1
2 3 4 5