New to C

Hello.

I was interested in programming in C and of course I bought a book. However the book is a little outdated, so they describe how to use the tools such as the compiler, etc. They teach you how to compile and link everything with Borland and Microsoft Visual C++. This book is C For Dummies, printed in '96. I downloaded Visual C++ and it has WAY more features than the one in the book has. I'm lost. Starting out he teaches you to do the usual "Hello world!" script, I started a new project, created a C file, typed in the code, saved it and... How do I compile I tried the Visual Studio command prompt, placed the C file where they told me to, typed in CL HELLOWORLD.C it made the OBJ file and the EXE file, when I click on the excutable file it opens then closes. Is this the way to go

 



Answer this question

New to C

  • rwgreene

    You need to execute from the command line too.  You compiled a console application and if you execute it in a console window the results will stick around. 

    When you double-click on the file from windows, it executes but the console window that it runs in closes as soon as the program finishes, so you don't get to see its results.

    So be in your project directory (the one with hello.c in it)  and do something like

    C> cl hello.c

    C>hello.exe

    and see your results.  You'll find that most of the early exercises and examples of programming in standard C (and standard C++) that you find in books will work this way.

    If you're using the Visual C++ 2005 Express Edition compiler, you can also learn how to do this in a VS project, but there's a bit more toolcraft you have to learn first:

    1. Learn how to create a project that makes a pure command-line application

    2. Learn how to get your source-program into the project

    3. Then build it (F7-key or Build | Build Solution menu) and correct it as needed.

    4. After you get it to build without error, run it (Ctrl-F5 or Debug | Start Without Debugging).  This runs in a console window that pauses so you can see the result.

    5. But command line compilation is somewhat simpler and it works with all of the introductory books.  You can simply use VS to edit and save your files, then do your compiling and running in a console window until you get the hang of the more complicated but also more-general VS model.   Taking baby steps can give you a better foundation and a way to be more confident as you try more sophisticated programs.

    6.  You can get a suitable console session in VS using Tools | My VS Express.  This will put you in the My Documents\Visual Studio 2005\Projects folder that was created for you.  Your projects should be in folders within that to keep things organized and to be consistent with Visual Studio's default conventions.

    7. I have some notes about resources for learning C/C++ at http://ODMA.info/faq/2005/09/q050903b.htm but the other tips haven't been updated to reflect use of the Express Edition yet.  



  • Igor Kryltsov

    Plainly not a lot.  I don't see anything for adding .c files in the add menu, perhaps it's no longer supported It's possible they chose this route rather than C99 compatibility.  If you create a C++ project, your C code will work fine in it, perhaps if you tried that.

     



  • DaveIII

    I'm sure you've had a lot of expirience with C++... The author in the C book said something about You have to know C to program C++... Or he could have said SOME C.

    And I do have express edition; I just remembered.

  • Qing Fang

    There was a time when C++ was new, and it's basis in C was a strength.  Knowing some C may well help someone to quickly get started in c++, but it's quicker to learn C++, IMO, and it also stops you from learning bad habits like passing char * around as strings, using FILE handles, using sprintf instead of ostringstream, etc.

    IMO ( and that of Bjarne Stroustrup ) the majority of C++ courses make this same mistake, and teach C with classes, instead of the C++ standard library.

     



  • Raghu1234

    orcmid, when I got Ctrl-F5 (to debug) it brings up an error message saying UNABLE TO START PROGRAM "C:\HelloWorld.exe"

    Where is the EXE supposed to come from When I go to my Projects folder I don't see any EXE file.

  • Clumsi

    I see what your saying...
    Okay, I typed in the code and hit Ctrl-B and it brings up a box that creates a New Breakpoint.
    Are you using the 2005 edition

  • Marius Gheorghe

    It's possible that you've set your keys up to be like VC6.  But, I still use VC6 at times, perhaps I am confused.

    Build is in the menu, you can see from there what the key is.

     



  • zegeba

    I'm not sure it is looking in the right place.

    Have you created a HelloWorld project in Visual Studio   The Build and Debug work only with a Visual Studio project.

    If you created the .exe file directly using the command-line compiler, Visual Studio doesn't know how to find it.  Just execute it from the console.  Use menu

    Tools | My VS Express

    Then find the folder where you compiled the .exe, then execute it as a command.  For example,

    c:\...\Projects> cd consoleapp

    c:\...\Projects\consoleapp> debug\consoleapp

    executes consoleapp.exe in the Projects\consoleapp\debug folder.

    Hmm, I wonder if "My VS Express" is a tool that I added.  Duhh.  I bet I did.  I need to document that.

    If your problem is with a Visual Studio project, there are many steps to check and I don't know a good way to tell what you did. 

    Have you worked through any of the videos at http://msdn.microsoft.com/vstudio/express/visualc/learning/

    There's an example of creating a console application from an empty project.  You should be able to duplicate that and then see how to morph it into your HelloWorld case.

     



  • webacadie

    By the way, this is the code:

    #include <stdio.h>

    void main()
    {
    printf("Hello World!\n);
    }

  • KrishnaM

    The compile option may be the one that's for the current file.  You can't compile a header file, which is why it disappears.  I don't have express installed, I think you need to check the help for more info.  However, if you find a debug option it will build and run your exe, that could be the one to go for.  Also, if you create a breakpoint in your app, you can use this to see your output before the window closes.

  • JM Willkie

    Now I'm very confused... For some reason in the "Build" menu had a compile option. I clicked it and it said it was complete... But where is the EXE file I searched where the c document is but it isn't there. Now when I go back to the build menu the compile option is gone.


  • Tony Marzullo

    The Express Edition of VC 2005 is currently free.  I recommend it. 

    The C language is subject to a standard, the complexity of the compiler has nothing to do with the language, although a Microsoft compiler will support all sorts of language extensions and libraries, you don't have to use them. 

    If you create a .c file instead of .cpp, the Microsoft compiler will compile it as C, I believe.

    I do believe you're doing your self irreperable harm unless you work really hard in learning C++ to unlearn the aspects of C that become the wrong way to do things in c++.  as well as making it as hard as it could possibly be on yourself.  Good luck.

     



  • John T. Campbell

    Here's the new situation.. I created a new project, then created a blank C file. I wrote the Hello World code and when I hit debug it says UNABLE TO START PROGRAM "C:\HelloWorld.exe"

    So then I hit BUILD instead, it says Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped

    So what good does that do me

  • bjun

    Do you have Visual Studio If so, just hit CTRL-B for build, or F5 to debug.

    Now, do you have a book for C, or C++ They are different beasts entirely, although C++ is built on C, and the VC compiler can handle both.  But, learning C in this day and age seems a little redundant, as well as a lot of work.

    Ah - you have compiled in the command line, and it's worked.  The reason your app disappears is that you've probably got

     

    cout << "Hello world";

    or if you're using C

    printf("Hello world")

    The problem is, the program prints this out, then closes.  You need to add an input line for the program to stop so you can see your work.  I'm not sure how to do this in C, off the top of my head, but in C++ it's

    int i;

    cin >> i;

    I doubt you're trying to learn C++ tho, any book from 1996 probably would tell you to include iostream.h, which is non-standard and finally gone in VC2005.  If your book is for C, I totally recommend you get a C++ one, or even C#.  Either way, C is not widely used, nowadays, certainly you wouldn't want to write a windows app in C.  You could move from C to C++ but that kind of like learning to tap dance before learning to walk.  Plus, from a C++ perspective, people who learn C first tend not to unlearn a lot of stuff that is plain bad to do in C++, although it works.

     



  • New to C