Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C- As In Constipation

Name: Anonymous 2010-10-15 18:54

So i'm new to the C language and i have a few questions.

First of all some past experience with me:java, C++ and lua

Now I've started to learn C and i find it kinda weird..... 1)structs i assume are the 'classes'(minus the functions inside) of C, is that correct?
2)each new C file isn't a new class like in most OOP languages instead it's just a continuation of the main file to provide more functions in a neater manor via it's header file, is that correct?
3)Is C supposed to be harder or easier than an OOP language?

Name: Anonymous 2010-10-15 22:42

1)structs i assume are the 'classes'(minus the functions inside) of C, is that correct?

Structs in C also do not have any public/private/protected accessors. They also do not have any inheritance. They are really simple

struct Person {
int age;
char name[36];
};

void main()
{
  // unlike C++ you have to use the struct keyword, unless you do a typedef
  struct Person Bob; // create a struct on Stack Memory
  const char *name = "Bob"; // create a pointer on stack, it
points to a data segment read-only memory where Bob is defined
 int len = strlen(name); // get len of name

  strcpy( Bob.name, name ); // copy name to Bob.name
 
  // add a NULL terminator
  Bob.name[len-1] = '\0';

  Bob.age = 16;

  printf("Bob age: %i name: %s\n", Bob.age, Bob.name);

  // all stack data is cleared when main returns
}

2)each new C file isn't a new class like in most OOP languages instead it's just a continuation of the main file to provide more functions in a neater manor via it's header file, is that correct?

ya basically, the header file contains necessary defines and prototypes or externs for data structures, variables, or functions defined in a library that you linked to or another C source file that you compile.

#include is a preprocessor directive, and pretty much copies the contents of the header file into the C source file before compilation, so you can even omit the header file and just paste the stuff you want directly in the source file.

3)Is C supposed to be harder or easier than an OOP language

OOP languages are generally high level languages and do all the memory management for you, which can be really nice especially for a new programmer. Also many high level languages abstract pointers as well.

With C you have to manage memory all yourself. It is important to understand how memory is used.

void main()
{
  struct Person *Bob; // declare a pointer to a struct named bob
  // the pointer is in stack memory

  // malloc returns the addr of a memory location allocated in Heap
  Bob = malloc( sizeof( struct Person ) );
  Bob->name = 8;

  // free the memory we allocated in heap when we are done with it
  free( Bob );
}

It is important to note that C is a Systems Programming Language, therefore it is preferred for use such as kernel development and drivers. It can easily become a head ache when used for developing applications as simple operations such as string or pointer manipulation can lead to fatal errors.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List