Three stupid C questions: ++,...

Discussion in 'C Programming' started by jean francois, Jun 11, 2012.

  1. Hi All,

    I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
    world.
    Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
    any suggestions :) ) and start learning C/C++, I need to translate a C
    prog into TP.

    Here are the supid questions:

    1st:

    I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
    is.
    If the program does 'MyPointer = p++' what does this mean?

    1) MyPointer = p; increment p of 1;
    2) MyPointer = p; Increment p of SizeOf(CStruct);
    3) increment p of 1; MyPointer = p;
    4) increment p op SizeOf(CStruct); MyPointer = p;
    5) something else?

    Second stupid question:

    'if !(a = b)' means

    1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    '=')
    2) a=b and if b=0 then....
    3) something else?

    Third stupid question:

    When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
    couldn't find the 'dealloc' function in the prog.

    If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
    net...

    Thanks a lot for your help, and please excuse the stupidity of my
    questions.
     
    jean francois, Jun 11, 2012
    #1
    1. Advertising

  2. jean francois

    BartC Guest

    "jean francois" <> wrote in message
    news:jr5lcs$4jr$...

    > I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
    > is.
    > If the program does 'MyPointer = p++' what does this mean?
    >
    > 1) MyPointer = p; increment p of 1;
    > 2) MyPointer = p; Increment p of SizeOf(CStruct);
    > 3) increment p of 1; MyPointer = p;
    > 4) increment p op SizeOf(CStruct); MyPointer = p;
    > 5) something else?


    Something between (1) and (2); arithmetic on pointers is in object-size
    units, not bytes.

    So if Cstruct was 12 bytes, then:

    p=p+1;
    ++p;

    would both point p to the next struct (adding 12 bytes to the actual value).

    > Second stupid question:
    >
    > 'if !(a = b)' means


    The syntax is wrong; probably if (!(a=b)), which assigns b to a, then
    effectively does:

    if (!a)

    So the condition is true when a (and b) is zero.

    > 1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    > '=')
    > 2) a=b and if b=0 then....
    > 3) something else?


    Check out if (a!=b) too; you already know that actual equality is written as
    a==b.

    >
    > Third stupid question:
    >
    > When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
    > couldn't find the 'dealloc' function in the prog.


    Standard C has malloc() and free(); perhaps the equivalent is _ffree()?

    In any case, no automatic de-allocation is done (in C; don't know about
    C++), except when the program itself terminates. (Doesn't Pascal work the
    same way?)

    > If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
    > net...


    comp.lang.c covers C, and comp.lang.c++ covers the other language (if you
    thought C was difficult, ...).


    --
    Bartc
     
    BartC, Jun 11, 2012
    #2
    1. Advertising

  3. jean francois <> writes:

    > I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
    > world.
    > Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
    > any suggestions :) ) and start learning C/C++, I need to translate a C
    > prog into TP.
    >
    > Here are the supid questions:
    >
    > 1st:
    >
    > I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
    > is.


    The symbols here don't mean much so I stick with the words: p is
    a pointer to an object of type CStruct.

    > If the program does 'MyPointer = p++' what does this mean?
    >
    > 1) MyPointer = p; increment p of 1;
    > 2) MyPointer = p; Increment p of SizeOf(CStruct);


    (aside: it's sizeof in C and C is case sensitive)

    > 3) increment p of 1; MyPointer = p;
    > 4) increment p op SizeOf(CStruct); MyPointer = p;
    > 5) something else?


    MyPointer gets the prior value of p, before anything happens to it, so
    that's one thing out of the way.

    Both (1) and (2) have an element of truth about them, but they both
    suggest a confusion that needs to be sorted out.

    p++ adds one to p (i.e. writing p = p + 1 has the same effect) so (1) is
    a correct statement. However, pointers in C are not simply addresses --
    they have a type too -- and adding one to a pointer makes it point to
    the next element of the target type in an array which is what some
    people might mean when they write (2).

    The key is that p will be altered so that it points to the next CStruct
    in some array of CStructs.

    > Second stupid question:
    >
    > 'if !(a = b)' means
    >
    > 1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    > '=')
    > 2) a=b and if b=0 then....
    > 3) something else?


    (3). It's a syntax error. You might have meant:

    if (!a == b)
    if (a != b)
    if (!(a == b))

    and I don't really want to guess.

    > Third stupid question:
    >
    > When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
    > couldn't find the 'dealloc' function in the prog.


    As a general rule, most modern systems release all memory that a program
    has used when it exits. But _fmalloc is not a "well known" function so
    I can't say for sure what it does.

    > If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
    > net...


    Ah. If you have a C++ program, this is not the best place to ask.
    comp.lang.c++ also exists. You might also want to try
    alt.comp.lang.learn.c-c++

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Jun 11, 2012
    #3
  4. jean francois

    John Gordon Guest

    In <jr5lcs$4jr$> jean francois <> writes:

    > I've got p as a pointer on a CStruct. p = *CStruct.


    Are you sure you meant to have that asterisk before Cstruct? Did you
    mean to have an ampersand instead?

    > No matter was CStruct is.
    > If the program does 'MyPointer = p++' what does this mean?


    Since the ++ comes after the p, MyPointer is set equal to p and then p is
    incremented by one width of Cstruct. (assuming p is declared as
    Cstruct *p).

    > 'if !(a = b)' means


    Since only one equal sign was used, it is an assignment, not a test for
    equality.

    a is set equal to the value of b, and as a side effect, the value of a is
    returned. this value is then negated, and if the negated value is true,
    the if-block is executed.

    In a slightly simpler form, this is equivalent to:

    a = b;
    if (!a)
    {
    ...
    }


    > Third stupid question:


    > When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
    > couldn't find the 'dealloc' function in the prog.


    All allocated memory is freed upon program exit. I don't know specifically
    what _fmalloc() does, but I'd be very surprised if it did anything
    different.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jun 11, 2012
    #4
  5. On Mon, 11 Jun 2012 20:45:16 +0000 (UTC), jean francois
    <> wrote:

    > Hi All,
    >
    >I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
    >world.
    >Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,


    Visual C++ 2010 Express is free from Microsoft. I'm sure there are
    other free compilers which others may choose to mention.

    >any suggestions :) ) and start learning C/C++, I need to translate a C


    Be aware that C and C++ are two separate languages with just enough
    commonality to insure newcomers become confused. Pick one language to
    learn and stick with it until you are comfortable before trying to
    learn the other.

    >prog into TP.
    >
    >Here are the supid questions:
    >
    >1st:
    >
    >I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
    >is.
    >If the program does 'MyPointer = p++' what does this mean?


    Except for void*, pointers in C always point to an object of a
    particular type (so you can access that object by dereferencing the
    pointer). Regardless of the type of the object, p++ will always cause
    the pointer to point to the byte immediately following the object that
    was pointed to. In the mathematical (as opposed to pointer
    arithmetic) sense, p++ adds sizeof(object) to p.

    >
    >1) MyPointer = p; increment p of 1;
    >2) MyPointer = p; Increment p of SizeOf(CStruct);
    >3) increment p of 1; MyPointer = p;
    >4) increment p op SizeOf(CStruct); MyPointer = p;
    >5) something else?
    >
    >Second stupid question:
    >
    >'if !(a = b)' means


    The ! operator evaluates to 1 if its operand evaluates to 0. If the
    operand evaluates to any non-zero value, then the ! operator evaluates
    to 0.

    In your case, the operand is (a=b) which is an assignment expression.
    An assignment expression evaluates to the final value of the left hand
    operand (a in this case). As you note below, this probably should be
    if !(a == b)
    which is exactly equivalent to
    if (a != b)

    >
    >1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    >'=')
    >2) a=b and if b=0 then....
    >3) something else?
    >
    >Third stupid question:
    >
    >When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
    >couldn't find the 'dealloc' function in the prog.


    _fmalloc is not a standard C function. For the standard malloc
    function, the memory remains allocated until it is explicitly freed by
    calling the standard free function.

    When you say "exit" above, if you mean a function other than main
    returning to the function that called it, then the allocated memory is
    not deallocated. The standard does not address what happens when main
    exits (returns to the operating system service that invoked the
    program). Most modern general purpose operating systems will clean up
    any residual resources, including allocated memory, when the program
    exits. I would not be surprised if this did not happen on some
    imbedded systems where main is never expected to exit.

    >
    >If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
    >net...


    C++ is discussed in comp.lang.c++, down the hall, third door on the
    left, just past the water cooler.

    --
    Remove del for email
     
    Barry Schwarz, Jun 11, 2012
    #5
  6. jean francois

    John Gordon Guest

    In <jr5pn5$ebm$> John Gordon <> writes:

    > > 'if !(a = b)' means


    > Since only one equal sign was used, it is an assignment, not a test for
    > equality.


    Ignore my answer to this one; Ben is right. It's a syntax error.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jun 11, 2012
    #6
  7. jean francois

    Geoff Guest

    On Mon, 11 Jun 2012 20:45:16 +0000 (UTC), jean francois
    <> wrote:

    > Hi All,
    >
    >I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
    >world.
    >Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
    >any suggestions :) ) and start learning C/C++, I need to translate a C
    >prog into TP.
    >
    >Here are the supid questions:
    >
    >1st:
    >
    >I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
    >is.


    p = *CStruct if p is a pointer to CStruct is an error.

    >If the program does 'MyPointer = p++' what does this mean?
    >
    >1) MyPointer = p; increment p of 1;
    >2) MyPointer = p; Increment p of SizeOf(CStruct);
    >3) increment p of 1; MyPointer = p;
    >4) increment p op SizeOf(CStruct); MyPointer = p;
    >5) something else?


    If p is a pointer to an object of any size, then p++ or p+1 points to
    the next object of that size.

    If p points to CStruct, then p++ increments p by sizeof(CStruct).
    There is no SizeOf operator. It's sizeof(CStruct).

    The statement MyPointer = p++; makes MyPointer point to the object
    pointed to by p before p was incremented to point to the next object
    of that type.

    >
    >Second stupid question:
    >
    >'if !(a = b)' means
    >
    >1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    >'=')
    >2) a=b and if b=0 then....
    >3) something else?


    The statement if !(anything) is a syntax error.
    The assignment operator is =.
    a = b makes a equal to b.

    The comparison operator is ==. Comparing a to b is written a == b.
    The statement if (a==b) is true when a is equal to b.
    The statement if (a!=b) is true when a is not equal to b.

    >
    >Third stupid question:
    >
    >When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
    >couldn't find the 'dealloc' function in the prog.


    The statement free() deallocates memory allocated by the malloc family
    of functions. The opposite of _fmalloc() was _ffree().

    _fmalloc() is an ancient Microsoft function for allocating FAR memory
    using an ancient x86 memory model. You are looking at some VERY OLD C
    code if you are seeing it used, probably MS-DOS code.

    >
    >If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
    >net...
    >

    Some quick links found from Google:

    http://msdn.microsoft.com/en-us/library/fw5abdx6.aspx

    http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html

    http://www.cprogramming.com/reference/
     
    Geoff, Jun 11, 2012
    #7
  8. jean francois

    MarkBluemel Guest

    On 06/11/2012 09:45 PM, jean francois wrote:
    > Hi All,
    >
    > I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
    > world.


    You may know what you mean by TP/Pas. I can possibly guess, but it might
    have been helpful for you to be clearer. The electrons wouldn't have
    cost much.

    > Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
    > any suggestions :) ) and start learning C/C++, I need to translate a C
    > prog into TP.


    No you don't really. You need to think about a) about whether you want
    to learn C or C++ and b) look at some decent reference material.

    Most of what you want answered here would have been covered by a good
    tutorial text.

    If you are an experienced programmer and want to learn C, Kernighan and
    Ritchie is a good choice. I can't comment about less experience or C++.

    >
    > Here are the supid questions:
    >
    > 1st:
    >
    > I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
    > is.
    > If the program does 'MyPointer = p++' what does this mean?


    Any tutorial text would cover this.


    > Second stupid question:
    >
    > 'if !(a = b)' means


    And this.

    > Third stupid question:
    >
    > When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
    > couldn't find the 'dealloc' function in the prog.


    A tutorial text may give you enough knowledge to detect that this is a
    non-standard function, so the behaviour is defined by the implementation
    not the specification.
     
    MarkBluemel, Jun 12, 2012
    #8
  9. jean francois

    Nomen Nescio Guest

    jean francois <> wrote:

    > Hi All,
    >
    > I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
    > world.
    > Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
    > any suggestions :)


    Borland has free older copies of their C/C++. Since you are targeting older
    platforms you can use these and they should be ok or even good. And they are
    free. Search for Borland Museum..

    DigitalMars.com has C/C++ for those platforms as well, is free, and is
    written by one of the top compiler writers of all time. Still maintained!

    ) and start learning C/C++, I need to translate a C
    > prog into TP.


    Better than the other way around!
     
    Nomen Nescio, Jun 12, 2012
    #9
  10. jean francois

    Guest

    On Monday, June 11, 2012 9:45:16 PM UTC+1, jean francois wrote:

    > I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
    > world.
    > Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
    > any suggestions :) ) and start learning C/C++, I need to translate a C
    > prog into TP.

    [...]
    > I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
    > is.
    > If the program does 'MyPointer = p++' what does this mean?


    as a general rule when asking a question about C (or any other programming language) it is best to post small complete *correct* fragments of code.

    I'm not sure if CStruct is part of your code or not in
    "I've got p as a pointer on a CStruct. p = *CStruct."
    is CStruct a type (ie. a struct) or a variable?

    <snip>
     
    , Jun 12, 2012
    #10
  11. jean francois

    Noob Guest

    Noob, Jun 12, 2012
    #11
  12. jean francois

    James Kuyper Guest

    On 06/12/2012 08:16 AM, wrote:
    ....
    > as a general rule when asking a question about C (or any other programming language) it is best to post small complete *correct* fragments of code.


    Even if the question is "Is this correct?" :)

    That exception doesn't apply in this case, but it's still quite clear
    that the OP doesn't know C well enough to distinguish correct code and
    incorrect code.
    --
    James Kuyper
     
    James Kuyper, Jun 12, 2012
    #12
  13. jean francois

    Neil Cerutti Guest

    Neil Cerutti, Jun 12, 2012
    #13
  14. jean francois

    Fred K Guest

    On Monday, June 11, 2012 2:53:08 PM UTC-7, Ben Bacarisse wrote:
    > jean francois <> writes:
    >
    > > I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
    > > world.
    > > Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
    > > any suggestions :) ) and start learning C/C++, I need to translate a C
    > > prog into TP.
    > >
    > > Here are the supid questions:
    > >
    > > 1st:
    > >
    > > I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
    > > is.

    >
    > The symbols here don't mean much so I stick with the words: p is
    > a pointer to an object of type CStruct.
    >
    > > If the program does 'MyPointer = p++' what does this mean?
    > >
    > > 1) MyPointer = p; increment p of 1;
    > > 2) MyPointer = p; Increment p of SizeOf(CStruct);

    >
    > (aside: it's sizeof in C and C is case sensitive)
    >
    > > 3) increment p of 1; MyPointer = p;
    > > 4) increment p op SizeOf(CStruct); MyPointer = p;
    > > 5) something else?

    >
    > MyPointer gets the prior value of p, before anything happens to it, so
    > that's one thing out of the way.
    >
    > Both (1) and (2) have an element of truth about them, but they both
    > suggest a confusion that needs to be sorted out.
    >
    > p++ adds one to p (i.e. writing p = p + 1 has the same effect) so (1) is
    > a correct statement. However, pointers in C are not simply addresses --
    > they have a type too -- and adding one to a pointer makes it point to
    > the next element of the target type in an array which is what some
    > people might mean when they write (2).
    >
    > The key is that p will be altered so that it points to the next CStruct
    > in some array of CStructs.
    >
    > > Second stupid question:
    > >
    > > 'if !(a = b)' means
    > >
    > > 1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    > > '=')
    > > 2) a=b and if b=0 then....
    > > 3) something else?

    >
    > (3). It's a syntax error. You might have meant:
    >
    > if (!a == b)
    > if (a != b)
    > if (!(a == b))
    >
    > and I don't really want to guess.



    Why is this a syntax error? Is the following a syntax error?
    int c = (a=b);
    if ( !(c) ) {...}
     
    Fred K, Jun 12, 2012
    #14
  15. jean francois

    James Kuyper Guest

    On 06/12/2012 11:01 AM, Fred K wrote:
    > On Monday, June 11, 2012 2:53:08 PM UTC-7, Ben Bacarisse wrote:
    >> jean francois <> writes:

    ....
    >>> 'if !(a = b)' means
    >>>
    >>> 1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    >>> '=')
    >>> 2) a=b and if b=0 then....
    >>> 3) something else?

    >>
    >> (3). It's a syntax error. You might have meant:
    >>
    >> if (!a == b)
    >> if (a != b)
    >> if (!(a == b))
    >>
    >> and I don't really want to guess.

    >
    >
    > Why is this a syntax error?


    Because the only things allowed between the keyword "if" and the
    condition associated with the "if" are a mandatory '(' and optional
    whitespace, including comments. "!" is not allowed.

    > Is the following a syntax error?
    > int c = (a=b);
    > if ( !(c) ) {...}


    No, but

    if !(c) { ... }

    is a syntax error, of exactly the same type as

    if !(a=b) { ... }
     
    James Kuyper, Jun 12, 2012
    #15
  16. jean francois

    John Gordon Guest

    In <> Fred K <> writes:

    > > > 'if !(a = b)' means

    > >
    > > (3). It's a syntax error.


    > Why is this a syntax error?


    It's a syntax error because '!' is between 'if' and '(', which
    is not allowed.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jun 12, 2012
    #16
  17. jean francois

    Eric Sosman Guest

    On 6/12/2012 11:01 AM, Fred K wrote:
    > On Monday, June 11, 2012 2:53:08 PM UTC-7, Ben Bacarisse wrote:
    >> jean francois <> writes:
    >> [...]
    >>> Second stupid question:
    >>>
    >>> 'if !(a = b)' means
    >>>
    >>> 1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    >>> '=')
    >>> 2) a=b and if b=0 then....
    >>> 3) something else?

    >>
    >> (3). It's a syntax error. You might have meant:
    >>
    >> if (!a == b)
    >> if (a != b)
    >> if (!(a == b))
    >>
    >> and I don't really want to guess.

    >
    >
    > Why is this a syntax error? Is the following a syntax error?
    > int c = (a=b);
    > if ( !(c) ) {...}


    Expanding on the explanations by James Kuyper and John Gordon:
    The parentheses are part of the syntax of the `if' statement itself,
    not just incidental parts of the expression being tested. Even if
    the expression is just a variable used as a Boolean, you must write

    if ( varname )
    and not
    if varname

    Similarly, parentheses are part of the required syntax of `while'
    and `do...while' and `for' and `switch': All of these involve an
    expression (or more than one), *plus* parentheses.

    This may at first seem weird, but in fact it's essential: It
    removes an ambiguity. If parentheses were not required you might
    write

    if varname
    *ptr = 42;

    See the problem? No? Try putting it all on one line

    if varname * ptr = 42 ;

    Now, what's the expression being tested? Is it `varname', or the
    multiplication `varname * ptr', or the assignment `varname * ptr = 42'?
    Where does the test expression end, and where does the governed stuff
    begin? (True, some of the possible parses would run afoul of other
    constraints, but it makes for easier parsing if the compiler can figure
    deduce the syntactic structure sooner rather than later.)

    Some languages avoid this difficulty by using something else as
    a separator, for example

    if varname then * ptr = 42 ;
    ^^^^

    .... and there are other ways out of the trouble, too. But C's way is
    to insist that the test expression (or "expressions," for `for') be
    surrounded by parentheses

    if ( varname ) * ptr = 42 ;

    if ( (a + b) * c > (d + e) / f ) ...

    --
    Eric Sosman
    d
     
    Eric Sosman, Jun 12, 2012
    #17
  18. jean francois

    Fred K Guest

    On Tuesday, June 12, 2012 8:45:26 AM UTC-7, James Kuyper wrote:
    > On 06/12/2012 11:01 AM, Fred K wrote:
    > > On Monday, June 11, 2012 2:53:08 PM UTC-7, Ben Bacarisse wrote:
    > >> jean francois <> writes:

    > ...
    > >>> 'if !(a = b)' means
    > >>>
    > >>> 1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
    > >>> '=')
    > >>> 2) a=b and if b=0 then....
    > >>> 3) something else?
    > >>
    > >> (3). It's a syntax error. You might have meant:
    > >>
    > >> if (!a == b)
    > >> if (a != b)
    > >> if (!(a == b))
    > >>
    > >> and I don't really want to guess.

    > >
    > >
    > > Why is this a syntax error?

    >
    > Because the only things allowed between the keyword "if" and the
    > condition associated with the "if" are a mandatory '(' and optional
    > whitespace, including comments. "!" is not allowed.
    >
    > > Is the following a syntax error?
    > > int c = (a=b);
    > > if ( !(c) ) {...}

    >
    > No, but
    >
    > if !(c) { ... }
    >
    > is a syntax error, of exactly the same type as
    >
    > if !(a=b) { ... }


    Ah - that's what happens when one doesn't look closely at the original, and the original isn't actually the *code* being used.
    I assumed that the OP's "if" was a part of his question, not part of his code. That is, the OP was asking:
    " if this code means ..."
    where "this code" was " !(a=b) "
     
    Fred K, Jun 12, 2012
    #18
  19. On Tue, 12 Jun 2012 13:52:46 -0400, Eric Sosman wrote:

    > On 6/12/2012 11:01 AM, Fred K wrote:
    >> On Monday, June 11, 2012 2:53:08 PM UTC-7, Ben Bacarisse wrote:
    >>> jean francois <> writes:
    >>> [...]
    >>>> Second stupid question:
    >>>>
    >>>> 'if !(a = b)' means
    >>>>
    >>>> 1) if not a equal b (Hmmm I really don't believe this: equal is '=='
    >>>> not '=')
    >>>> 2) a=b and if b=0 then....
    >>>> 3) something else?
    >>>
    >>> (3). It's a syntax error. You might have meant:
    >>>
    >>> if (!a == b)
    >>> if (a != b)
    >>> if (!(a == b))
    >>>
    >>> and I don't really want to guess.

    >>
    >>
    >> Why is this a syntax error? Is the following a syntax error?
    >> int c = (a=b);
    >> if ( !(c) ) {...}

    >
    > Expanding on the explanations by James Kuyper and John Gordon:
    > The parentheses are part of the syntax of the `if' statement itself, not
    > just incidental parts of the expression being tested. Even if the
    > expression is just a variable used as a Boolean, you must write
    >
    > if ( varname )
    > and not
    > if varname
    >
    > Similarly, parentheses are part of the required syntax of `while' and
    > `do...while' and `for' and `switch': All of these involve an expression
    > (or more than one), *plus* parentheses.
    >
    > This may at first seem weird, but in fact it's essential: It
    > removes an ambiguity.


    I see . . . so in effect, if() is really a special kind of fonction ?

    Thanks for all of the helpful responses.
     
    jean francois, Jun 12, 2012
    #19
  20. jean francois

    James Kuyper Guest

    On 06/12/2012 03:07 PM, jean francois wrote:
    > On Tue, 12 Jun 2012 13:52:46 -0400, Eric Sosman wrote:

    ....
    >> Expanding on the explanations by James Kuyper and John Gordon:
    >> The parentheses are part of the syntax of the `if' statement itself, not
    >> just incidental parts of the expression being tested. Even if the
    >> expression is just a variable used as a Boolean, you must write
    >>
    >> if ( varname )
    >> and not
    >> if varname
    >>
    >> Similarly, parentheses are part of the required syntax of `while' and
    >> `do...while' and `for' and `switch': All of these involve an expression
    >> (or more than one), *plus* parentheses.
    >>
    >> This may at first seem weird, but in fact it's essential: It
    >> removes an ambiguity.

    >
    > I see . . . so in effect, if() is really a special kind of fonction ?


    No. The only thing that if(), while(), and for() have in common with
    function calls is their use of parentheses; what they do with those
    parentheses is pretty much completely unrelated to what function calls
    do with them.
     
    James Kuyper, Jun 12, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam White
    Replies:
    10
    Views:
    1,917
    Joerg Jooss
    Nov 3, 2004
  2. Marcin Misiewicz
    Replies:
    1
    Views:
    1,953
    zoopy
    Aug 19, 2004
  3. Brandon McCombs
    Replies:
    4
    Views:
    525
    Richard Wheeldon
    Aug 28, 2006
  4. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,039
  5. Jean-Claude ROVIER

    Three stupid C questions

    Jean-Claude ROVIER, Aug 31, 2010, in forum: C Programming
    Replies:
    16
    Views:
    623
    Nick Keighley
    Sep 3, 2010
Loading...

Share This Page