Why the control goes in "else" part? [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
int a = 8;
if (a==8)
printf("x");
else
printf("y");
Though a is equal to 8, it outputs y.

The code above always prints x. If your code prints something else, then you omitted vital information in your question.
To find out what that might be, try this:
Insert #undef a before the int a = 8; to make sure there isn't a C preprocessor macro that messes with the code.
Swap the condition to see if a is really what you expect:
if( 8 == a )
This little trick also prevents you from the accidental assignment bug (if( a = 8 ))

Related

C variable initialization by default [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In C, why does the following result in x[1] being 2?
int a = 2, x;
...
printf("x[1] = ", &x[1])
It doesn't. It results in undefined behaviour where anything can happen. You cannot access elements beyond the end of an array in a defined manner.
What's most likely happening is that a is just "above" x on the stack, which results in x[1] having the same address as a, but it's by no means guaranteed.
This is, of course, assuming that your printf is a typo. As it stands, it doesn't even compile. I'm assuming it's a typo since the question title just asks about the value of x[1] rather than the output.
To get it to work, you'd have to use something like:
printf ("x[1] = %d\n", (&x)[1]);
which also prints 2 on my system, but may do something totally different elsewhere.

Optimize this C Code [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How to Optimize this piece of C code...??
int c = no, diff = u - d;
while (no--)
for (d = u; d < p[no]; d += diff)
c++;
The best optimization, for size, speed, cleverness, clarity, and anything else you may think of, is to have no code.
So, just remove those 4 lines from your source(s) and you've optimized your code.

C - Left Shift or Add to Itself (Speed) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Which is executes faster?
1:
n = n << 1;
2:
n = n + n;
Any good compiler will end up making them both the same, so I can't imagine it matters.
In principle, << can be faster for signed types because it's less-strictly defined. n+n is defined whenever it doesn't overflow, but n<<1 is defined only when n is non-negative and the result does not overflow.
In reality, the compiler will generate the exact same machine code for both.

for loop syntax execution in C language [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
IN C what will be the actual execution when the for loop syntax is for (initializer;incrementation;condition)
eg:
for(i=1;i<100;i++)
{
printf("%d",i);
}
It will be
123456789...99
Unless your libc doesn't flush stdout on close. Or are you asking how it works, in which, case, It's equavalent to:
initializer;
while(condition){
...
incrementation
}
or
i=1;
while(i<100){
printf("%d", i);
i++;
}

Some really ugly C macro [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 13 years ago.
I need some really ugly C macro (as an example, not to any real project), that does some simple thing (like adding one, for example) really hard and unreadable way.
Anyone has some idea?
#define new delete
#define True 0
#define False -1
#define BEGIN {

Resources