putting return type in front of swap function call makes it not work(c programming)

Viewed 50

Hello so I am learning pointers in c programming right now and I made a basic call by reference swap function that doesn't work as seen below.

#include <stdio.h>

void swap (int *x, int *y) {
  int temp = *x;
  *x = *y;
  *y = temp;
}

int main(void) {
  int a = 10, b = 20;
  int *ptr1 = &a, *ptr2 = &b;
  printf("before swap: \t %d \t %d\n", a, b);

  void swap(&a, &b);

  printf("after swap: \t %d \t %d", a, b);
}

int main();

However when I got rid of the void in front of the swap function call (below) it then started to work. So I want to know why this is and why this isn't the case for the main function call (at the bottom) as I have the return type int in front of that.

#include <stdio.h>

void swap (int *x, int *y) {
  int temp = *x;
  *x = *y;
  *y = temp;
}

int main(void) {
  int a = 10, b = 20;
  int *ptr1 = &a, *ptr2 = &b;
  printf("before swap: \t %d \t %d\n", a, b);

  swap(&a, &b);

  printf("after swap: \t %d \t %d", a, b);
}

int main();
1 Answers

The compiler shall issue a message that this record

void swap(&a, &b);

is incorrect. So the first program shall not be compiled.

For example the compiler can consider this record

void swap(&a, &b);

as a function declaration with an identifier list where identifiers are specified incorrectly.

Also the second declaration of main

int main();

does not make sense, Remove it.

Related