Page 2 of 2

Posted: Mon May 19, 2003 3:57 pm
by Veritas
Notice I just edited my post above...


postcount++;

Posted: Mon May 19, 2003 4:12 pm
by just brew it!
From the technical FAQ maintained by Bjarne Stroustrup, inventor of C++:

"In C++ main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution."

The FAQ itself is here:

http://www.research.att.com/~bs/bs_faq2.html#void-main

Posted: Sat May 24, 2003 11:28 am
by liquidsquid
Craig P. wrote:
liquidsquid wrote:
void main(void)

Non-standard, diagnostic required. Microsoft violates the standard by accepting this without a diagnostic.

I don't know if the C standard allows it, but the C++ standard allows no return from main(), e.g.

int main(void)
{
// ... etc ... - no return statement
}


Of course, VC6 (haven't tried VC7.1 yet) converts this into void main(). :x


Obviously you skipped reading my later post of the reason why I have no entry or return value in main(). My main is the main program OS loop, and never exits, so by returning something, what would you return it to? By entering with something, what would you be passing into main? I suppose I could make it pass something, but what would be the point?

Oh, and my compiler handles the //, which I know is not ANSI C, but it sure is handy.

My main routine is something like this:

void main(void) {
short hardError = NO_ERROR;
short objError = NO_ERROR;

hardError = initThis();
//handle errors after each, though I didn't put the effort forth in this post.
hardError = initThat();
hardError = initTheOther();
hardError = initObjects(); //Sets static object locations, look, feel, and associated actions (lots of ram in this system)

objError = drawObjects(); //First time in, draw graphic objects
objError = handleTasks(); //Stays here forever (hopefully!)

//If we have made it here, something serious has happened
if (objError) {
msgBox("Object Error", getObjectErrorTest()); //Display any debug information if error was set.
} else {
msgBox("OS ****", "Learn to program you moron"); //Tell myself to fix the problem
}

goto __start; //Go right back to the start, re-init everything, stack, empt RAM, re-init hardware.
}


As you can see, it is not programming on the PC, though I know many of you are sticlers for standards, sometimes you just have to say "NO" and go the efficient way ;-)

BTW, I don't use the scanf() function, I haven't since college. Instead I use a robust better (IMHO) version I have maintained for the last three years which handles graphic text much like Window's text entry boxes, but inherently includes value limits, up/down increments, etc. so the user cannot enter an invalid value. Makes for really clean code, though the routine itself was a bitch. Once you start to get into programming, you will probably use sprintf() to format your output into a string rather than display it directly, and write more specific scanf() type fucntions yourself.


So WTF is the difference between a reference and a pointer? From what I unserstand, a reference is a variable or constant which refers to another object or variable, thus it contains an address. So does a pointer. You have to forgive me because I haven't delved into C++ in a LONG time, but apart from semantics and the way you write code, the generated assembly is essentially the same between the two.

A nice article:
http://www.embedded.com/story/OEG20010311S0024


-LS

Posted: Sat May 24, 2003 12:30 pm
by IntelMole
muyuubyou wrote:
Of course it's stored in binary :D . The scanf function does all the conversion (in the same way it does when you pass it a dec number via %d).

In fact, when you explained your problem I thought you wanted to program the conversion yourself, and not simply calling scanf to do it ;)

How is your progress going? I want you to start with some SDL and OpenGL real soon. 8)



Perhaps I worded my post badly, I meant the statement to be more "obvious" than "oh my GOD!" :lol:

I meant that in order to store the number, the scanf has to convert to binary, then the printf does the bin -> dec conversion...

When I started the problem I was going to do the conversion myself... however, after using this program a couple of times, I can see that would be a bitch. For example, the hex number a2d3 (my test value) comes out as 41683... I wouldn't be able (ATM anyways) to string together the four hex values into a sensible binary :-D

SDL? OpenGL? lol the possibilities :-D,
IntelMole

Posted: Sat May 24, 2003 1:06 pm
by just brew it!
So WTF is the difference between a reference and a pointer? From what I unserstand, a reference is a variable or constant which refers to another object or variable, thus it contains an address. So does a pointer. You have to forgive me because I haven't delved into C++ in a LONG time, but apart from semantics and the way you write code, the generated assembly is essentially the same between the two.

Think of a reference as an alias, rather than a pointer. Judicious use of references can make the code easier to follow. But yes, it is true that you can usually get equivalent functionality using pointers.

There are a few situations in C++ where there is no substitute for references. For example... if you are overloading one of the assignment operators, the overloaded operator needs to return a reference to the variable that was just assigned to, in order to preserve C++ semantics for assignment.

