Sign in to follow this  
darthstrife

C Programming and Visual Studio 2005 Questions

Recommended Posts

I'm new to programming and from researching on this site, I came to the conclusion that learning the C language is the best way to get started. So, I picked up "The C Programming Language" by Kernighan and Ritchie and Visual Studio 2005 Standard Edition to begin the quest towards being a game programmer. Now, reading through the book I came to my "Hello, World" program. How do I get this to compile in VS 2005? When I debug the program as typed in the book I get an error. When I release the project and try to run it, the prompt comes up and quickly disappears. How do I make a C project in VS 2005? How do I get this program to run and the prompt to stay where I can see it? Thanks in advance.

Share this post


Link to post
Share on other sites
There is a setting within your project settings to switch from Compile as C++ (which is default), to Compile as C.

Share this post


Link to post
Share on other sites
What error does you get and how does your code look? (just post all the code here)

Share this post


Link to post
Share on other sites
Visual Studio is horrible for a beginner. I can't get it to compile things and I've been using it for a few months now. At least it's nice for writing code.

Anyway, you're either not typing the code right or the compiler just doesn't want to work. Maybe you're using a C++ file?

Make sure your code looks like this:

#include <stdio.h>

main()
{
printf("Hello, world\n");
return 0;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
From Visual Studio, Ctrl + F5.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Any way to do this outside of Visual Studio? For people to look at the program that are not programmers?

Share this post


Link to post
Share on other sites
1.) You should not have to switch to "Compile as C" in order to get no errors. C is a subset of C++, so any legal C code will run fine through a C++ compiler (a good one at least, and the one included with Visual Studio is pretty good).

2.) When creating a console application in VS, it automatically makes it so the message "press any key to continue" is displayed when your program stops executing (so long as you don't make an empty project). If you want to do it yourself, just include a scanf() that does nothing.

Share this post


Link to post
Share on other sites
Quote:
Original post by programwizard
1.) You should not have to switch to "Compile as C" in order to get no errors. C is a subset of C++, so any legal C code will run fine through a C++ compiler (a good one at least, and the one included with Visual Studio is pretty good).

2.) When creating a console application in VS, it automatically makes it so the message "press any key to continue" is displayed when your program stops executing (so long as you don't make an empty project). If you want to do it yourself, just include a scanf() that does nothing.


Wrong. C is not a subset of C++. C and C++ are siblings.

Here's a valid C program, that should not compile with any reasonable C++ compiler:


main() //implicit 'int' return type does not exist in C++
{
int* x = malloc( 100*sizeof(int) ); //C++ does not support implicit conversion from void* to any pointer type - you need an explicit cast to make this work in C++
}





http://public.research.att.com/~bs/bs_faq.html#C-is-subset

[Edited by - RDragon1 on April 4, 2006 4:23:11 PM]

Share this post


Link to post
Share on other sites
Guys, all you have to do to compile C code in visual studio is make sure the file extension for the source file ends in .c, and not .cpp.

- xeddie

Share this post


Link to post
Share on other sites
That's it, just create your project, add a new source and name it whatever.c (note the .c extension), the IDE (is supposed to) recognize it and compile it as a C file.

By the way, posting the compile log (the error messages you got) will make it easier to find out the problem.

Best wishes,
José Jorge (GeoMX).

Share this post


Link to post
Share on other sites
I usually do two things to compile c project in VS 2005:
1. Create and save files with .c extension
2. Change the project settings: Configuration settings -> c/c++ -> Advanced -> Compile As. The default of this setting is "Compile as C++ code", change it to "Compile as C code".

Share this post


Link to post
Share on other sites
Quote: 2.) When creating a console application in VS, it automatically makes it so the message "press any key to continue" is displayed when your program stops executing (so long as you don't make an empty project). If you want to do it yourself, just include a scanf() that does nothing.

instead of a bogus instruction, the following does the job:

system ( "PAUSE" );

before the return statement.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this