Does C require that variables are declared at start of scope? [closed] - c

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
The C language requires that all variables used in the program should be declared before any other statements.
Why was this of significance? Was it for readability? If it was for readability, then why don't other languages follow the same rule?
I personally prefer declaring all my variables at the very start.
Moderators: Please delete if this is off-topic or already been asked. I was just keen to know improve my knowledge. Thanks.

Like other people said in comments now it's not mandatory anymore. Anyway it wasn't for readability. C is a very old language and one of its peculiar features is that a C file can be compiled with just one parsing. I think that the earliest version of the language made the choice to declare variable at the beginning for simplify the parsing

Related

Why is the "FILE" data type in C not simply written as "file"? [duplicate]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
This just seems odd to me, most other things are lower case.
Is there some historical reason?
It's a macro. Macros have historically had all caps in C. Unfortunately recent trends appear to have broken that fact.
Small history lesson: Also, FILE was an io buffer abstraction in UNIX v7 libc stdio. A FILE doesn't necessarily represent a physical file, just something that can do block IO. Source:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/stdio.h.html
"file" was already defined by the kernel as well:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/sys/file.h.html
As someone else said here it's probably a typedef now, but I don't think C had typedefs back in '79 as it only just had structs. Then again I wasn't born then so... :)
It's all caps almost certainly because at least originally it was a macro. Nowadays, chances are pretty good that it's a typedef instead, but nobody changed the name to boot (and doing so would be a pretty lousy idea).

How one did manage to create and access structures that behaved like arrays in C89? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Before C99 programmers were obliged to define all arrays with fixed sizes that were known at compile time. How did people manage to create and access structures that behaved like arrays but whose sizes were not known until runtime?
With the use of malloc(3) and free(3) to do dynamic memory management. It's still done today.
void f(int n)
{
int *nInts = malloc(n * sizeof(int));
/* do stuff with 'nInts' */
free(nInts);
}
I think you are referring to this.
As said there, it's not really sure if it's legal or portable. That said I have seen code like this but never written it myself.

Is there any compilation of C terminology available on net? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Is there any compilation of C terminology available on net ? I feel it many times difficult to explain in exact technical terms what I wish to say.
Here is a compilation:
C Terminology
C Reference
Basic C
cdecl
cprogramming
CLC-wiki<---got from anon ymous to add
A to Z(C++ but there are helpful common terms)
A to Z(C/C++/C#)
wiki-book
Big tutorial
Tips and Tricks
There really is no substitute for the C Language Standard. The real deal from ISO is pretty expensive, but you can see a late draft at
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
The rationale can also be helpful.
http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf
Half compiled terminology are available at
CLC: terms
Programming Examples
and the best at 3. Terms, definitions, and symbols of C standard.

Why is FILE all-caps as in FILE*? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
This just seems odd to me, most other things are lower case.
Is there some historical reason?
It's a macro. Macros have historically had all caps in C. Unfortunately recent trends appear to have broken that fact.
Small history lesson: Also, FILE was an io buffer abstraction in UNIX v7 libc stdio. A FILE doesn't necessarily represent a physical file, just something that can do block IO. Source:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/stdio.h.html
"file" was already defined by the kernel as well:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/sys/file.h.html
As someone else said here it's probably a typedef now, but I don't think C had typedefs back in '79 as it only just had structs. Then again I wasn't born then so... :)
It's all caps almost certainly because at least originally it was a macro. Nowadays, chances are pretty good that it's a typedef instead, but nobody changed the name to boot (and doing so would be a pretty lousy idea).

How do you prefer between &cycle->read_events[i] and cycle->read_events+i [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
&cycle->read_events[i]
cycle->read_events+i
They are the same,how do you prefer one to the other?
Assuming cycle->read_events is a pointer or array, they are identical within the language and will compile identically.
However, I prefer &cycle->read_events[i], because someday you might decide to port to C++ and use a vector or other container class. Such classes are more likely to have a [] operator than to implement + as "pointer arithmetic". So this way makes it easier to change the data type without needing a recompile.
(Personally, I also find &a[x] to be more expressive of intent than a+x, but that is purely a matter of taste.)
According to The Elements of C Programming Style,
14.1 Use a[b] instead of *(a+b)
14.2 In general, use *a instead of a[0]
14.3 In general, use a+b instead of &a[b]
The rationales for each of these rules are roughly the same: it's shorter and usually clearer.

Resources