Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26655

Settings and configurations for C programming with Visual Studio Express C++?

I'm wondering which configuration and settings I can experiment with as I'm learning C with Microsofts VS Express 2010 compiler. It works with C files if I just start an empty project and add my .h and .c files. I don't have to change the mode from C++ to C and I could compile an executable and run it from the command land. Is it possible to from within the IDE launch your project that takes command-line args? If yes, how? I have a modularized program that does heap sorting that takes a list parameter from the command line. I can run it from the command line invoking with command-line args but next time I do this I'd like to launch my program with command-line arguments from within the IDE. Is this possible?

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

#include "sort.h"


/* argc kommer att innehålla antalet argument på kommandoraden
   argv är en vektor med argc strängar som representerar
   argumenten. Observera att första argumentet, argv[0], är
   programnamnet.
*/
int main(int argc, char *argv[]) {
  int *vector, n, i;

  if(argc > 1) {
    n = argc - 1;
    vector = (int *) malloc(n * sizeof(int));

    for(i = 0; i<n; i++)
      vector[i] = atoi(argv[i+1]);

    sort(vector, n);

    printf("Sorted input: %d", vector[0]);
    for(i = 1; i<n; i++)
      printf(" %d", vector[i]);
    printf("\n");

    free(vector);
    return 0;
  } else {
    fprintf(stderr, "Error: No input arguments.\n");
    printf("This program sorts number on the command line.\n");
    printf("Usage: %s n1 n2 n3 ...\n", argv[0]);

    return 1;
  }
}

Upvotes: 1

Views: 3277

Answers (3)

Jay D
Jay D

Reputation: 3307

Other Fellows have already pointed out how to do a IDE change :

When you are compiling with a C++ (g++) compiler and have a "C" code as part of your C++ project you should have the appropriate macros guarding your C code. Something as follows:

#ifdef __cplusplus
extern "C" {
#endif //__cplusplus

`your C Code here `



#ifdef __cplusplus
}
#endif //__cplusplus

Upvotes: 1

pb2q
pb2q

Reputation: 59607

The IDE can launch your program with command-line arguments.

To add command line arguments:

  1. Right-click on your solution in the Solution Explorer.
  2. Select Configuration Properties->Debugging in the sidebar.
  3. Add the command-line arguments that you've been using to the Command Arguments field.

Furthermore, set breakpoints in your code to halt execution if necessary.

One more thing: if you click in the Command Arguments field, or most of the fields in the Configuration Properties GUI, you'll get a new dialog with a text area for editing the field. Along with this, you'll see a Macros>> button that shows you the IDE macros that you can use in many of the configuration fields.

Upvotes: 2

Mahesh
Mahesh

Reputation: 34605

Right click on the project, go to Properties.

  1. Click on Configuration Properties.
  2. C/C++
  3. Advanced
  4. Compile as ( Change to Compile as C code /TC )

Apply the above settings and change your source file to .c extension.

enter image description here

Upvotes: 1

Related Questions