Build C application under Visual Studio (or Linux) – and be aware that you really write a C code!

This article is dedicated to the C language which is still popular because is used by the embedded systems. To start learn C when we know already C++ the mainly important point is to recognize the differences. Many people confuse the low-level langulage C with the more developed C++. The main reason is that we can build almost all C application under the C++ compilators such as “g++”. C and C++ syntaxes looks really similar, but they are the same?

There are listed three the most populat mistakes between C and C++:

QUESTION 1: This is a common syntax for C and C++?

for(int i = 0; i < 100; i++){
//…
}

At the first look it seems that YES. So let’s build it by the gcc… And the result is: “error: ‘for’ loop initial declaration used outside C99 mode”. Of course the reason is that it is a C++ systax. In C we have to write this (notice where is the i declaration):

int i;
for(i = 0; i < 100; i++){
//…
}

QUESTION 2: How allocate memory in C? Maybe in this way:

int * p = new int;
double * array = new double[100];
float ** matrix = new float *[10];

Of course NOT. The operators new and delete are introduced only in C++. In C we have to use malloc function from stdlib.h library:

int * p = malloc(sizeof(double));
double * array = (double *)malloc(sizeof(double) * (size_t)100);
float ** matrix = (double **)malloc(sizeof(double *) * (size_t)10);

Don’t forget about free the memory: in C use the free function

QUESTION 3: How write and read from console. Let’s use stream from iostream library, they are really easy:

std::cout << “Sample number: ” << 16 << std::endl;
int c;
std::cin << c;   //Read integer

As you guess the streams we can use only in C++. In C we have to use printf and scanf functions:

printf(“Use to write”);
int c;
scanf(“%d”, &c);  //To read

Summarize to be sure that you really write a C application you should use a C compiler to build it. If you programming under Linux it is easy, because you should just use gcc compuiler instead of g++ (if you had used it before). Under Windows you can use many IDE, I will show you how to use the Visual Studio for C langulage:

  1. Create “Win32 Console Application” or “Win32 Project” in Visual Studio.
  2. Go to: Project -> Properties… -> Configuration Properties -> C/C++ -> Anvanced
  3. In oppend window change value for row: “Compile As” from Default to Compile C as C Code (/TC)

Note that source code written in C should have an extension: .c. Header files are the same with the .h extension.

One response to “Build C application under Visual Studio (or Linux) – and be aware that you really write a C code!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s