Message from C, C++ discussions

November 2019

— This is a well known case of undefined behavior.

That is, this code is wrong, incorrect. But the compiler is not obliged to check this, and must compile the code well and produce executable, which will work in this case unpredictably.

The bad pice of code is

x = x++ + y++;

You cannot modify one variable, x in this case, more then once in one statement before one sequence point happens, ; in this case.

Two modifications of x , caused by assignment and by increment, may happen in any arbitrary order, and final value of x is undefined.

This happens independently on the way you enter values of x and y in the previous statement.

Message permanent page

— 

I am curious why when i want to call a function ,the function need to be upper of the function of calling ?

Like :

void test(){

}

void test1(){
test();
}

test need to be above test 1

— User Michal Vagen has 1/2 warnings; be careful!
The latest warn was because:
advertising

— Just needs to be declared first

— Not defined

— I'm not positive but I think it's for the preprocessor/lexer or whatever because the compiler is gonna need to know that a function named test exists so I'm guessing it can be linked together properly

Message permanent page

— Because now i use c to program mcu nrf52840 using segger IDE. when i put it under the other function it cannot detect the function. usually compiler will give suggestion function as we type. so strangeee

Message permanent page

— "The required behaviour of C89 when encountering a function call for which no definition or prototype is available is to assume that it returns an int and has the number and types of the arguments that you supply in the call. If you subsequent call it with different argument types that cannot be legally implicitly cast, or a different number of arguments, the compiler will reject the code.

The compiler or linker will complain when it eventually finds a mis-matching definition. The use of a prototype allows the compiler to generate the error at the call rather than the definition, of when trying to link the definition from another module. So in short don't do it if you want the compiler to help you write working code!

In C99 and C++ prototypes are required if the definition is not already visible.

"

Message permanent page

— Thanks. i dont understand.😆😆

— It does not need this.

To call a function, it has to be declared first, before calling, but does not need defining.
That is, you can only declare the function, but define it later in the source file or even in another source file. This is what we have in C++.

In classic C you even don't need to declare a function, the call just will not be checked on correctness of the parameters.

You needed to put your function upper in the file only because you don't you separate pre declaration of the function.
But definition of function is always also a declaration, hence the compiler can use this function lower in the file

Message permanent page

— This is better. thanks

— But i dont understand the pre declaration of the function. anyway will google it.