Page 2 of 3

Re:C aptitude

Posted: Fri Apr 07, 2006 12:28 pm
by Candy
YeXo wrote: From what you say I assume gcc (and any windows port of that) is using C99, is that correct?
Don't assume (as we've pointed out above).

gcc -std=c99 -> gcc in c99 mode

gcc -std=c89 -> gcc in c89 mode

gcc -std=gnu++ -> gcc in c++ with GNU quirks mode

gcc -std=gnu99 -> gcc in c99 mode with GNU hacks

Read the docs.

Re:C aptitude

Posted: Fri Apr 07, 2006 12:50 pm
by Solar
YeXo wrote: From what you say I assume gcc (and any windows port of that) is using C99, is that correct?
No. GCC defaults to C90 plus GNU extensions when compiling C, and to C++98 with GNU extensions when compiling C++. Note that GNU is not fully compliant to C99 even when told to use C99. It is not fully compliant to the C++ standard, either, but very few compilers are. (And yes, I have to mention it, virtually all of them are lacking [tt]export[/tt]. 8) )

Re:C aptitude

Posted: Fri Apr 07, 2006 8:40 pm
by Schol-R-LEA
Solar wrote: ...but in C89 there is no such thing as a line comment, so it would be (42 + 12 / 3 + 4) = 50...
kicks self I entirely missed the "C89/C99?" part.

Re:C aptitude

Posted: Fri Apr 21, 2006 6:06 am
by Pype.Clicker
YeXo wrote: Adding numbers to pointers always confuses me.
Don't think of them as "adding numbers to pointers", consider them as "indexing an array".

[tt]*(ptr+4)[/tt] is semantically identical to [tt]ptr[4][/tt], whatever type [tt]ptr[/tt] might have. That means when computing "ptr + 4" your compiler actually produce an that is 4*sizeof(*ptr) bytes farther than the address of ptr.

Thus on a machine where "long int" is 4 bytes, "long_int_ptr++" do advance the pointer to the next long int ...

and, btw, i don't know what code has output the line
ptr points to the value: 0x14
but ptr doesn't point to "value 0x14". it points towards "address 0x14" (which is very unlikely to be a valid address anyway :P )

Re:C aptitude

Posted: Fri Apr 28, 2006 12:29 pm
by viral
hello...
Try this

Code: Select all

     int i = 5;
              printf("%d %d %d", i++, i++, i++);
it will print:
7 6 5


Acctually arguements are passed on stack.. hence value of first i++ (5) is pushed 1st then (6) then (7) and finnally they are poped and print in as 7 6 5.

Re:C aptitude

Posted: Sat Apr 29, 2006 12:42 pm
by LongHorn
Somebody is coming to our college to teach us C in deep. I don't know how deep is it going to be? Just like to impress them by saying c99 kind of stuff. Is there any book explaining c89,c99 compliance etc.

Re:C aptitude

Posted: Sun Apr 30, 2006 7:02 am
by YeXo
Pype.Clicker wrote: [tt]*(ptr+4)[/tt] is semantically identical to [tt]ptr[4][/tt], whatever type [tt]ptr[/tt] might have. That means when computing "ptr + 4" your compiler actually produce an that is 4*sizeof(*ptr) bytes farther than the address of ptr.

Thus on a machine where "long int" is 4 bytes, "long_int_ptr++" do advance the pointer to the next long int ...
When computer "ptr + 4" the compiler adds 4*sizeof(ptr) rather than 4*sizeof(*ptr). On a pentium, the first would mean 4*4 (the size of a pointer is 32 bits, or 4 bytes), while the later whould be 4*whatever the size of the type is.
and, btw, i don't know what code has output the line
ptr points to the value: 0x14
but ptr doesn't point to "value 0x14". it points towards "address 0x14" (which is very unlikely to be a valid address anyway :P )
It wasn't meant to be output of any code, merely as explanation of the contents of the variables. You're right ptr points to address 0x14, instead of value 0x14, that was my mistake. (ofcourse 0x14 is unlikey to be a valid address, but it is most times more simple to explain something with small than with large numbers).

Re:C aptitude

Posted: Sun Apr 30, 2006 8:29 am
by Candy
viral wrote: hello...
Try this

Code: Select all

     int i = 5;
              printf("%d %d %d", i++, i++, i++);
it will print:
7 6 5


Acctually arguements are passed on stack.. hence value of first i++ (5) is pushed 1st then (6) then (7) and finnally they are poped and print in as 7 6 5.
According to the spec, the behaviour for more than one i in a line, one of more containing ++ or -- is undefined. Undefined means as much as the compiler might eat your cat and sell the fur on ebay.

It is however, something people will wonder. Just like, what would

Code: Select all

i++++;
do. Well, it'll preprocess as:

Code: Select all

