This post we talk about C Programming in Windows using Visual Studio.

Programming in C on Windows or any other operating system are pretty standard procedure. You code, compile, link and execute. Traditionally we have used Turbo C compiler – TC to program on Windows in college. However those were the good old 16bit college days. Also that, for advance C programming students jump to Linux and like operating system because of wide range of documentation and How-To guides available. This blog post is to introduce you to Programming C on Windows operating system.

There is a wide range of compilers available for Windows operating system, you can choose any of them

  • Borland
  • Intel
  • Bloodshed Dev C++
  • Visual Studio
  • Turbo C
  • GCC
  • etc.

I would prefer to use Visual Studio command line compiler for the flexibility and control and learning purpose. When you install Visual Studio, it sets a shortcut to command prompt. This command prompt is configured to run a script that sets up environment variables that control the programming environment. If you run “set” command in this command prompt, you will see more environment variables than normal.

You can right-click on the shortcut > Properties and change the “Start in:” directory to your source code directory so that every time you start, it defaults to your code directory, thus easing your programming.

I prefer to use Notepad++ to edit my programs. I create a text file as “Hello.c” and note the following code to it.

/*
 * Hello.c
 * Simple Hello World program
 *
 * @author achindra
 * @version 0.1
 */

#include <stdio.h>

int main()
{
    printf ("Hello World! n");
    return 0;
}

 

This is the code file. Now we need Visual Studio compiler to compile and link this program to build an executable binary.

Following is how you compile you program to generate obj file.

C:UsersAchinBhaDocumentscodehello>cl /c Hello.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.20506.01 for 80×86
Copyright (C) Microsoft Corporation.  All rights reserved.

Hello.c

C:UsersAchinBhaDocumentscodehello>dir
Volume in drive C has no label.
Volume Serial Number is 1CE3-DB2A

Directory of C:UsersAchinBhaDocumentscodehello

13-08-2009  23:59    <DIR>          .
13-08-2009  23:59    <DIR>          ..
13-08-2009  23:48               178 Hello.c
13-08-2009  23:59               652 Hello.obj
               2 File(s)            830 bytes
               2 Dir(s)  15,208,050,688 bytes free

Once you have successfully compiled your code to an object file, you need to link it to generate an executable.

C:UsersAchinBhaDocumentscodehello>cl Hello.obj /link
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.20506.01 for 80×86
Copyright (C) Microsoft Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 10.00.20506.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:Hello.exe
Hello.obj

C:UsersAchinBhaDocumentscodehello>dir
Volume in drive C has no label.
Volume Serial Number is 1CE3-DB2A

Directory of C:UsersAchinBhaDocumentscodehello

14-08-2009  00:04    <DIR>          .
14-08-2009  00:04    <DIR>          ..
13-08-2009  23:48               178 Hello.c
14-08-2009  00:04            41,472 Hello.exe
13-08-2009  23:59               652 Hello.obj
               3 File(s)         42,302 bytes
               2 Dir(s)  15,194,320,896 bytes free

 

Once you have the binary built, you can execute it

C:UsersAchinBhaDocumentscodehello>Hello.exe
Hello World!

 

You can also do this compilation and linking in one go

C:UsersAchinBhaDocumentscodehello>cl Hello.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.20506.01 for 80×86
Copyright (C) Microsoft Corporation.  All rights reserved.

Hello.c
Microsoft (R) Incremental Linker Version 10.00.20506.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:Hello.exe
Hello.obj

C:UsersAchinBhaDocumentscodehello>Hello.exe
Hello World!

Advertisements