First C class, yay. Alternatives to DevC++?

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
My professor is recommending I use DevC++ for the class, but I can't get it to install on my Windows x64 rig at home. He also recommends to not use a C++ compiler and only a C compiler.

Any suggestions?

EDIT: DevC++ 5 beta seems to work on x64, nevermind.
 

pedant

2[H]4U
Joined
Feb 1, 2001
Messages
3,266
Alternatively you could have downloaded mingw http://www.mingw.org/docs.shtml & used that. I guess you might already have that installed though?
You can then use notepad, metapad, textpad or whatever your choice of text editor for compiling the files. As an introductory C thing, I think that is actually better then using an ide.

Finally, although probably unlikely, I'd suggest using linux then you could use kate or kwrite as the editor - still a text editor but additional nice aspects such as bracket matching and highlighting.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
Why wouldn't you get the Visual Studo Express Edition?

Why does your professor recommend a C compiler over a C++ compiler? That seems odd. The Visual C++ command-line compiler can treat a file as C or C++ with a command-line switch.
 

HHunt

Supreme [H]ardness
Joined
Apr 12, 2001
Messages
6,063
I'd guess that it is to make sure they don't accidentally compile as C++.
 

HaMMerHeD

[H]F Junkie
Joined
Jul 13, 2004
Messages
10,398
It never fails to amaze me that people still use straight C when C++ is available. String handling in ANSI C is a nightmare, and should be punishable by stoning.
 

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
HaMMerHeD said:
It never fails to amaze me that people still use straight C when C++ is available. String handling in ANSI C is a nightmare, and should be punishable by stoning.
lol.

Maybe he wants to show his students what programming was like in the day, so that they feel spoiled rotten when they get to touch a program that uses the STL or similar stuff. I know I felt like being in heaven when I first learned that there was this Vector<> template class :)
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
Professor says "Beware to make sure you use a C compiler and NOT a C++ compiler. Remember NO cin or cout!!!"
 

HaMMerHeD

[H]F Junkie
Joined
Jul 13, 2004
Messages
10,398
Dillusion said:
Professor says "Beware to make sure you use a C compiler and NOT a C++ compiler. Remember NO cin or cout!!!"

I don't envy you, mate.
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
drizzt81 said:
well, scanf and printf isn't all that bad either. :D

