Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Three stupid C questions: ++,...

Reply
Thread Tools

Three stupid C questions: ++,...

 
 
Noob
Guest
Posts: n/a
 
      06-12-2012
Jean Francois wrote:

> Before I get enough money to buy a good C/C++ compiler (for DOS/Win/NT)


Try GCC under cygwin, it's free.

https://en.wikipedia.org/wiki/Cygwin
https://en.wikipedia.org/wiki/GNU_Compiler_Collection

or perhaps Eclipse, if you're looking for an IDE
(NB: Eclipse is written mostly in Java, a poisonous language)

https://en.wikipedia.org/wiki/Eclipse_(software)
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      06-12-2012
On 06/12/2012 08:16 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      06-12-2012
On 2012-06-12, Noob <root@127.0.0.1> wrote:
> Jean Francois wrote:
>> Before I get enough money to buy a good C/C++ compiler (for
>> DOS/Win/NT)

>
> Try GCC under cygwin, it's free.


> https://en.wikipedia.org/wiki/Cygwin
> https://en.wikipedia.org/wiki/GNU_Compiler_Collection


MinGW with is a nice package when you want to compile programs
for Win32 with gcc.

http://www.mingw.org/

--
Neil Cerutti
 
Reply With Quote
 
Fred K
Guest
Posts: n/a
 
      06-12-2012
On Monday, June 11, 2012 2:53:08 PM UTC-7, Ben Bacarisse wrote:
> jean francois <(E-Mail Removed)> 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) ) {...}

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-12-2012
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 <(E-Mail Removed)> 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) { ... }
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      06-12-2012
In <(E-Mail Removed)> Fred K <(E-Mail Removed)> 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
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-12-2012
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 <(E-Mail Removed)> 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
(E-Mail Removed)d


 
Reply With Quote
 
Fred K
Guest
Posts: n/a
 
      06-12-2012
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 <(E-Mail Removed)> 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) "
 
Reply With Quote
 
jean francois
Guest
Posts: n/a
 
      06-12-2012
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 <(E-Mail Removed)> 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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-12-2012
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Three stupid C questions Jean-Claude ROVIER C Programming 16 09-03-2010 06:59 AM
Re: Three Mobile --> Skype on three (Non-three [Symbian - Nokia] handsets) Harry Stottle UK VOIP 0 01-05-2010 08:59 AM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
Stupid is as Stupid Does! Michael P Gabriel Digital Photography 3 06-26-2004 12:49 PM



Advertisments