Welcome, Guest. Please login or register.

Author Topic: Beginners 'C' programing group  (Read 13081 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Beginners 'C' programing group
« on: February 12, 2004, 07:23:20 PM »
Quote
Why not C++? C is inferior.

Because C++ is much more complex than C.
Because C++ is based on C (learning C++ is easier after C).
Because most of the software is written in C still.

Although I am myself too busy atm to help with this project I welcome the selection is C. Good choice.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Beginners 'C' programing group
« Reply #1 on: February 12, 2004, 10:59:49 PM »
Right. Here's the correct one:

#include
#include

int main(void)
{
printf("hello world\n");
return EXIT_SUCCESS;
}
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Beginners 'C' programing group
« Reply #2 on: February 13, 2004, 01:47:27 AM »
@crystall
Quote

I think strict ANSI-C code would look like this

#include
#include

int main( void )
{
printf("Hello world!\n");
exit(0);
}

Possibly. However, EXIT_SUCCESS can be used instead of 0, and in general, if there is a define to use, use it instead of fixed value (that way if things would ever change, your code will adapt with just recompile and you don't need to worry about changing all the fixed constants in the code). This is more like generic programming style, not ANSI standard.

I think ansi does state that 0 is "success" for exit(), however. So you're correct here.

Anyway, use of exit() instead of proper failure code path is a bit dull, and forces you to write cleanup stub routines with atexit(), and thus forces you to use global variables. I would advice coders to stay away from exit() if possible, esp if coding for AmigaOS, MorphOS and other compatibles.

The problem is that there is no automatic cleanup for OS resources, just for resources set up by the startup code (stuff provided by the ANSI standard, for example: malloc()d memory, filehandles opened by fopen() etc). So  if you would open a intuition window and call exit() to terminate, the window would not get closed (unless if you do it with atexit cleanup stub routine).

Now I got carried away, sorry. :-)
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Beginners 'C' programing group
« Reply #3 on: February 15, 2004, 03:26:37 AM »
Quote

char* clone(char* string)
{
  char* result;
  result = (char*)malloc(strlen(string));
  strcpy(result, string);
  return result;
}

Now, the code above trashes one byte past the allocated memory. That is because strlen() return the string length without the terminating zero. strcpy() will write this zero-char and trash one byte past allocated memory. This is a common beginner mistake. Now, this won't be the last time we meet an error or bug, and we should not get depressed if we make them. Mistakes are good, since often you learn much more from fixing them than always writing perfect code.

To fix this bug, we must add one to strlen() result to make room for the terminating zero-char:

char *clone(const char *string)
{
  char *result;

  result = malloc(strlen(string) + 1);
  if (result)
  {
    strcpy(result, string);
  }

  return result;
}

NOTE: 'const char *string' indicates that the memory pointed by the string pointer is read only, and not modified by the function. This is not really needed, but helps to locate programming mistakes (here: accidently modifying read only parameter) in the function.