none
How do you use Visual Studio Ultimate 2010/2012 to create C programs?

    Question

  • How do you use Visual Studio Ultimate 2010/2012 to create C programs within the IDE?
    • Edited by jmartinez102891 Tuesday, September 04, 2012 10:20 PM
    • Moved by Konrad Neitzel Wednesday, September 05, 2012 1:00 PM (From:Developer Documentation and Help System)
    Tuesday, September 04, 2012 10:14 PM

Answers

  • Hi,

    the Visual Studio IDE offers a lot of functions to start an application and even debug it. My example is not that good to test, because all you will see is a quick popup of a CMD Window which is drectly closed afterwards.

    Inside the visual studio ide you can set a so called breakpoint. Simply click at the grey area in front of the code line and a red circle should appear. That is a so called breakpoint. When debugging the execution will stop and you can use the visual studio ide to look at variables and all such things.

    So maybe you try the example but you change the code a little bit to that code:

    #include <stdio.h>
    
    int main()
    {
    	int i = 0;
    	i++;
    	i=5;
    	printf("Just a test!");
    	return 0;
    }

    Now set a breakpoint into the line int i = 0; (So you have a red point in front of the line)

    Next you start debugging through F5 or the Debug -> Start Debugging menu. A cmd window should pop up and then inside visual studio you should see the code with a yellow arrow on the line int i = 0;

    Now you can press F10 or use Debug -> Step Over to execute the line.

    Afterwards check out the Locals window (if it is not open so far you can open it through Debug -> Windows -> Locals)
    You shoud see an entry that tells you that there is a variable with name i and value 0.

    Now you could press F10 again, the yellow arrow moves further (so the i++; is executed) and the Locals window is changed to i 1 (and the 1 is even red now because it was changed).

    Another F10 and you see i is now 5 (of course - i = 5 was executed).

    Another F10 and look at the cmd window: The text was printed.

    Another F10 and we are at the end (}) and another F10 and the application ended and Visual Studio goes out of the debug mode to the normal edit mode (Ok, maybe you are going through some other code that wasn't build by you. I came into a crtexe.c - just ignore it!

    That was a quick introduction into debugging an application. A lot more things are possible but that needs more experience and you should start slowly.

    With kind regards,

    Konrad

    Wednesday, September 05, 2012 1:42 PM
  • anything that works in C will also work in C++.


    No, that's not true. C++ has added type safety and
    other rules which will break some C code. Examples:

    MyFunc() // OK in C, error in C++
      {
      puts("Oops!");
      }

    int main(int argc, char* argv[])
    {
      char *p = malloc(100); // OK in C, error in C++

      unsigned char ucarray[] = {'A','B','C','D','E'};
      int *ptr;
      ptr = ucarray; // warning in C, error in C++  
     
      return 0;
    }

    - Wayne

    Wednesday, September 05, 2012 7:27 PM

All replies

  • Hi,

    at the moment I am not sure where you need support. I hope that the following step by step walkthrough is helpfull for you:

    The steps for a simple c application could be:
    - Create a new C++ project, choose Win32 Console Application and choose a name.
    - In the wizard make sure that you choose empty project and do not check any c++ libraries (ATL/MFC)
    - Add a new c++ File but add the extension .c (instead of .cpp which is the default for a c++ file.)

    Enter your code e.g.

    #include <stdio.h>
    
    int main()
    {
    	printf("Just a test!");
    	return 0;
    }

    Now you can compile and run it.

    Please understand that I am moving this thread to the C++ General forum for better support.

    With kind regards,

    Konrad

    Wednesday, September 05, 2012 12:59 PM
  • Thank you, that helps. Another question how do i go about compiling it and then executing it with Visual Studio IDE. I  know its as simple as "cl hello.c" and then "hello.exe" on a command prompt command line, however what is the procedure for using the IDE?

    Wednesday, September 05, 2012 1:23 PM
  • Thank you, that helps. Another question how do i go about compiling it and then executing it with Visual Studio IDE. I know its as simple as "cl hello.c" and then "hello.exe" on a command prompt command line, however what is the procedure for using the IDE?
    You should use Build Solution from the Build menu. You can also customize the IDE to add the Build toolbar, which is what I always do.
     
    Or if you use the green arrow next to the configuration dropdown then the IDE wil first build the program and then run it under the debugger.
     
    If you are asking how to create a project, you should select Win32 Console Application, Empty Project, and add your .c file to the project.
     
    The IDE is very intimidatng at first, but it is easy to use once you get used to it.
     

    David Wilkinson | Visual C++ MVP
    Wednesday, September 05, 2012 1:38 PM
  • Press F5 (if you are using the standard key mappings).  If not, Debug Menu -> Start Debugging.
    Wednesday, September 05, 2012 1:39 PM
  • Hi,

    the Visual Studio IDE offers a lot of functions to start an application and even debug it. My example is not that good to test, because all you will see is a quick popup of a CMD Window which is drectly closed afterwards.

    Inside the visual studio ide you can set a so called breakpoint. Simply click at the grey area in front of the code line and a red circle should appear. That is a so called breakpoint. When debugging the execution will stop and you can use the visual studio ide to look at variables and all such things.

    So maybe you try the example but you change the code a little bit to that code:

    #include <stdio.h>
    
    int main()
    {
    	int i = 0;
    	i++;
    	i=5;
    	printf("Just a test!");
    	return 0;
    }

    Now set a breakpoint into the line int i = 0; (So you have a red point in front of the line)

    Next you start debugging through F5 or the Debug -> Start Debugging menu. A cmd window should pop up and then inside visual studio you should see the code with a yellow arrow on the line int i = 0;

    Now you can press F10 or use Debug -> Step Over to execute the line.

    Afterwards check out the Locals window (if it is not open so far you can open it through Debug -> Windows -> Locals)
    You shoud see an entry that tells you that there is a variable with name i and value 0.

    Now you could press F10 again, the yellow arrow moves further (so the i++; is executed) and the Locals window is changed to i 1 (and the 1 is even red now because it was changed).

    Another F10 and you see i is now 5 (of course - i = 5 was executed).

    Another F10 and look at the cmd window: The text was printed.

    Another F10 and we are at the end (}) and another F10 and the application ended and Visual Studio goes out of the debug mode to the normal edit mode (Ok, maybe you are going through some other code that wasn't build by you. I came into a crtexe.c - just ignore it!

    That was a quick introduction into debugging an application. A lot more things are possible but that needs more experience and you should start slowly.

    With kind regards,

    Konrad

    Wednesday, September 05, 2012 1:42 PM
  • Thanks, this really helps me understand it more. I'll be doing some reading regarding the tutorials for visual studio.
    Wednesday, September 05, 2012 4:48 PM
  • How do you use Visual Studio Ultimate 2010/2012 to create C programs within the IDE?

    I will add one item, since your subject asked "how ...
    to create C programs". VC++ can create either C or C++
    programs. The two languages are different, and what
    works in one may not work in the other. By default,
    if you give your source code module an extension of
    .C it will be compiled according to the rules of the
    C language. If you give it an extension of .CPP it
    will be compiled according to C++ rules.

    These defaults can be overridden by changing the
    Properties for the project being built and/or for
    an individual source module in a project.

    - Wayne

    Wednesday, September 05, 2012 5:12 PM
  • Thanks for taking the time to comment. I appreciate everyone's feedback. When I make reference to creating a C program I mean to say that in the context of the Visual Studio IDE. I understand the language of C and I'm also aware that C and C++ are two different languages however C is more like a sub-language within C++  and anything that works in C will also work in C++. My focus in this forum is to gain a deeper understanding in using the GUI in the Visual Studio IDE to do some basic operations on my coding such as going from source code to object file and then excecutable, or straight to executable.

    Thanks again

    Wednesday, September 05, 2012 7:02 PM
  • anything that works in C will also work in C++.


    No, that's not true. C++ has added type safety and
    other rules which will break some C code. Examples:

    MyFunc() // OK in C, error in C++
      {
      puts("Oops!");
      }

    int main(int argc, char* argv[])
    {
      char *p = malloc(100); // OK in C, error in C++

      unsigned char ucarray[] = {'A','B','C','D','E'};
      int *ptr;
      ptr = ucarray; // warning in C, error in C++  
     
      return 0;
    }

    - Wayne

    Wednesday, September 05, 2012 7:27 PM
  • C++ has added type safety and other rules which will break some C code.

    Here's another subtle change in C++:

    unsigned char ucarray5[5] = {"ABCDE"}; // OK in C, error in C++

    - Wayne

    Wednesday, September 05, 2012 7:49 PM