Why doesn't this simple program print a thing?

I wrote a simple program that should add 1 and 53 and return 54. But at the same time, it does nothing. No mistake, just nothing.

The file is saved as firstEx.c, I compiled and launched as

$ gcc -o firstEx firstEx.c
$ ./firstEx

Everything is going well, but I see no way out. Can anyone help? (I am completely new to C.)

Here is the code:

#include <stdio.h>

int main()
{ 
  int var1, var2;
  var1 = 1; 
  var2 = 53;
  void add_two_numbers (int var1, int var2);
  return 0;
}

void add_two_numbers (int a, int b) 
{ 
  int c;
  c = a + b; 
  printf ("%d\n", c); 
}
+4
source share
1 answer

Line

void add_two_numbers (int var1, int var2);

It is a function prototype, not a call to the specified function. It’s unusual to see this in the middle of a function, but this is certainly true as it’s just a declaration style in accordance with the standard ( ISO C11 6.7).

To actually call a function, you should use something like:

add_two_numbers (var1, var2);

, , , , . - :

#include <stdio.h>

void add_two_numbers (int a, int b) {
    int c;
    c = a + b;
    printf ("%d\n", c);
}

int main (void) {
    int var1, var2;
    var1 = 1;
    var2 = 53;
    add_two_numbers (var1, var2);
    return 0;
}

, :

#include <stdio.h>

void add_two_numbers (int, int);

int main (void) {
    int var1, var2;
    var1 = 1;
    var2 = 53;
    add_two_numbers (var1, var2);
    return 0;
}

void add_two_numbers (int a, int b) {
    int c;
    c = a + b;
    printf ("%d\n", c);
}

, :

  • . add_two_numbers, , . , print_sum_of_two_numbers.
  • , :-) add_two_numbers printf ("%d", a + b);.
  • , main add_two_numbers (1, 53);.
  • C99, main, "", . , , , , , , .

:

#include <stdio.h>

void print_sum_of_two_numbers (int a, int b) {
    printf ("%d\n", a + b);
}

int main (void) {
    print_sum_of_two_numbers (1, 53);
}
+11

Source: https://habr.com/ru/post/1609428/


All Articles