1

The following code displays a linking error when compiled using Turbo C IDE

I have used the swap function in this block of code to learn from a C tutorial.

It displays a single error saying undefined symbol '_swap' in module PROG1.C ( filename in my computer )

#include <stdio.h>

/* function declaration */ 
void swap(int x, int y); 

int main () { 

  /* local variable definition */ 
  int a = 100; 
  int b = 200; 

  printf("Before swap, value of a : %d\n", a ); 
  printf("Before swap, value of b : %d\n", b ); 

  /* calling a function to swap the values */ 
  swap(a, b); 

  printf("After swap, value of a : %d\n", a ); 
  printf("After swap, value of b : %d\n", b ); 

  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
sagar_02
  • 69
  • 6
  • 2
    You declare `swap`, but you don't have it defined anywhere that you've shown. – Thomas Jager Jun 03 '20 at 17:12
  • 1
    C does not have a builtin swap function https://stackoverflow.com/questions/8862136/is-there-a-built-in-swap-function-in-c. C++ does have two templated ones (and several overloaded ones for `std::` containers). Did you mean to make a C++ project? – JohnFilleau Jun 03 '20 at 17:13
  • C isn't quite the moving target that C++ is, but Turbo C is still decades old. You're missing out on C99 and C11 and the changes in those standard revisions are not insignificant. If you are required to use Turbo C for a programming course, learn what you can from it, but also supplement your learning with some books that cover modern C.and a modern C compiler.; Your entry into the workforce will be smoother. – user4581301 Jun 03 '20 at 17:17
  • @chux-ReinstateMonica I'm new to programming in c. Could you show me code to define the swap() function, it would be really helpful.. – sagar_02 Jun 03 '20 at 17:18
  • @sagar_02 ["show me code to define the swap() function"](https://stackoverflow.com/a/8862167/2410359) – chux - Reinstate Monica Jun 03 '20 at 17:20
  • @user4581301 Yes, I'm aware that there are much better and modern compilers, I'm fairly inexperienced at programming so I'll use the new compilers when I get better at coding for sure. Thanks for your suggestion! – sagar_02 Jun 03 '20 at 17:22
  • No worries. Another recommendation: look in the TurboC folder for TD.EXE. That's Turbo Debugger, one of the best debuggers ever made commercially available. It's still better than a lot of today's offerings. Learn to use it early and it will save you fantastic amounts of time troubleshooting your code. My RPG campaigns back when I was in school likely owe their existence to Turbo Debugger. Without it I never would have had the time for anything except debugging. – user4581301 Jun 03 '20 at 17:36

0 Answers0