Wednesday, April 28, 2010

C Programming on Windows

Some of my friends who started learning C programming have asked me which IDE is good to use on Windows. For me, I personally like to use Microsoft Visual Studio Express which is available for free at http:// www.microsoft.com /express/

Dev-C++ from Bloodshed is also a popular one and it can be downloaded from http:// www.bloodshed.net/ devcpp.html.
The following is an example to create new C project on Visual C++ 2008 Express Edition. Go to File menu>>New>>Project... New Project window will appear. Select Win32 in Project types: Visual C++ Select Win32 Console Application in Templates: Visual Studio Installed templates Enter project name in the name text box and browse the folder to save the project. Click OK. Win32 Application Wizard box will appear. Click Next. Choose Console application for Application type and Check Empty project in additional options: Click Finish button. In the Solution Explorer window near the left, right click Source Files and click Add>>New Item... as shown in the following picture. Add new item window will appear. In the Name text box, enter the file name with .c extension e.g. StrPos.c I have been frequently asked how to write a program to find the case insensitive string without using C library functions and the following is an example.
#include <stdio.h>
typedef signed char   CHAR;
typedef signed int    POSITION;
#define ToL(c) (((c)>='A')&&((c)<='Z')?(c+0x20):(c))
POSITION strcmp(CHAR* s1,CHAR* s2)
{ 
    for(;*s2;s1++,s2++) if(ToL(*s1)!=ToL(*s2)) return 0;
    return 1;
}
POSITION stripos (CHAR* haystack,CHAR* needle,POSITION offset)
{        
    for(;*(haystack+offset);offset++) if(strcmp(haystack+offset,needle)) return offset;    
    return -1;  
}
int main(int argc,char *argv[])
{
  CHAR str1[]="Hello! Good morning!";
  CHAR str2[]="good";
  printf("\nFound at: %d \n",stripos(str1,str2,0));   
  return 0;
}
After that, you can run the program by pressing F5 or by clicking Debug menu>>Start Debugging. You can also click Debug menu>>Start Without Debugging.