Object-less management in C - And why can I declare variables anywhere in a function in C?

all. I actually have two questions, several related.

Question # 1: Why does gcc allow me to declare variables after action statements? I thought the C89 standard did not allow this. (GCC Version: 4.4.3) This happens even when I explicitly use --std=c89the compilation line. I know that most compilers implement things that are non-standard, i.e. C compilers allowing //comments when the standard does not indicate this. I would like to know only the standard, so if I ever need to use only the standard, I do not focus on such things.

Question # 2: How do you deal without objects in C? I program as a hobby, and I have not yet used a language that has no objects (aka OO?). I already know some C ++, and I would like to learn how to use C on my own. Presumably, one way is to create a POD structure and create functions similar StructName_constructor(), StructName_doSomething()etc., And pass an instance of the structure of each function - is this the "right" way, or am I completely disabled?

EDIT: Due to some slight confusion, I determine that my second question is clearer: I don't ask. How do I use Objects in C?I ask How do you manage without objects in C?, how do you do things without objects, where do you usually use objects?

Thanks in advance. I have never used a language without OOP! :)

EDIT: as requested, here is an example of a problem with a variable declaration:

/* includes, or whatever */
int main(int argc, char *argv[]) {
    int  myInt = 5;
    printf("myInt is %d\n", myInt);
    int test = 4; /* This does not result in a compile error */
    printf("Test is %d\n", test);
    return 0;
}
+3
4
  • c89 , c99 . , , ( gcc), , c99.
  • , , , , ++ Objective C. , C, .

, , this - .

() , . , , , .

, / , , , - , ++ .

: : , , C, . , ++ ; C, .

, - - () , , .. , , ..

C-, ( ) left right. (), node next ( , - prev). , , - .

+3

@Question # 1: , , , . , :. :

{
    int some_local_var;
}

@Question # 2: C . . , GTK , . , , , , , . :

someStruct* someStruct_alloc() { return (someStruct*)malloc(sizeof(someStruct)); }
void someStruct_init(someStruct* this, int arg1, arg2) {...}

- , ( , ). API ++:

someStruct* str = someStruct_alloc();
someStruct_init(str);
str->someFunc(10, 20, 30);
+1

C, SO? , - C?.

, , OO- Linux.

+1

, C, OO ++, - , OO - , Modula-2 ( ) BASIC ( - BASIC, QBASIC, DOS 5.0, Quick BASIC).

The methods that you use to execute Modula-2 or Pascal functions (prohibiting strong typing, which protects against certain types of errors, but makes it more difficult to perform certain tasks), exactly those used in non-OO C, and, while working in a language with different syntaxes, perhaps (perhaps IMO), will make it easier to learn concepts without your "programming reflexes" and attempts to do OO operations in an almost familiar language.

0
source

All Articles