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

Pages: 1-

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 19:00

You shouldn't be learning C.

Name: Anonymous 2010-10-15 19:05

>>2
thank you for your positive comments, anyone else?

Name: Anonymous 2010-10-15 19:07

Get rid of the bits of C++ that you like. This is C.

Name: Anonymous 2010-10-15 19:08

>>1
"manor"?  What the fuck?  Learn English first.

Name: Anonymous 2010-10-15 19:10

>>5
nothing wrong with setting up your code in a neater house-like situation

Name: Anonymous 2010-10-15 20:06

>>1

1) Nope, C structs aren't like classes. And they can have functions.

2) Nope, I don't think I've written a program where I could dump everything into the same file and it would work. Just an example, look up how the keyword static works in C.

3) Nope, C was made before OOP languages, it is far easier to understand if you began programming with an imperative language.

Look, if you're trying to learn C from its similarities with other languages, I'd stop.

Name: Anonymous 2010-10-15 20:16

>>7
Actually, work began on Smalltalk in 1969 and Simula was around before that.

Name: Anonymous 2010-10-15 20:31

>>7
Structures can't have functions.

Name: Anonymous 2010-10-15 21:05

>>9
Sure they can. It's even legit to call C structs 'objects'.

Name: Anonymous 2010-10-15 21:17

>>9
See >>GTK

Name: Anonymous 2010-10-15 21:21

>>8
Sorry, I meant to say, the OOP languages he mentioned. My bad.

Name: Anonymous 2010-10-15 21:53

>>10
No they can't. They can have pointers to functions, but not functions. Abuse of language is fine when the reader already understands, but if you tell the OP that "structures can have functions", he's going to misunderstand. And the word "object" is going to be even more confusing since it has a different meaning in C.

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.

Name: Anonymous 2010-10-15 22:59

>>13
It's not an abuse. Abuse would be calling those functions 'methods' and then perhaps writing a bunch of preprocessor macros to make them work just like real method calls. Some OO models work precisely this way except they're not implemented in a preprocessor (well... not anymore.)

The only way >>1 is going to be confused by any of it is if he doesn't know what a pointer is, but then he'll be confused by everything in C. Being confused by what C calls an "object" is also something you have to get over in order to learn the language.

Name: Anonymous 2010-10-15 23:56

If you want to really understand C, just study the standard. The latest public version (C99 + TC1 + TC2 + TC3) is available for free at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf. The entire language, excluding the library, is specified in fewer than 200 pages.

Name: Anonymous 2010-10-16 6:58

>>14
strcpy( Bob.name, name );
Segmentation fault

Name: Anonymous 2011-03-03 16:32

aa
a
fff

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