I start to learn C (I used to have a little C # experience), I can't figure out how to assign a result to a variable to a variable. For example:

int main() { int x1=signfx(); printf("%d",x1); } int signfx() { int x=20; return x; } 

How can x1 assign the result of a call to the signfx() function?

  • 2
    And what is the problem? - post_zeew
  • How do you compile this code and what did not suit you in it? - αλεχολυτ

2 answers 2

First, in C there are no methods, there are functions. Second, you must declare a function prototype before using it:

 int signfx(); int main() { int x1=signfx(); printf("%d",x1); } int signfx() { int x=20; return x; } 

Or put the function itself before its first call:

 int signfx() { int x=20; return x; } int main() { int x1=signfx(); printf("%d",x1); } 

Well, a couple of little things:

 #include <stdio.h> static int signfx( void ) { int x = 20; return x; } int main( void ) { int x1 = signfx(); printf( "%d", x1 ); return 0; } 

    First of all, according to the C standard, the main function without parameters must be declared as

     int main( void ) 

    Since the program uses the standard ptintf function, it is required to include the header <stdio.h> , where this function is declared.

     #include <stdio.h> 

    In this declaration, the postfix expression signfx() is used as the initializer:

     int x1=signfx(); 

    However, the name signfx is undefined. Older compilers or newer compilers that support compatibility with older compilers can compile this code, because before the current standard was adopted, the compiler assumed by default that this is a function name that has the return type int

    However, now it does not conform to the C standard, and therefore compilers may mark this sentence as erroneous. You must first declare this name as a function name before using it in a program. for example

     int signfx( void ); ^^^^^ 

    Notice that the Void keyword is used as a parameter. This means that the function has no parameters.

    These two announcements, unlike the C ++ programming language

     int signfx( void ); int signfx(); 

    semantically different. The first declaration says that the function has no parameters, and the second declaration says that nothing is known about the parameters of the function, and the number and types of parameters will be derived from calling this function in the program.

    With that said, your program will look like this.

     #include <stdio.h> int signfx( void ); int main(void) { int x1 = signfx(); printf( "x1 = %d\n", x1 ); return 0; } int signfx( void ) { int x = 20; return x; } 

    Its output to the console

     x1 = 20 

    You could declare and define a function immediately before using it by placing its definition before the main function. The definition of a function is also its declaration. for example

     #include <stdio.h> int signfx( void ) { int x = 20; return x; } int main(void) { int x1 = signfx(); printf( "x1 = %d\n", x1 ); return 0; } 

    Keep in mind that the concept method refers to classes, that is, classes and class objects have methods. Since there are no classes in C, there is no such term as a method, but the term function is used.

    • Actually in C ++ there are no methods either, despite the presence of classes. - αλεχολυτ
    • The @alexolut Method is a general term used in OOP theory, regardless of programming language. - Vlad from Moscow