Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am trying to figure out which practice is the best between :
#define TEST
//#define TEST commented if not used or simply deleted
#define TEST 1
#define TEST 0 //if not used
For readability, I prefer to define a "Boolean" and check it in a if but I guess it is not efficient since it doesn't use #ifdef and it needs to be checked everytime in a if
While you can't use an #ifdef for your second case, you absolutely can use an #if so there would be no additional runtime cost for TEST 0. This btw. also means you can have multiple different values for TEST, maybe corresponding to different log levels or things like that
(Posted my comment as an answer, as suggested by Bathsheba)
Many moons ago some compilers (one for Solaris if I recall correctly) would not correctly compile code that used the first snippet.
But the C standard (now) requires them to and as such there is no benefit at all in using the second way which could now be regarded as idiosyncratic.
Of course if you need TEST to be a numerical value such as in a condition check as opposed to merely using #ifdef then you'd need to use the second way.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I saw this special macro when I read a source code. If I remember correctly, it is defined in the standard library.
The name of this macro is related to the buffer size, and in my machine its implementation is 1024.
Now I want to use it to initialize the buffer but I forgot what it is called.
So is there any one who can help me make my code look more professional?
If I don't know what I am looking for specifically, how can I clearly say what I need?
Are you talking about BUFSIZ? It's a macro provided by <stdio.h> and it expands to the size of the buffer used by setbuf().
I'm not sure what use it has in your own code.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have to call a function from main function like:
void main()
...
...
...
printf("Starting function- saveSubscriber");
status = saveSubscriber(io_ctr,
i_pRec,
/*&ufpEsn,*/ **/* Change #6*/**
iov_pmktbuf,
&i_pCntRec, **/* Change #23 */**
iv_pActvBuf->pr );
...
...
}
Is putting comments beside arguments (/* Change #6*/ and /* Change #23*/) okay? That is, will the code compile and will it function the same as when there are no comments.
The comment syntax is fine.
For me commenting arguments is a sign, that the name of the argument is not chosen clearly.
Moreover, commenting out a complete agrument leaves me a bit puzzled. It seams like somebody changed the signature of the function and was too lazy to clean it up properly.
This is perfectly fine, as others have said.
However, you might be able to avoid this if you use meaningful variable names.
I also find myself declaring a lot of differently named enums, which are basically just Boolean.
Which is clearer?
1)
processData(1); // you might add acomemnt to explain what 1 means.
// Many won't bother (especially those #~£%$!! who write
// code that I have to main years later !!)
2)
#define TRUE 1
#define FALSE 0
...
processData(TRUE);
3)
typedef enum {deleteDuplicates, retainDuplicates} howTohandleDuplicates_t;
processData(deleteDuplicates);
Always try your best to write maintainable, easy to read, code. Comments are fine, but well chosen variable names can obviate the need for most of them
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
As in the source code
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("\n\tQuadratic Equation Solver");
printf("\n\nUse this program to solve a quadratic equation.\n");
return 0;
}
I could have written all the text I wanted to output in a single statement, but I wrote it in two statements to make reading the source code easier for me. Is this considered bad programming practice and does it increase the execution time of the program?
It may increase the execution time of the program, as you're making multiple function calls, instead of just one. However, the difference will be negligible, especially because your terminal output (inside printf) is likely to be the bottleneck in the execution. It's also really of concern if you're printing a lot to the console. I think most would agree code readability is more important than extremely small optimizations such as combining the text.
the answer for **Is this considered bad programming practice and does it increase the execution time of the program? **
is No and No
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am adding a function to a library which is expected to be called millions of times. What would be the most efficient way to check if the function is passed a NULL pointer and exit if it is and continue if not given that it is expected that the pointer passed will be non-NULL most of the time. Any pointers would be welcome ...
Thanks
GNU C compiler has a __builtin_expect to tell the optimizer the most probable outcome of a value.
It is not standard C though.
You can #define macros to wrap around it and conditionally switch which definition to use, based on the compiler.
Linux kernel uses it this way (include/linux/compiler.h):
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
The ifs then become:
if (likely(an_expression)) { ... }
if (unlikely(another_expression)) { ... }
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got one question a very simple one.
while implementing a simple locking mechanism using global variable.I can't find out how to prevent the access of global variable from 2 processes/thread.
My algorithm (take 2 process)
Process p1 check if variable g is set then do not modify the code.
If not set then set it then modify code.
Same for process 2 .
while executing I got fair result but is it correct.
My doubt in some architecture if 2 instructions are not atomic then how to avoid accessing the global variable at a time.
please give me some basic idea.
Use atomic methods to manipulate the global variable.
Atomic operations are 'built-in' to gcc. While they are not generally 'portable', most compilers offer atomic operations.
For GCC, you might implement something like this:
if(__sync_bool_compare_and_swap(&g, g, g+1))
/* Changed by you. */;
else
/* Another process changed it before you could. Perhaps try again. */