Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help with some c programming

Status
Not open for further replies.

symphony888

Newbie level 5
Newbie level 5
Joined
Nov 28, 2011
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,467
{ int cnt = 5, a=1;
if ( - -a )
cnt = cnt – a;
else
a++;
}

I still can't figure out what is the value of 'a' in this case?

I know cnt will be 5. How bout a ?

Does anyone knows? Please explain thanks!
 

It will be 0.

Initially a will be one and it will be pre decremented so it becomes 1 - 1 = 0. cnt = 5 - 0 = 5.

Incorrect.

Reference the following explanations.


{ int cnt = 5, a=1;
if ( - -a )
cnt = cnt – a;
else
a++;
}

I still can't figure out what is the value of 'a' in this case?

I know cnt will be 5. How bout a ?

Does anyone knows? Please explain thanks!


I should point out, there is a considerable difference between the unary operator(s) preceding the variable a depending on the actual intend code.

Case 1, Original Code:
Code:
int cnt = 5, a=1;

if ( [COLOR="#FF0000"]- -a[/COLOR] )     //Parsed as two separate and consecutive tokens of unary minus (arithmetic negation), more clearly written, - (-a)
cnt = cnt – a;

else
a++;

The code of Case 1 results in cnt = 4 and a =1 and due to the conditional being evaluated as "- (-a)" for the following reasons:

1. Whitespace characters: space, end-of-line, vertical tab, form feed and horizontal tab, are generally ignored except when used to separate adjacent tokens or when inserted within character or string constants.

2. Unary operators ALL have associativity of from Right to Left.

During the parsing phase of if statement conditional the following occurs:

  • The first '-' is parsed as a token, following whitespace is skipped.
  • Then second '-' is parsed as a seperate token.
  • Then 'a' is parsed as a separate token.

During the compilation of the above three tokens:

  • The first '-' token is evaluated to be a unary minus operator due largely to the lack of an operand, neither lvalue nor rvalue, to the left of it.
  • The second '-' token is evaluated to be a unary minus operator due largely to the lack of an operand, neither lvalue nor rvalue, to the left of it.
  • The 'a' token is evaluated as symbol for the variable 'a', initialized to value of 1 the first operand to right of the unary minus operators.
  • Resulting in the compilation of -(-a), with parenthesis inserted for clarity of meaning.

Therefore during runtime the conditional "- -a" results in simply the value '1' which is evaluated as true conditional, any non-zero value.

Code:
if( - -a )

The if statement then passes execution flow to the following statement, which results in cnt = 4 and a = 1 and execution flow exits code block.

Code:
cnt = cnt – a;


One more note, as the unary minus operator is rarely used multiple times in succession, you may want to test these results with your specific C compiler.

The major of C compilers, especially embedded C compilers, do not fully support C89 nor C99, which may result in unexpected errors or results in such a case.




Case 2, Possibly Intended Code:

Code:
int cnt = 5, a=1;

if ( [COLOR="#FF0000"]--a[/COLOR] )     //Parsed as a single token of prefix decrement operator 
cnt = cnt – a;

else
a++;

If the intended operator was a single prefix decrement operator, the code of Case 2, the results are cnt = 5 and a = 1.

The results are due to the evaluation of the if statement conditional, "--a" resulting in a = 0 which evaluates conditionally as false, zero value.

Therefore branches over the immediately following statement:

Code:
 cnt = cnt - a;

And then transfers execution flow to else statement:

Code:
else
a++;

Which when executed results in the value of a = 1 and cnt = 5 remaining unchanged, the execution flow then exits the code block.



I would recommend referencing the following for an in-depth discussion of this and related topics:

C: A Reference Manual (5th Edition)

C Traps and Pitfalls


BigDog
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top