This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

What to do about wrong “control reaches end of non-void function” gcc warning

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 0.69 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. $ cat t.cpp
  2. int sign(int i) {
  3.     if(i > 0) return 1;
  4.     if(i == 0) return 0;
  5.     if(i < 0) return -1;
  6. }
  7. $ g++ -c t.cpp -Wall
  8. t.cpp: In function ‘int sign(int)’:
  9. t.cpp:5: warning: control reaches end of non-void function
  10. $
  11.        
  12. int sign(int i) {
  13.     if(i > 0) return 1;
  14.     if(i == 0) return 0;    
  15.     return -1; // i<0
  16. }
  17.        
  18. int sign(int i) {
  19.     return (i>0) ? 1 : ((i<0)?-1:0);
  20. }
  21.        
  22. int sign(int i) {
  23.     return (i > 0) - (i < 0);
  24. }
  25.        
  26. int sign(int i)
  27. {
  28.     if (i > 0)
  29.         return 1;
  30.     else if (i == 0)
  31.         return 0;    
  32.     else
  33.         return -1; // i<0
  34. }
  35.        
  36. Unoptimized     Optimized (-O5)
  37.  if      166             110
  38.  ?:      150              98
  39.  >-<     122              98
clone this paste RAW Paste Data