...

View Full Version : Can you compile with this code?



BreadBoy
09-02-2008, 12:15 AM
I mean using Dev C++ Bloodshed?



#include<iostream.h>
int print_candy_gumballs_extra_from_cupons(int c)

{

if(c<0)

{

cout<<"Can' b -ve";

return -1;

}

cout<<c<<" coupons --) ";

cout<<c/10<<" candy bar,";

c=c&#37;10;

cout<<c/3<<" gumball,";

c=c%3;

cout<<c<<" extra coupons\n";



return 0;

}

main(){

int cupons;

print_candy_gumballs_extra_from_cupons(5);

print_candy_gumballs_extra_from_cupons(52);

print_candy_gumballs_extra_from_cupons(126);

return 0;

}

/*

Output will be

5 coupons --) 0 candy bar,1 gumball,2 extra coupons

52 coupons --) 5 candy bar,0 gumball,2 extra coupons

126 coupons --) 12 candy bar,2 gumball,0 extra coupons

*/

ralph l mayo
09-02-2008, 12:46 AM
Couldn't you just try it yourself?

I don't have this Bloodshed thing, but it makes it through gcc compilation with a few warnings:



In file included from /usr/include/c++/4.1.3/backward/iostream.h:31,
from ./foo.cpp:1:
/usr/include/c++/4.1.3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
./foo.cpp:34: warning: ISO C++ forbids declaration of ‘main’ with no type
./foo.cpp: In function ‘int main()’:
./foo.cpp:36: warning: unused variable ‘cupons’

Gox
09-02-2008, 05:20 AM
Yes, it compiles fine using Dev C++ 4.9.9.2.

I don't claim to know my c++ (or compilers) but I was a little surprised to find that you could declare main() without a return type (including void) and still return an int. I guess the compiler just assumes if no return type is declared then the return type is an int.

oracleguy
09-02-2008, 08:12 AM
Yes, it compiles fine using Dev C++ 4.9.9.2.

I don't claim to know my c++ (or compilers) but I was a little surprised to fine that you could declare main() without a return type (including void) and still return an int. I guess the compiler just assumes if no return type is declared then the return type is an int.
Yeah that is exactly what it does, if no return type is specified the default is int. However you should still always specify the return type which is why most compilers throw out a warning if you don't.

Gox
09-02-2008, 10:35 PM
Yeah that is exactly what it does, if no return type is specified the default is int. However you should still always specify the return type which is why most compilers throw out a warning if you don't.
I guess if I'd read the post above mine a little more carefully I would have noticed the warning produced for this. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum