1

Why doesn't ANSI C use strrev instead of creating such a big reverse function?

This code is showing me an error. Please correct it. What is the error. I am using Code::Blocks

Error message that I get:

c:\programfiles(x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1......\libmingw32.a(main.o):main.c:(.text.startup+0xa7)||undefined reference to `WinMain@16'|

#include <stdio.h>
#include <string.h>
#include <conio.h>

 void reverse(int n, char s[])
 {
     int c,i, j;

     for(i=0, j= strlen(s)-1; i<j; i++, j--){
         c=s[i];
         s[i]=s[j];
         s[j]=c;
     }
  }

 void itoa(int n, char s[])
 {
     int i=0 ,sign;

     if((sign=n) < 0 )
         n= -n;
     do{
         s[i++] = n%10 + '0';
     }while(n /=10 >0);

     if(sign <0)
         s[i++] = '-';

     s[i] = '\0';
     reverse(n, s);
 }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 2
    Because nobody needs to reverse a string, except for homework, and implementing itoa. – Mooing Duck Jun 18 '14 at 21:10
  • `n= -n;` is undefined behavior for 2's complement architectures when `n` is `INT_MIN`. Proper `itoa` implementations do not do this. – Pascal Cuoq Jun 18 '14 at 21:17
  • @MooingDuck ok so that means i have to be using strrev in ordinary conditions? –  Jun 18 '14 at 21:23
  • @PascalCuoq i have copy pasted this code from ANSI C book this n=-n is correct. The problem was i didn't added main function. –  Jun 18 '14 at 21:24
  • 1
    @AbhimanyuAryan: No, he's correct, `n = -n` will fail if `n` is `INT_MIN`. The code has a bug. – Mooing Duck Jun 18 '14 at 21:25
  • @MooingDuck that means the book has an error sorry for doubting Pascal :) –  Jun 18 '14 at 21:28
  • @AbhimanyuAryan: It's actually quite common. Doing it without a bug is hard and complicated, so the books just show the simple version for teaching purposes, even though real code should be far more complex. – Mooing Duck Jun 18 '14 at 21:31
  • @MooingDuck so how should i learn. I learnt from youtube videos & now i am reading ANSI C book....i want to be good programmer :) who writes clean & readable codes excluding bugs –  Jun 18 '14 at 21:34
  • @AbhimanyuAryan: We recommend books from this list for C++: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list But even if your only book has some bugs, read it. It will teach you the language at least. After that, answer questions on StackOverflow. Commenters will notify you of all bugs you didn't know about. – Mooing Duck Jun 18 '14 at 21:40
  • @MooingDuck but i am studying C right now from this book: http://www.amazon.in/The-Programming-Language-Ansi-Version/dp/8120305965?tag=googinhydr2112-21 –  Jun 18 '14 at 21:44
  • @MooingDuck i have one more question related to this code why is using s[i] = '\0' at the end shouldn't he use s[i++]= '\0' cause the last value of i has been incremented in while loop if we exclude the if condition after the while loop –  Jun 18 '14 at 21:56
  • @AbhimanyuAryan: That was actually the _best_ book.... 24 years ago. It's a wee bit out-of-date nowdays. As for `s[i]` vs `s[i++]`, it depends on what you do with `i` after that. Since the code doesn't do anything, it doesn't matter what `i` is after the call. `i++` uses the current value of `i`, and increments `i` _afterwards_. – Mooing Duck Jun 18 '14 at 21:58

1 Answers1

1

Your code has no entry point. You need to have a main function, or WinMain or something.

http://mingw-starter.blogspot.com/2008/02/mingw-sdl.html says

Also, don't forget to add the -mwindows flag, if your IDE doesn't add it automatically (in addition to whatever other libraries you want to link). If you don't put them in the right order, you'll get a linker error complaining about the missing symbol WinMain@16.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • ohh sorry that was so stupid of me didn't pay attention though....really sorry –  Jun 18 '14 at 21:11
  • it worked i just added main(){ return 0;} ....without even using any of these functions –  Jun 18 '14 at 21:14
  • 1
    @AbhimanyuAryan: Presumably it "worked" in the sense that it compiled and linked. With `main() { return 0; }` (NOTE: `main()` should be `int main(void)`) the program won't do anything. – Keith Thompson Jun 18 '14 at 22:38
  • got it @KeithThompson runs without any error now –  Jun 19 '14 at 00:07