Our first assignment is to use printf and scanf to ask a user how much beer/cost of beer and give them how much they would spend in a year and drink in a year....and im stuck :(

I dont get this C shit.
 

Shadow2531

[H]ard|Gawd
Joined
Jun 13, 2003
Messages
1,670
If you want try the Mingw compiler that comes with DevC++ to see if it'll work on Win64 ( since you can't install a non-beta DevC++ ), but also only want the C part, download this ( I'll leave that up for a while ).

Extract it to wherever you want.

Then, just run startconsole.bat in the bin directory. ( You can create a shortcut to it if you want and place it in your quick launch toolbar or on your desktop. Also, once the console is loaded, right-click on its taskbar button -> properties -> options -> check "quick edit mode" -> "save properties for future windows" -> click O.K. That will allow you to highlight without going through the menu and allow you to paste by right-clicking.)

The startconsole.bat automatically adds the bin directory to the path for itself so gcc.exe can be found.
The console is setup by default to start in your desktop directory. ( That way you can create .c files on your desktop, fire up the console and build them. You can change the settings in initconsole.bat.)

The mingw packages included in the distro are from the 'current' section at http://www.mingw.org/download.shtml
gcc-core-3.4.2-20040916-1.tar.gz
mingw-runtime-3.9.tar.gz
w32api-3.6.tar.gz
binutils-2.15.91-20040904-1.tar.gz
gdb-5.2.1-1.exe
mingw32-make-3.80.0-3.tar.gz

( to add c++ support, you'd add gcc-g++-3.4.2-20040916-1.tar.gz )

To test it, fire up a text editor of your choice and type the following:

Code:
#include <stdio.h>

int main() {
    printf("\nHello, world!\n");
    return 0;
}

Save that as hello.c to your desktop. ( Make sure there's a blank line at the end of the text file.)
Load startconsole.bat

Then, type gcc -Wall -Wextra hello.c -o hello and press enter
Then, type hello and press enter to execute the program

To make gcc show errors for things that don't conform to the ISO C90 standard:
gcc -Wall -Wextra -ansi -pedantic file.c -o file

You can't use // for a comment. You have to use /* comment */.
You can't delcare variables in for loops or put variable declarations anywhere but the beginning of a block.
You have to return for main. It's not implied.

Now, if you do:
gcc -Wall -Wextra -std=c99 file.c -o file
, you can do all the good stuff, but it might not build with old|other compilers or be allowed for you.
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
lol i JUST learned how to make that Hello world program :D

EDIT: Also, how do I run the program after i write it? I have two files. hello.c and hello.exe and when i click 'Run' in DevC++ a CMD prompt shows up but dissapears immediately...is this x64 messing with me?
 

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
run hello.exe from a command prompt, not the run... thing
do:
start->run->cmd <return>

as a programmer you should get familiar with the command line, at least a little bit.
 

acidic

Gawd
Joined
Jun 25, 2005
Messages
952
At my university, we use vim and compiled with gcc, later on we used IDE like eclipse made everything a whole lot less tedious, you might want to give it a try.
 

Shadow2531

[H]ard|Gawd
Joined
Jun 13, 2003
Messages
1,670
Dillusion said:
lAlso, how do I run the program after i write it? I have two files. hello.c and hello.exe and when i click 'Run' in DevC++ a CMD prompt shows up but dissapears immediately...is this x64 messing with me?

As already said, you need to run hello.exe from a command prompt. However, if you're allowed, you can do it like this:

Code:
#include <stdio.h>

void holdUp() {
    printf("\nPress ENTER to exist . . .");
    /* 
       So this function doesn't automatically finish, flush stdin so 
       getchar() doesn't suck up any chars left in the input
       buffer ( if any ) 
    */
    fflush( stdin ); 
    getchar(); /* ask user for a char */
}

int main() {
    printf("\nHello, world!\n");
    holdUp();
    return 0;
}
 

acidic

Gawd
Joined
Jun 25, 2005
Messages
952
you can add these two lines, which I believe comes in default in dev-c++

system("PAUSE");
return 0;
 

Shadow2531

[H]ard|Gawd
Joined
Jun 13, 2003
Messages
1,670
Dillusion said:
Our first assignment is to use printf and scanf to ask a user how much beer/cost of beer and give them how much they would spend in a year and drink in a year....and im stuck :(

I dont get this C shit.

Here are 2 examples that show how to use scanf, printf and store and multiply decimals:

Code:
#include <stdio.h>

int main() {
    float a = 2.35;
    float b = 8;
    float c = 634.96;
    printf("\n$%f\n", a * b * c );
    printf("\n$%4.2f\n", a * b * c );
    return 0;
}

Code:
#include <stdio.h>

int main() {
    float x;
    scanf("%f", &x);
    printf("\n%f\n", x );
    return 0;
}

printf info
scanf info
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
HaMMerHeD said:
It never fails to amaze me that people still use straight C when C++ is available. String handling in ANSI C is a nightmare, and should be punishable by stoning.
This is a pretty ignorant assertion, even if you manage to ignore the distemperate "punishment". Before you say this again publicly, you should try writing the same code using only ANSI C functions as you do with the std::basic_string library, and do so on a couple of different platforms.

You'll find that std::string sucks in practice in both performance and in interface, and it shouldn't take you too long to realize that if you're doing any realistic usage.
 

HHunt

Supreme [H]ardness
Joined
Apr 12, 2001
Messages
6,063
I can only agree with mikeblas here.
But by all means, feel free to contribute a real C++ version to this thread. If it's as fast as the C, or notably easier to read/understand, you've proven your point.
(Mine doesn't qualify as "real C++", it's more C with a touch of OO.)
 

HaMMerHeD

[H]F Junkie
Joined
Jul 13, 2004
Messages
10,398
mikeblas said:
This is a pretty ignorant assertion, even if you manage to ignore the distemperate "punishment". Before you say this again publicly, you should try writing the same code using only ANSI C functions as you do with the std::basic_string library, and do so on a couple of different platforms.

You'll find that std::string sucks in practice in both performance and in interface, and it shouldn't take you too long to realize that if you're doing any realistic usage.

Gawrsh....I didn't mean to upset Your Majesty, there. I just meant to make an exaggerated sarcastic remark about how I don't care for ANSI C string handling.

I guess you sure put me in my place...
 

GAMMA_BLADE

Limp Gawd
Joined
Aug 28, 2004
Messages
170
eclipse with the C plugins works really well. Or if you have the time and knowledge Dual boot linux which has gcc native on it. I use linux myself and it works really well.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
Hammered said:
I just meant to make an exaggerated sarcastic remark about how I don't care for ANSI C string handling.
You didn't even succeed at that!
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
What is the difference between %d and %f? is %f only for instances where you would use a decimal? Like %.2f? I really dont get this C stuff. The book I have now 'C by Dissection' isnt much of a help.

How do i know when to define a variable with int or define, and how do i know if i need to define an equation before my printf and scanf's or within an if..else statement?
 

HHunt

Supreme [H]ardness
Joined
Apr 12, 2001
Messages
6,063
Dillusion said:
What is the difference between %d and %f? is %f only for instances where you would use a decimal? Like %.2f? I really dont get this C stuff. The book I have now 'C by Dissection' isnt much of a help.

How do i know when to define a variable with int or define, and how do i know if i need to define an equation before my printf and scanf's or within an if..else statement?

%d is for integers, %f for floating-point. I personally use %i instead of %d, and as far as I can see they are identical. (%i for integer, %f for floating point seems easier to remember.)

"#define MYVAR 2" actually means "search and replace all occurances of 'MYVAR' with '2' before compiling". You can use this for other things than values, too. [1]
Generally, use #define to replace hard-coded values. Instead of "feet = metres * 3.3", use
Code:
#define FEET_PER_METER 3.3
(...)
feet = metres * FEET_PER_METER;
This makes it easier to read the code, and if you ever want to change the value (e.g. to increase the precision) you only have to change one line. Besides, you don't have to remember the actual value when writing the code.

You can also use #define to put your default values somewhere. I've done something like this (pseudocode) :
Code:
#define MUTATION_RATE 15
(...)
int mutation_rate = MUTATION_RATE;
loop through the arguments.
    if (argument is mutation rate) mutation_rate = value from argument;
end loop.
The benefit from this is that you can move the compile-time tweakables together at the start of the file and give them reasonable names. It's rather convenient if you have a few of them spread around.

For everything else, use variables.


[1] You could do "#define start {" and "#define stop }" to write code that looks a bit like pascal, with the bonus effect of driving whoever else reads your code mad. Just remember not to use the words "start" and "stop" anywhere else in the program, since all uses will be replaced.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
Note that variables can be declared as constants, and those might be more appropriate than a #define macro in some situations.

Also note that you can use an anonymous enum to declare symbolic constants. This is sometimes better than using a plain old #define.
 

HHunt

Supreme [H]ardness
Joined
Apr 12, 2001
Messages
6,063
mikeblas said:
Note that variables can be declared as constants, and those might be more appropriate than a #define macro in some situations.

Also note that you can use an anonymous enum to declare symbolic constants. This is sometimes better than using a plain old #define.

Feel free to provide examples. :)
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
You can find examples in any good book on C++ programming (or C, if the OP and his professor haven't yet realized there's so little reason to discriminate between the two); particuraly, books on programming style should cover the issue.

Off the top of my head, one of the biggest problems with defined macros is that they're global.

Code:
#define ARRAY_SIZE 50

makes ARRAY_SIZE change everywhere. And you might be surprised by such a change. When you declare a variable as const, you can

declare it local to a scope.

Code:
void UseBigArray()
{
   const size_t cnArraySize = 50;
   int* pnArray = (int*) malloc(cnArraySize * sizeof(int));
}

void UseSmallArray()
{
   const size_t cnArraySize = 50;
   int* pnArray = (int*) malloc(cnArraySize * sizeof(int));
}
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
So do i use #define for %d and int for %f? My professor showed us to define variable with int and #define, but he didnt specify.

So far I have this, but i cant even begin to see where i can define my variables or write my expressions.

Code:
#include<stdio.h>
#include<stdlib.h>

int main (void)
 {
         // Define variables here?
         int calories = 150, days = 365;
         #define
         expr1 = calories * beers;
         
         //Define expressions here...but i dont even know where to start

         scanf("On average, how many beers will you consume each day?\n"); scanf("%d" , &beers);  
         scanf("On average, how much will you pay for each can of beer?\n"); scanf("%d" , &beers);
              
         printf("That is approximately %d beers in one year.\n");            
         printf("In one year, you will consume approximately %d calories from beer alone.\n");                 
         printf("Without diet or exercise to counter these calories, you can expect to gain %d pounds from drinking that much beer this year.\n");                                   
         printf("You will spend approximately %.2f on beer this year.\n"); 
 
          system("PAUSE");
         return 0;
}

I dont want you guys to break the hoemwork rules by helping me, I just need to egt used to writing C, its allot harder than html :(
 

HHunt

Supreme [H]ardness
Joined
Apr 12, 2001
Messages
6,063
I would suggest using #define for the number of days per year and number of calories per beer. And in general, put all your defines on the top of the file, and give them ALL_CAPS names so they stand out when you use them.

The printf/scanf symbols (%i, %d, %f and the like) signify what kind of number you're dealing with. %f is a float (a number that can have decimals), while %i and %d are integers. I just found the difference between %d and %i: When using scanf, %d will only accept normal, base-10, numbers, while %i will treat numbers like "0x123" as hex and "0123" as octal. When printing (with printf), they are identical. I would suggest you use %d for integers.

In short: If it might have decimals, declare it as a float, and use %f. If it won't have, use int and %d.

An other thing: you read both how many beers and how much they cost into the variable "beers" in the two first scanf lines. (Each scanf overwrites the earlier value, so after the first to questions, "beers" contains the cost.)

The values are also missing from your printf()s, but I guess you're aware of that. And use printf instead of scanf to print things.
 

gamz247

Gawd
Joined
Jun 9, 2003
Messages
655
Oh how I miss writing in C. Java's all nice n' pretty n' stuff, but C was so much fun back in the day.

You're on the right track, man. Just use this for how lay out your program. Fill in the commented out parts with code, naturally.
Code:
/* #include libraries */

/* #define constants */

int main() {

    /* initialize variables */

    /* perform statements */

    return 0;
}

Also, keep your scanfs together

Code:
scanf("On average, how many beers will you consume each day? %d[^\n]" , &beers);
scanf("On average, how much will you pay for each can of beer? %f[^\n]" , &cost);
 

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
If you are ever confused about the define/ variable distinction, I think HHunt made a very good, simple point right here:
HHunt said:
#define MYVAR 2" actually means "search and replace all occurances of 'MYVAR' with '2' before compiling". You can use this for other things than values, too.
Another important distinction is that variables can change their values during runtime (with some exceptions), but #define's cannot.
 

_cashel

[H]ard|Gawd
Joined
Jul 9, 2002
Messages
1,784
Dillusion said:
Our first assignment is to use printf and scanf to ask a user how much beer/cost of beer and give them how much they would spend in a year and drink in a year....and im stuck :(

I dont get this C shit.


When do you have the class? I'm at UCF and am working on the same assignment...weird.
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
_cashel said:
When do you have the class? I'm at UCF and am working on the same assignment...weird.

1:30-2:45 TuThu.

Im in the library right now cause my itnernet is down at riverwind, lol.

Wanna help me? :(
 

_cashel

[H]ard|Gawd
Joined
Jul 9, 2002
Messages
1,784
Dillusion said:
1:30-2:45 TuThu.

Im in the library right now cause my itnernet is down at riverwind, lol.

Wanna help me? :(


Lol I have the same class. I would help you, but I'm clueless when it comes to this shit. I got the first program working, but that was just from trial and error. I have no idea where to start on the second portion. How would I include the option of choosing whether or not it's a leap year, so that depending on what you choose, you get different results from the program?

Right now I've got a few errands to run, and then I'm going out to Greek Park for rushing. I can show you my program and we'll compare results a little later on if you want.
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
_cashel said:
Lol I have the same class. I would help you, but I'm clueless when it comes to this shit. I got the first program working, but that was just from trial and error. I have no idea where to start on the second portion. How would I include the option of choosing whether or not it's a leap year, so that depending on what you choose, you get different results from the program?

Right now I've got a few errands to run, and then I'm going out to Greek Park for rushing. I can show you my program and we'll compare results a little later on if you want.

Im going to Lake Clair to rush a frat aswell....weird haha. Sure, we'll keep in touch- i dont have internet as of now at home and I wont be at this library long, but I do have DevC on my main rig at home. I'll PM you in a hot minute here with my cell and it would be great if I could get a look at the coding.
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
To anyone still looking in here, the program keeps exiting when I input the values from the keyboard and hit enter. Any ideas?

Code:
#include <stdio.h>
#include <stdlib.h>

#define CALORIES 150
#define DAYS 365
#define WEIGHT_GAIN 15

int main ()
{

	float cost, beers, pay;
	int calories, calories_year, pounds;

     printf("\nOn average, how many beers will you consume each day?\n" ,beers); 
	 scanf("%f" , &beers);  
     printf("\nOn average, how much will you pay for each can of beer?\n" ,cost); 
	 scanf("%f" , &pay);

	 beers = DAYS*beers;
	 cost = beers*pay;
	 calories_year = CALORIES*beers*DAYS;
	 pounds = WEIGHT_GAIN*beers;
 
 	 printf("\nThat is approximately %f beers in one year.\n" ,DAYS*beers);            
     printf("\nIn one year, you will consume approximately %d calories from beer alone.\n" ,CALORIES*beers*DAYS);                 
     printf("\nWithout diet or exercise to counter these calories, you can expect to gain %d pounds from drinking that much beer this year.\n" ,WEIGHT_GAIN*beers);                                   
     printf("\nYou will spend approximately %.2f on beer this year.\n" ,beers*pay); 


	return 0;
	system("PAUSE");
}
 

gamz247

Gawd
Joined
Jun 9, 2003
Messages
655
You're multiplying beers by DAYS more than once in this program, which will definitely get you larger values than you intend. Perhaps you should create a variable like "beers_in_a_year" and set that to DAYS*beers so that you're not getting confused.
 

_cashel

[H]ard|Gawd
Joined
Jul 9, 2002
Messages
1,784
Here is mine if you'd like to refer to it. I just called and left a message saying I can meet when/wherever.

Code:
#include<stdio.h>

#define year     365
#define beer_calories         150
#define weight                15/365
int main()
{
    float rate, beer, total_beers, cost, total_cost, total_calories, total_weight;
    year, beer_calories, weight;
        
    //Get data from user        
    printf("On average, how many beers will you consume each day? ");
    scanf("%f", &beer);
    printf("On average, how much will you pay for each can of beer? ");
    scanf("%f", &cost);
    
    //Calculate amount of beers consumed a year
    total_beers = beer* year;
    total_calories = total_beers* beer_calories;
    total_weight = total_beers* weight;
    total_cost = total_beers* cost;
    
    //Print out the amount consumed a year.
    printf("\nThat is approximately %.2f beers in one year.", total_beers);
    printf("\nIn one year, you will consume approximately %.2f calories from beer alone.", total_calories);
    printf("\nWithout diet or exercise to counter these calories, you can expect to gain %.2f pounds from drinking that much beer this year.", total_weight);
    printf("\nYou will spend approximately $%.2f on beer this year.", total_cost);
    system("PAUSE");
    return 0;
}

This seems to work so far, but for everyone else, do you notice any major errors?
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
  • Should have a space between "include" and "<".
  • Abusing #define, and you're lucky it works right.
  • No error checking.
  • Doesn't specify units.
  • Spawns "PAUSE" on the system unconditionaly, even though it's only meant for debugging.
  • Doesn't check return code from system().
  • Should use internals instead of "PAUSE".
  • Kind of odd to begin output strings with newline instead of end 'me that way. You exit without a newline on your last output line as a result of this.
 
Top