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 9 years ago.
I have a very short (and pretty much noob) question. I have a function that includes:
void function(int x)
{
x = 3;
if (x == 4)
printf("HI!");
x = 4;
}
Will the word HI! be printed? In other words, is a C program read sequentially or not?
Thanks a lot!
No, the code compiles to a sequence of instructions which happen sequentially one after the other. The comparison to 4 will always before the assignment x = 4. So it will be false.
This type of order is guaranteed when you are dealing with a single thread. When you have multiple threads you can get strange results and race-conditions unless you are careful.
It will not be printed. The line above is what matters of course.
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 9 years ago.
As an aside: Such a standard typedef would have help reduce the number of reserved words in C/C++, and relegated void to a simple type declaration, e.g. in <stddef.h>. (c.f. SRB in AB33/Mar 1972 - PDF=7kB)
That's pretty speculative. But one good reason is because void is not an empty value; it's the absence of a value. For instance, a function with a signature of int f(void) takes zero arguments, not one.
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.
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.
if (++bufCnt >= smenu->bufSize) //line1
if (bufCnt++ >= smenu->bufSize) //line2
In line1 bfrCnt is first incremented and then compared. In line2 it is first compared and then incremented. Is this correct?
Correct.
In both case you increase bufCnt.
But without else if you increase bufCnt two times.
Yes,
In line1 bfrCnt is first incremented and then compared.
In line2 it is first compared and then incremented.
Your question is not having the quality when it is compared to the other questions in this site. That is why you are getting down votes.
So when ever you are posting a question, make sure it is having enough standard.
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 want to use a stack in C, anybody recommend a library?
For example for a hash table I used UThash.
Thanks!
Stack implementation fits in single sheet of paper.
That's simplest stack example
int stack[1000];
int *sp;
#define push(sp, n) (*((sp)++) = (n))
#define pop(sp) (*--(sp))
...
{
sp = stack; /* initialize */
push(sp, 10);
x = pop(sp);
}
Here is a similar question:
Are there any open source C libraries with common data structures?
And here is CCAN, C's equivalent to CPAN:
http://ccan.ozlabs.org/
If you can fudge it a bit and use C++, Qt is a really great library with a lot of basic data structures.