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.
In C if i have only one line in the if condition then can i combine it into one line to reduce the size of code. The size of code is long so I intend to take this step to reduce size of code. Should I should I not? Is it a good programming practice?
It makes no sense to be concerned about the size of the sources at the point of sacrificing readability, especially since sources are extremely small compared to almost any kind of other data that our computers usually process/store (e.g. the bzipped sourced of the Whole Firefox are 85 MB - smaller than any medium-length video). Also, omitting a newline won't change the compilation times of a millisecond.
So, if you prefer one-line ifs for your own stylistic reasons it's fine (although it's often frowned upon), but for saving a few bytes it makes no sense at all.
Actually if the code is long then it's not good programming practice as that code will not be as readable.
so it's better to use if else syntax to make your code readable
Related
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).
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.
I tried googling it, but no solid answer. What are available, still maintained dynamic array implementations for C? What are pros and cons for every one of them, and what is the best one(speed/footprint ratio)? Just asking, so that I don't have to reinvent the wheel.
GArray from GLib does what you want.
If you are looking for something like NSMutableArray, from Objective-C, or something like ArrayList from Java, you won't find anything (std C, at least).
You can create your own dynamic array implementation in C, though. It will take you a few code lines and is not that hard to implement.
All you need to have in mind is Time vs Memory. You can do an implementation that allocates a new array with a bigger size, every time you push/add an element, and then pops it for you in the return or by reference, or you can reallocate memory every time. I don't see big advantages in neither one, except that realloc is a C library function that I think is low level implemented, meaning it is probably faster, and in matters of implementation I would go with the realloc one since it is faster to implement.
You can even build an api that gives you sorting types and clean all methods.
Now is up to you.
Hope this helps.
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.
I've finished a project that relies heavily on string manipulation in C. The problem is that should I change my global variables to pass by reference instead? Code is longer than 300 lines, and the global variables seem to be doing fine. I've read other posts that pbr can increase readability.
I have about 6 global vars.
You should always aim to keep your state as small as reasonably possible. Having big state (roughly: at any point in the code, the number of accessible (dependable) variables is high) means more more responsibility at each point, more difficult maintenance, less intuitive to read.
So yes, global variables only where you can't bear passing them to every little function.
If you have a throw-away program, global state is obviously ok, but then you normally don't start in C in the first place.
6 global vars doesn't sound much, but you can't generalize.
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.
Consider the below code snippet:
for(i=0;i<10;i+=2) // 1
for(i=0;i<2;i=i+2) // 2
Which one will be better to use?
Does it make any difference in the performance?
The following took 0.0260015 seconds
for (i = 0 ; i < 10000000 ; i += 2)
And this took 0.0170010
for (i = 0 ; i < 10000000 ; i = i + 2)
#MasterID is right though when I enabled 'optimize code' both reported 0.0150009 seconds
There is no definite answer to your question. It depends on how smart your compiler is among other things (optimization level, ...) and on the target platform. This is not a C language question. The language is not more or less performant by itself. It just depends on what the compiler builds out of it. So test it for your use case if performance matters at all...
Otherwise my advice, just write it in the way you feel it more readable.
The first option is as fast as the second, at least.
Although any compilation optimization would generate the same assembly code.
Both express the exact same semantics, i.e. the exact same effect in the abstract machine of the C language. If one is slower than the other, it's a quality-of-implementation flaw in your compiler.
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).