References also allow functions to return an lvalue, i.e. something that can be used on the left side of an assignment.

Posted: Sat May 24, 2003 2:01 pm
by just brew it!
For example, the hex number a2d3 (my test value) comes out as 41683... I wouldn't be able (ATM anyways) to string together the four hex values into a sensible binary

It's not that hard. Here's an example of how to do the conversion yourself:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

long Convert(char *s)
{

    long result = 0;
    for ( ; *s != '\0'; s++)  /* examine string 1 char at a time */
    {
        if (*s >= '0' && *s <= '9')
            result = (result << 4) + (*s - '0');  /* handle digits */
        else if (*s >= 'A' && *s <= 'F')
            result = (result << 4) + (*s - 'A') + 10;  /* handle A-F */
        else if (*s >= 'a' && *s <= 'f')
            result = (result << 4) + (*s - 'a') + 10;  /* handle a-f */
        else
            break;  /* no more hex digits */
    }
    return result;

}

int main(void)
{

    char buffer[12];
    long value;

    do
    {
        printf("Enter hexadecimal number...\n");
        fgets(buffer, sizeof(buffer), stdin);   /* read string */
    } while (!isxdigit(buffer[0]));             /* until we get valid hex */

    value = Convert(buffer);           /* convert from hex */

    printf("0x%08lX hex is %ld dec\n", value, value);     /* output */

    printf("Hit Enter to quit: ");
    fgets(buffer, sizeof(buffer), stdin);       /* wait for user to hit Enter */
    printf("Have a nice day\n");

    return 0; /* Tell the OS there was no problem */

}

Posted: Sun May 25, 2003 10:45 am
by Veritas
it is not programming on the PC, though I know many of you are sticlers for standards, sometimes you just have to say "NO" and go the efficient way


The ANSI C standard applies to compilers, not platforms. While your compiler might handle it now, a future version may not (and should not if it wants to be compliant with the rest of the world). Yes, it is an easy fix, but why not be compliant now? How hard is it to write 'int' instead of 'void' and add return 0; as your very last line just in case your compiler writer gets smart?

I have written embedded code on systems where returning something from main does seem stupid...but I had no choice as my compiler was ANSI C compliant. My point is why do it the wrong way now (even if it is more effiencient...those 12 keystrokes are important </sarcasm>) and then have to go back and fix it? If you get to that point, it really becomes less efficient.

Edit: Also, in the example where you used void main(void) why did you bother to put a return 0; and then put a comment on doing so to tell the OS everything was allright? Obviously the example you were giving was not OS type code, so your excuse for doing it fails. In your earlier example it was NECESSARY to have int main(void) at the least.

Edit #2: Here is a good introduction to references in C++:
http://www.parashift.com/c++-faq-lite/r ... ml#faq-8.1

Posted: Mon May 26, 2003 7:52 am
by IntelMole
just brew it! wrote:
For example, the hex number a2d3 (my test value) comes out as 41683... I wouldn't be able (ATM anyways) to string together the four hex values into a sensible binary

It's not that hard. Here's an example of how to do the conversion yourself:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

long Convert(char *s)
{

    long result = 0;
    for ( ; *s != '\0'; s++)  /* examine string 1 char at a time */
    {
        if (*s >= '0' && *s <= '9')
            result = (result << 4) + (*s - '0');  /* handle digits */
        else if (*s >= 'A' && *s <= 'F')
            result = (result << 4) + (*s - 'A') + 10;  /* handle A-F */
        else if (*s >= 'a' && *s <= 'f')
            result = (result << 4) + (*s - 'a') + 10;  /* handle a-f */
        else
            break;  /* no more hex digits */
    }
    return result;

}

int main(void)
{

    char buffer[12];
    long value;

    do
    {
        printf("Enter hexadecimal number...\n");
        fgets(buffer, sizeof(buffer), stdin);   /* read string */
    } while (!isxdigit(buffer[0]));             /* until we get valid hex */

    value = Convert(buffer);           /* convert from hex */

    printf("0x%08lX hex is %ld dec\n", value, value);     /* output */

    printf("Hit Enter to quit: ");
    fgets(buffer, sizeof(buffer), stdin);       /* wait for user to hit Enter */
    printf("Have a nice day\n");

    return 0; /* Tell the OS there was no problem */

}


Exactly :lol:

Could I do that? Not ATM...

Anyways, who changed my thread title, now I sound camper than a row of pink tents :-D,
IntelMole