tags:

views:

2081

answers:

10

Hi,

This is an absolute beginner's question, but per the latest podcast, I understand that no question is too newbie.

I have 0 programming experience and I want to learn C, so I'm starting the K&R book. I am using a Windows XP laptop, and I am planning on using Notepad++ to write, and Code::Blocks to compile.

Here's my question: once I have written the "hello world" program in Notepad++, how should I save it, compile it, and run it?

Edit & new question: When I build & run from Code::Blocks, I get the prompt window with the "hello world" message. It stays open until I close it manually. However, when I double click the .exe file, the prompt just flashes and disappears, why is that?

Many thanks,

JD

Edit: I'm going to use Code::Blocks as an IDE, per the recommendations here.

A: 

Try some simpler compiler, like *nix gcc. Visual Studio C++ puts a lot of MS extensions to code files.

smok1
This is a complete beginner with 0 programming experience. He likely has no idea what MS extensions are. You answer will only confuse him. Sorry, -1.
unforgiven3
@unforgiven3: This is why I suggest him using plain gcc or even free Borland C++ compiler, simply to avoid his exposure to MS extensions. I was taught "pure" C++, and I was shocked when I saw for the first time an empty C++ project in Visual Studio. I want him to avoid this MS-mess. I think you misunderstood me.
smok1
You can compile C code in Visual Studio "the old fashioned way," without any MS extensions. It's easy.
amdfan
And I doubt that an absolute newbie is familiar with '*nix gcc'. These are the type of things that can really confuse a newcomer.
TURBOxSPOOL
+6  A: 

You should be able to write C in Visual Studio. You certainly could in older versions of the IDE. Just create source files with the .c extension.

You should be able to build and run the code from within Visual Studio.

ChrisF
Thank you. I didn't know I could write the code inside Visual Studio. That makes things that much easier.
JDelage
Yes and no - you'll be able to write a fairly old-fashioned style of C, missing the tidy-ups made in the last 15 years or so.
Pete Kirkham
True - but I was pointing out that he had all the tools necessary already.
ChrisF
+7  A: 

Save it as a .c file. And you can use the MinGW compiler to compile the file into an .exe file. But since you have Visual Studio, you should use that compiler.

Ólafur Waage
+1  A: 

For just starting I'd recommend creating your project, editing and compiling right in VC. Then start exploring what commands VC actually runs under its covers (I believe that'd be something like "output" tab there.) You can do all that on command line though Windows is not the programmer-friendliest environment.

Nikolai N Fetissov
+5  A: 

If you're just learning to program, I wouldn't recommend starting with C. C still has its place today, but there are much easier languages with which to learn basic syntax and technique. As I understand, there aren't that many employed beginner C programmers out there. You can, however, be an employed beginner java, c#, VB, ruby, or python programmer and people will pay you to learn. Then, once you have a solid foundation on structure and technique, you can get closer to the metal with C.

Just my two cents even if it isn't really an answer to your question.

D. Patrick
Thanks, those are fair points. I don't plan on a career coding, I just want to develop my knowledge of computer software, and I selected C because (based on what I read) it covers the key principles in a fairly small language.
JDelage
@JDelage. that's exactly why C is a great place to start. it clearly defines the intrinsic strenghts and weaknesses of software in general. higher level languages mask those issues to make it easier for developers, but create an illusion. worse of all are massive do-it-all languages like C#, VB, and Java. if you want to take the easy, fun and somewhat insightful way, go Python or Lua
Javier
+2  A: 

If you DON'T want to use visual-studio, you might want to try something like codeblocks which lets you use the VS compiler... it's much more simple+lightweight if you're just beginning.

Hugo
+1 for codeblocks for a beginner.
unforgiven3
OK, Code::Blocks it is then.
JDelage
+2  A: 

As other posters have pointed out, Visual Studio can be a bit overwhelming to the beginner - lots of options, and a lot of fluff required to get a simple "Hello World" out the door.

I personally recommend Bloodshed's Dev-C++ IDE for beginners. It's not being worked on anymore (so get the stable, not the beta version), but it's very simple to go from typing your first program to compiling and running it. If you stick with Windows, you'll move to Visual Studio eventually, but Dev is a nice east place to start.

You can edit files in Visual Studio, (It's an IDE, an Integrated Development Environment, and is supposed to contain absolutely everything that you need to write a program) but you'll notice that most programmers use external editors like Notepad++, as they offer some nice features that you usually don't get in a built-in IDE. Don't worry about that for the moment, but try out an external editor like n++ when you're more familiar with the rest of the process.

Oh, and C in no way whatsoever can be described as "covering the key principles in a small language". It definitely covers the 'key principles' - it's been used for everything under the sun, but is absolutely huge. Whether it's appropriate for a beginner these days is subject to debate; it'll definitely give you some good experience, but won't be the easiest way of starting.

Edit: CodeBlocks is more modern and still under development, so might be a better place to start than DevC++ http://en.wikipedia.org/wiki/Codeblocks

James Broadhead
Thanks, I stand corrected on the size of C.OK, Code::Blocks it is then.
JDelage
+1  A: 

just in case you haven't realised yet, like visual studio, code::blocks also has an editor built in. you don't need to write in notepad++ (unless you actually prefer npp as an editor of course)

geocoin
+1  A: 

Here's an opinion from someone who used C for years in biomedical research: image processing, data crunching. IMHO, C is a major intellectual achievement, probably the finest distillation of intent into syntax. I would learn C again now, if I were starting out, even if just for the purpose of learning to think.

That said, I have never even attempted to program in C on MS. (All my work was on Suns and SGIs.) To avoid the overhead mentioned in other answers, I have switched to Python on MS XP/cygwin. However, I miss C's terse, expressive syntax rather often, and I miss mucking about with bits and values as stored in memory. Note that even with Python, the back-end is C, and custom extensions are coded in C. Thus, if I wanted to redo my image processing code for Python, I probably would wind up writing C after all.

Sorry that MS imposes such a burden on writing in about the leanest language ever invented. (BTW, as for editors, my personal choice is vim (as gvim), not IDEs.) (Have you thought of setting up a Linux box? Lots to be said for that these days: UNIX without the cost of workstations.)

behindthefall
+2  A: 

If you copy the code out of K&R you need to add a system("pause"); before you end main() and make sure you save the file with .c

int main()
{
printf("Hello, world\n");

system("pause");
return 0;
}
I am also a beginner I hope this helps.

MrWiseGuy