facebook like button

17 March, 2011

"Hello World" The first C program

The need: 

The "Hello World" is conventionally is the first program when you learn any language. This just gives you confidence that things are working fine. If you are not familiar to programming and this the the first program which you'll write, you probably need to a bit of setup(if you already have not done). For a quick start on windows just download C-free and get things going(refer remarks section of this post below). For linux you just need to create a file with .c extension.

Once the setup is done, write following code in a new file. Save the file and run the program.
The code:
----------------------------------------------------------
#include<stdio.h>
int main()
{
  printf("Hello World.!\n");
  return 0;
}
----------------------------------------------------------

    Here "stdio.h" , the header file included in the program is the file referred by the program when it comes to the function printf. Every built-in function will be having a header file. The function main()is the start point of any program. main() is declared as int as some compilers don't accept any other returned value of main. The printf function prints whatever is written inside the quotes of this statement as it is. One more thing worth-telling is that '\n' here will not be printed as it is because it is the symbol for giving a line break wherever  it occurs in the statement. Now observe in the end of program "return 0;" is a statement. This is how a function returns a value when it is called and main() is no exception. The details of "return" statement we'll be seeing in next posts(user defined functions). For now just remember that except for the line containing "printf"everything is compulsory and will be there in every C program...

Remark:
   a). Compiler used:
     All these programs are verified and compiled on "C-Free standard 4.0" which can be freely downloaded from following link:
http://www.programarts.com/cfree_en/download.htm
direct download link:
http://www.ipower.com/scripts/clickthroughtracker.cmp?url=http://www.programarts.com/download/cfree4_0_std_setup.exe&username=ipw.programarts
b) for details http://www.java2s.com/Code/C/CatalogC.htm can be referred.

3 comments:

  1. Hi,

    I have been trying to find the minimum number in an array.It shows me no errors or warnings but when i start entering more than 2 values on the console it crashes.Can you tell the problem or is some problem with the console?

    #include
    int main()
    {
    int a[20],i,min;
    printf("enter 20 non negative numbers \n");
    for(i=0;i<20;i++)
    scanf("%d",a[i]);
    min=a[0];
    for(i=1;i<20;i++)
    {
    if(min>a[i])
    min=a[i];
    }
    printf("the min number in the array is %d \n",min);
    return 0;
    }

    ReplyDelete
    Replies
    1. change 7th line of your code to
      scanf("%d",&a[i]);

      Delete
    2. thanks..i dint notice the '&' was missing !!

      Delete

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply