C - Precedence increment/decrement? [duplicate] - c

This question already has answers here:
Can you have a triple minus signs in C programming? What does it mean? [duplicate]
(5 answers)
Why doesn't a+++++b work?
(9 answers)
Closed 6 years ago.
I just started learning C Programming and I have a question based on expression evaluation.
If we have 3 variables, a,b, and c:
c=a+++b++;
c=a+++++b;
Why is the 1st expression valid and 2nd invalid?

It appears that the C compiler does interpret a+++ as a++ +, while +++b generates an error even if you put another variable before it.
In practice it is a very bad idea to write such expressions without spaces. You would confuse yourself when you come back to look at your code, and annoy anyone else looking at it :) Just rewrite:
a++ + b++
a++ + ++b
and everything will work as expected.

Related

Using a dot for variable declaration in C [duplicate]

This question already has answers here:
What is the meaning of a dot (.) after an integer in c?
(2 answers)
Closed 2 years ago.
I read a code online and the next line caught my attention since I don't know why does it have a "." after the 0:
variable=0.;
I couldn't find the answer after looking for it. Could you please tell me what is the dot for?
Thanks!!
The dot makes it a double. A clearer way to write it is 0.0.

In C, why were two different operators (. and ->) defined to work with structures? [duplicate]

This question already has answers here:
Arrow Operator vs. Dot Operator [closed]
(2 answers)
Why does the arrow (->) operator in C exist?
(3 answers)
Closed 4 years ago.
Let's be clear:
I know the difference between the . and the -> operators
When working with a structure, the . operator is a simple offset added to the address of the structure. When working with a pointer to a structure, the -> operator first dereferences the pointer and then adds the offset.
My question is: Why were two operators introduced in the first place?
The compiler obviously knows whether I'm working with a structure or with a pointer to a structure, so it shouldn't require me to use the proper operator. With the single . operator, it could dereference the pointer by itself first if working with a pointer to a structure.
I ask myself this question every single time I use the improper operator, and I couldn't find the answer.

why does parenthesis needed for assignment in second part of conditional operator in c [duplicate]

This question already has answers here:
Assignment statement used in conditional operators
(6 answers)
Closed 6 years ago.
The code below
i>0?j=9:i=7;
Is giving an error:
lvalue required as left operand of assignment
Can you explain this why I am getting this error?
when, i>0?j=9:(i=7); is working correctly
Imho: basically your problem is that is C the termary operator has higher priority than "=" (in c++ the prioriy is equal, so it works as expected) and that is the reason you need the parenthesis.

C Programming Increment and Decrement Operator Problem [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
$void main()
{
int a=10,c;
c= ++a + ++a;
printf("%d",c);
}
this program Actualy Print Value Of c=24 but By Calculation we can say it should be
c=23 ,how it possible?
Your program has a bug -- you modify the same variable twice without an intervening sequence point. Fix the bug and the mystery will go away.
A very deep understanding of not just how the language works but how compilers work is required to understand why buggy code happens to do what it happens to do. I would just suggest not writing buggy code and, when you find a bug, simply fix it instead of trying to understand precisely why and how it broke.
My advice to you is to stop. You learned the right lesson -- code that triggers undefined behavior is unpredictable and frequently doesn't do what you might expect it to do. That's all you need to know about UB until you're an expert at using the language correctly.
'++' > '+'
Here post increment operation is done before.Since you gave it two times if does post increment two times so the value of 'a' becomes 12 and adds it up (12+12).So the final value is 24.

an easy c question [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
What belongs in an educational tool to demonstrate the unwarranted assumptions people make in C/C++?
In C99, is f()+g() undefined or merely unspecified?
In the statement, "function1() + function2(); "which function will be called first?
The order of evaluation is unspecified.
Read this answer.

Resources