(i++)++;
which won't compile because you can't increment (++) an expression (i++). Just use ++ and -- for increment and decrement on single lines or at the end of for statements and at no other places. That keeps your code clean and understandable, without stuff that's compiler dependant or undefined.

Re:C aptitude

Posted: Sat May 06, 2006 7:04 am
by B.E
Pype.Clicker wrote: if _that_ confuses you, then what would it be with

Code: Select all

   int x = 5;
   if (x=6) printf("six");
   else printf("not six");
This first inits x to 5 in the first line. then in the if statement x is assigned 6(only one = sign) , and because true in c/c++ is non zero than it will always be true and always print "six".
or even

Code: Select all

#include <stdio.h>

#define SIX 1 + 5
#define NINE 8 + 1

int main(void)
{
    printf( "What do you get if you multiply %d by %d? %d\n", SIX, NINE, SIX * NINE );
    return 0;
}
which does print ... 42 :P (that one is on wikipedia :P)
The c preprocessor does not evalate the expression in a define, it just adds the string where ever it is(i.e. SIX * NINE = 1 + 5 * 8 + 1)

Re:C aptitude

Posted: Sat May 06, 2006 3:47 pm
by zloba
2 YeXo:
When computer "ptr + 4" the compiler adds 4*sizeof(ptr) rather than 4*sizeof(*ptr). On a pentium, the first would mean 4*4 (the size of a pointer is 32 bits, or 4 bytes), while the later whould be 4*whatever the size of the type is.
ahem.. what??? :o how did you get that idea?

as absurd as it was, i decided to check, maybe all this time i've been doing it wrong.

nope.

Code: Select all

$ ./foo 
sizeof(Foo)=32
&foo[4]=bfc3eaf4
foo+4  =bfc3eaf4
&ptr[4]=bfc3eaf4
ptr+4  =bfc3eaf4
 wrong =bfc3ea84

Re:C aptitude

Posted: Sun May 07, 2006 2:34 pm
by Kemp
Just in case the conflicting viewpoints have got confusing by now, here's what happens.

Code: Select all

int loadOfInts[20];
// initialised here
int *ptr = &(loadOfInts[0]);  // or simply int *ptr = loadOfInts;
ptr++;  // ptr points to the second element
ptr = ptr + 4;  // ptr points to 6th element
ptr is always incremented by multiples of the size of whatever it points to.

(Ignoring any typoes or blatant silliness this should be correct)

Re:C aptitude

Posted: Tue May 09, 2006 1:05 am
by Neo
LongHorn wrote: Somebody is coming to our college to teach us C in deep. I don't know how deep is it going to be? Just like to impress them by saying c99 kind of stuff. Is there any book explaining c89,c99 compliance etc.
Are you sure you wanna learn C ??

Re:C aptitude

Posted: Tue May 09, 2006 1:11 am
by Solar
I think C is one of the few languages every programmer should know. And it's not that much to learn either - both the language proper and the library are quite manageable.

C89 -> C99 changed rather little in the language itself. Line comments ("//") were added, runtime-sized arrays, and a couple of keywords that nobody really needs or uses (restrict, for example). Most changes were in the library, and for that, any good reference book should do. (www.dinkumware.com offers a downloadable HTML C/C++ reference that is quite good and affordable.)

Re:C aptitude

Posted: Tue May 09, 2006 5:47 am
by mystran
To be honest, I'd say C99 was basicly: standardize all the extensions to C89 that most everyone uses anyway, and clarify some things that cause a lot of confusion.

What I mean is:

for(int i = 0; i < foo; i++) { ... }

Most other languages agreed on the scope rule for i, and it's more intuitive than C89's, so why not use it instead.

// this is a comment

Everybody used that anyway, every compiler supported it anyway (well, almost) so what's the point. Let's support it.

#include <stdint.h>

More or less every project defines types for specific word-lengths anyway, so why not make a single place to put them with standard names to avoid the mess.

and so on.. basicly C99 is a long waited cleanup..

Re:C aptitude

Posted: Tue May 09, 2006 6:18 am
by YeXo
zloba wrote: 2 YeXo:
When computer "ptr + 4" the compiler adds 4*sizeof(ptr) rather than 4*sizeof(*ptr). On a pentium, the first would mean 4*4 (the size of a pointer is 32 bits, or 4 bytes), while the later whould be 4*whatever the size of the type is.
ahem.. what??? :o how did you get that idea?
What I meant is this:
Some pointer ptr of type unsigned short (2 bytes) points to addres 0x100000
Then after this line:
ptr = ptr + 4;
Ptr point to addres 0x100008, because pointer now points to ptr+4*sizeof(unsigned short), not to ptr+4*sizeof(unsigned short*)
sizeof(unsigned short)=2 while sizeof(unsigned short*)=4