Click here to Skip to main content
15,895,256 members
Home / Discussions / C#
   

C#

 
GeneralRe: A suggestion Pin
Jonathan C Dickinson10-Aug-10 0:11
Jonathan C Dickinson10-Aug-10 0:11 
GeneralRe: A suggestion Pin
Richard MacCutchan10-Aug-10 0:43
mveRichard MacCutchan10-Aug-10 0:43 
GeneralRe: A suggestion Pin
Jonathan C Dickinson10-Aug-10 1:17
Jonathan C Dickinson10-Aug-10 1:17 
GeneralRe: A suggestion Pin
Richard MacCutchan10-Aug-10 1:48
mveRichard MacCutchan10-Aug-10 1:48 
GeneralRe: A suggestion Pin
Jonathan C Dickinson10-Aug-10 21:25
Jonathan C Dickinson10-Aug-10 21:25 
GeneralRe: A suggestion Pin
Keith Barrow7-Aug-10 5:13
professionalKeith Barrow7-Aug-10 5:13 
GeneralRe: A suggestion Pin
Richard MacCutchan7-Aug-10 7:12
mveRichard MacCutchan7-Aug-10 7:12 
GeneralRe: A suggestion [modified] Pin
PIEBALDconsult7-Aug-10 16:58
mvePIEBALDconsult7-Aug-10 16:58 
I used to work in a shop (doing just plain C) that had that specified in the coding standards. It made no sense to me, but I went along. One day I was talking with "the guru" (who probably wrote the standard) and he agreed that it was pointless so he didn't enforce it.

The basic problem is that it only works when comparing an Lvalue and an Rvalue; many times that is not the case, so you need to think about what you are doing and recognize the situation. And if you can do that, then chances are you won't make that mistake anyway.

When it comes right down to it; anyone who is prone to that kind of mistake is also unlikely to remember the rule.


By the way... HP C V7.3-009 on OpenVMS Alpha V8.3 says:

JB> cc aa.c /warn=(enable=(check),verbose)

    if ( result = 5 )
....^
%CC-I-CONTROLASSIGN, In this statement, the assignment expression "result=5" is used as the controlling expression of an if, while o
r for statement.
at line number 13 in file MY$ROOT:[000000]AA.C;3
Description: A common user mistake is to accidentally use assignment operator "=" instead of the equality operator "==" in an expres
sion that controls a transfer. For example saying if (a = b) instead of if (a == b).  While using the assignment operator is valid,
it is often not what was intended.  When this message is enabled, the compiler will detect these cases at compile-time. This can oft
en avoid long debugging sessions needed to find the bug in the user's program.
User Action: Make sure that the assignment operator is what is expected.

    if ( 5 = result )
.........^
%CC-E-NEEDLVALUE, In this statement, "5" is not an lvalue, but occurs in a context that requires one.
at line number 18 in file MY$ROOT:[000000]AA.C;3
Description: An expression that must be an lvalue was not an lvalue.  For example, the operand of the address-of operator must be an
 lvalue.
User Action: Modify the expression so that it is an lvalue.



Edit:

C:\>cc aa
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
aa.c:
Warning W8060 aa.c 14: Possibly incorrect assignment in function main

C:\>\mingw\bin\gcc -Wall aa.c
aa.c: In function `main':
aa.c:14: warning: suggest parentheses around assignment used as truth value


modified on Saturday, August 7, 2010 11:40 PM

AnswerRe: A suggestion Pin
Luc Pattyn7-Aug-10 17:01
sitebuilderLuc Pattyn7-Aug-10 17:01 
GeneralRe: A suggestion Pin
Richard MacCutchan7-Aug-10 21:23
mveRichard MacCutchan7-Aug-10 21:23 
GeneralRe: A suggestion Pin
Jonathan C Dickinson9-Aug-10 21:40
Jonathan C Dickinson9-Aug-10 21:40 
GeneralRe: A suggestion Pin
Stefan_Lang11-Aug-10 5:13
Stefan_Lang11-Aug-10 5:13 
GeneralRe: A suggestion Pin
Richard MacCutchan11-Aug-10 7:31
mveRichard MacCutchan11-Aug-10 7:31 
GeneralRe: A suggestion Pin
Stefan_Lang11-Aug-10 23:02
Stefan_Lang11-Aug-10 23:02 
GeneralRe: A suggestion Pin
Richard MacCutchan12-Aug-10 1:30
mveRichard MacCutchan12-Aug-10 1:30 
AnswerRe: Cannot implicitly convert type 'object' to 'bool' PinPopular
Orjan Westin7-Aug-10 9:06
professionalOrjan Westin7-Aug-10 9:06 
GeneralRe: Cannot implicitly convert type 'object' to 'bool' PinPopular
Richard MacCutchan7-Aug-10 11:23
mveRichard MacCutchan7-Aug-10 11:23 
GeneralRe: Cannot implicitly convert type 'object' to 'bool' Pin
Orjan Westin7-Aug-10 12:12
professionalOrjan Westin7-Aug-10 12:12 
GeneralRe: Cannot implicitly convert type 'object' to 'bool' Pin
Richard MacCutchan7-Aug-10 21:19
mveRichard MacCutchan7-Aug-10 21:19 
GeneralRe: Cannot implicitly convert type 'object' to 'bool' Pin
ErrolErrol9-Aug-10 7:59
ErrolErrol9-Aug-10 7:59 
JokeRe: Cannot implicitly convert type 'object' to 'bool' Pin
Orjan Westin10-Aug-10 2:27
professionalOrjan Westin10-Aug-10 2:27 
AnswerRe: Cannot implicitly convert type 'object' to 'bool' Pin
PIEBALDconsult7-Aug-10 17:32
mvePIEBALDconsult7-Aug-10 17:32 
AnswerRe: Cannot implicitly convert type 'object' to 'bool' Pin
cor28799-Aug-10 4:31
cor28799-Aug-10 4:31 
GeneralRe: Cannot implicitly convert type 'object' to 'bool' Pin
Jonathan C Dickinson9-Aug-10 21:52
Jonathan C Dickinson9-Aug-10 21:52 
GeneralRe: Cannot implicitly convert type 'object' to 'bool' Pin
cor287910-Aug-10 1:59
cor287910-Aug-10 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.