Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
Photo
- - - - -

C for Beginner -- part2 : How to Write a Hello World Program in C Language

c beginner fresher hello world c concept compiler c compiler

  • Please log in to reply
No replies to this topic

#1 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 26 February 2013 - 02:35 AM

In the first part of this series tutorial we had come to know about computer programming, how to design a program, how to implement an algorithm etc. Today we will know about C language, various types of programming language, C compilers and finally how to write a hello world program in C language.

 

 

C Language

The programming language C was originally developed by Dennis Ritchie of Bell Laboratories and was designed to run on a PDP-11 with a UNIX operating system. Although it was originally intended to run under UNIX, there has been a great interest in running it under the MS-DOS operating system on the IBM PC and compatibles. It is an excellent language for this environment because of the simplicity of expression, the compactness of the code, and the wide range of applicability. Also, due to the simplicity and ease of writing a C compiler, it is usually the first high level language available on any new computer, including microcomputers, minicomputers, and mainframes.

One experienced C programmer made the statement, "You can program anything in C, and the statement is well supported by my own experience with the language. Along with the resulting freedom however, you take on a great deal of responsibility because it is very easy to write a program that can destroy itself due to the silly little errors".
 
 
Compilers
A compiler reads the entire program(source code) and converts it into object code, which is a translation of the  program's source code into a form that the computer can execute directly. Object code is also  referred to as binary code or machine code. Once the program is compiled, a line of source code is no longer meaningful in the execution of your program.
 
Interpreter does also same thing as compiler does. In general, an interpreted program runs slower than a compiled program. Remember, a compiler converts a program's source code into object code that a computer can execute directly. Therefore, compilation is a one-time cost, while interpretation incurs an overhead each time a program is run. 
C was created, influenced, and field-tested by working programmers. The end result is that C gives the programmer what the programmer wants: few restrictions, few complaints, block structure, stand-alone functions, and a compact set of keywords. By using C, we can nearly achieve the efficiency of assembly code combined with the structure of Pascal or Modula-2. It is no wonder that C has become the universal language of programmers around the world. Here's some example of programming languages by their level of human perception type.
 
High level (Human Perception Higher) 
  •     Ada
  •     Modula-2 
  •     Pascal 
  •     COBOL 
  •     FORTRAN 
  •     BASIC
Middle level Java  
  •     C++ 
  •     
  •     FORTH 
  •     Macro-assembler
Low level
  •     Assembler
 
Compilation Procedures of a C Program
Creating an executable form of a C program consists of these three steps:
  1.  Creating the program
  2.  Compiling the program
  3.  Linking the program with whatever functions are needed from the library
Today, most compilers supply integrated programming environments that include an editor. Most  also include stand-alone compilers. For stand-alone versions, we must have a separate editor to create your program. In either case, be careful: Compilers only accept standard text files for input. 
For example, the compilers will not accept files created by certain word processors because they contain control codes and non printing characters.The exact method you use to compile your program will depend upon what compiler you are using. Also, how linking is accomplished will vary between compilers and environments; for example, it  may be included as part of the compiler or as a stand-alone application.
 

 

 

Popular C Compilers

In this tutorial we are going to use the Microsoft Visual Studio 10 IDE and its integrated compiler.
 
 
Setting Up The Environment
Download the Microsoft Visual Studio 10 from Microsoft site then install it. Run the Microsoft Visual Studio 10 program and create a project.
  • Create a new project by clicking File->New->Project.
  • From Installed Template choose other language
  • Choose language Visual C++
  • In Visual C++ choose tab Win32
  • Choose project type Win32 Console Application

new_project.png

 

From the dialog box click on Next button.

 

new_project2.png

 

In the W32 Application Wizard please ensure the application type is console application and in the additional options uncheck the precompiled header then click finish.

 

new_project3.png

 

 

The Hello World C Program

To start programming, right click on Source Files and add a new item. You need to make sure below two points,

  • Select C++ File to add 
  • Name the file with extension to .c, default is .cpp. To work with C language program source file name should be with extension .c In this case I am giving source file name as hello_world.c
new_file.png

 

So now we are set to write our first C program. The basic structure of a C program should be

#include header files
The main function(){
        Code in the main function
}

Each C program must follow this structure. There are lots of header files come with the c compilers. We can use this header files in our program by just the include() preprocessor. Just like

#include<stdio.h>
#include<conio.h>

Here we included stdio.h file to use its build in functions like printf(), scanf() etc. This is the most used header file we generally use in a c program. The purpose of using conio.h will be described in time.

So the main block will be

void main(){
      printf(“Hello!. This is our first c program.”);
      getch();
}

The main() function is the starting point in a C program and there must be one main function in a c program. C compilers execute a c program from top to bottom starting from the first instruction in the main function. Each function must have a return type in c language,  here void is the return type of the main function that returns nothing. The printf() function shows generally the message it  argument-ed. So our program should show the message "Hello! This is our first c program" . The getch() is a build in function from header file conio.h that waits until a key is pressed when the output is showing in the console. To compile and execute the program press F5 and we will see(if everything is okay that means there is no error in our program)

execute.png

 

 


  • 1





Also tagged with one or more of these keywords: c, beginner, fresher, hello world, c concept, compiler, c compiler