সহজ সরল প্রথম C প্রোগ্রাম টি লিখতে শিখুন

Submitted by administrator on Mon, 01/02/2012 - 14:22

প্রথম C প্রোগ্রাম টি লিখতে শেখা

 

আমরা জানি C একটি উচ্চ-স্তরের প্রোগ্রামিং ভাষা এবং C প্রোগ্রামগুলি বাইনারি কোডে অনুবাদ করতে C কম্পাইলার (Compiler) দরকার হয় । বাইনারি কোড কম্পিউটার বুঝতে পারে এবং এক্সিকিউট করতে পারে। C প্রোগ্রাম লিখতে যে বেসিক জিনিসগুলি জানতে হবে --

  • The #include directive
  • Header files
  • Comments
  • The main() function
  • The return statement
  • The exit() function
  • The newline character (\n)
  • The void data type
  • Translating a C program into an executable file
  • Debugging

 

A Simple C Program
Let's have a look at our first C program, demonstrated in Listing 2.1. Later in this lesson you're going to write your own C program for the first time.

TYPE
 Listing 2.1. A simple C program.


  /* Simple C program */
  #include <stdio.h>

  main()
  {
     printf ("Suman, bah! This is my first C program.\n");
     return 0;
  }

This is a very simple C program, which is saved in a file called cprog01.c. Note that the name of a C program file must have an extension of .c. If you've installed a C compiler and set up the proper development environment, you should be able to compile this C program and make it into an executable file.

I set up my development environment in such a way that all C programs in this book can be compiled and made into DOS-based applications. For instance, cprog01.exe is the name of the DOS application made from cprog01.c. Note that .exe is included as the extension to the name of a DOS application program (that is, an executable file).

Also, on my machine, I save all the executable files made from the C programs in this book into a dedicated directory called C:\app. Therefore, if I type in cprog01 from a DOS prompt and press the Enter key, I can run the cprog01.exe executable file and display the message Howdy, neighbor! This is my first C program. on the screen. The following output is a copy from the screen:

C:\app> cprog01
Suman, bah! This is my first C program.
C:\app>

 

 

Summary
In this lesson you've learned the following:

  • Some header files should be included at the beginning of your C program.
  • Header files, such as stdio.h and stdlib.h, contain the declarations for functions used in your C program; for example, the printf() and exit() functions.
  • Comments in your C programs are needed to help you document your programs. You can put comments anywhere you like in your programs.
  • In ANSI C, a comment starts with the opening comment mark, /*, and ends with the closing comment mark, */.
  • Every C program should have one but only one main() function. The program execution starts and ends with the main() function.
  • The sequence of a carriage return and a line feed is carried out when the computer sees the newline character, \n.
  • The return statement can be used to return a value to indicate to the operating system whether an error has occurred. The exit() function terminates a program; the argument to the function indicates the error status, too.
  • The void data type can be used to modify the type of a return value for a function. Applying void to main() tells the operating system that the main() function does not return any value after termination.
  • Compiling and linking are consecutive steps that have to be finished before an executable file is produced.
  • Everybody, including yourself, makes mistakes in programming. Debugging is a very important step in your program design and coding.

In the next lesson you'll learn more about the essentials of C programs.

 

Comments

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Why is random access to a disk file necessary?