What is C
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie.
C is called as 'Middle Level Language' because it is understandable by both machine as well as human.
Types of C Constants
C constants can be divided into two major categories:
(a) Primary Constants
(b) Secondary Constants
C Keywords
Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. Some C compilers allow you to construct variable names that exactly resemble the keywords. However, it would be safer not to mix up the variable names and the keywords. The keywords are also called ‘Reserved words’.
There are only 32 keywords available in C. Following is the list of keywords:
The First C Program
Let us now write down our first C program. It would simply calculate simple interest for a set of values representing principle, number of years and rate of interest.
/* Calculation of simple interest */
/* Author gekay Date: 25/05/2004 */
main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}
Now a few useful tips about the program...
− Comment about the program should be enclosed within /* */. For example, the first two statements in our program are comments.
− Though comments are not necessary, it is a good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written.
− Comments are completely ignored by the compiler.
− Comments cannot be nested.
− A comment can be split over more than one line, as in,
/* This is
a jazzy
comment */
Such a comment is often called a multi-line comment.
− main( ) is a collective name given to a set of statements. This name has to be main( ), it cannot be anything else. All statements that belong to main( ) are enclosed within a pair of braces { } as shown below.
main( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
− Technically speaking main( ) is a function. Every function has a pair of parentheses ( ) associated with it.
− Any variable used in the program must be declared before using it. For example,
int p, n ;
float r, si ;
− Any C statement always ends with a ;
− Once the value of si is calculated it needs to be displayed on the screen. Unlike other languages, C does not contain any instruction to display output on the screen. All output to screen is achieved using readymade library functions. One such function is printf( ). We have used it display on the screen the value contained in si.
The general form of printf( ) function is,
printf ( "<format string>", <list of variables> ) ;
<format string> can contain,
%f for printing real values
%d for printing integer values
%c for printing character values
In addition to format specifiers like %f, %d and %c the format string may also contain any other characters. These characters are printed as they are when the printf( ) is executed.
The output of the last statement would look like this...
Prin = 1000
Rate = 8.5
What is ‘\n’ doing in this statement?
It is called newline and it takes the cursor to the next line. Therefore, you get the output split over two lines. ‘\n’ is one of the several Escape Sequences available in C.
Compilation and Execution
Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor. Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler. Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler.
Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program…
(a)Start the compiler at C> prompt. The compiler (TC.EXE is usually present in C:\TC\BIN directory).
(b)Select New from the File menu.
(c)Type the program.
(d)Save the program using F2 under a proper name (say Program1.c).
(e)Use Ctrl + F9 to compile and execute the program.
(f)Use Alt + F5 to view the output.
Note that on compiling the program its machine language equivalent is stored as an EXE file (Program1.EXE) on the disk. This file is called an executable file. If we copy this file to another machine we can execute it there without being required to recompile it. In fact the other machine need not even have a compiler to be able to execute the file.
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie.
C is called as 'Middle Level Language' because it is understandable by both machine as well as human.
Constants, Variables and Keywords
The alphabets, numbers and special symbols when properly combined form constants, variables and keywords.
A constant is an entity that doesn’t change during program execution.
A variable is an entity that may vary during program execution. Variable names are names given to locations in memory. These locations can contain integer, real or character constants.
Types of C Constants
C constants can be divided into two major categories:
(a) Primary Constants
(b) Secondary Constants
C Keywords
Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. Some C compilers allow you to construct variable names that exactly resemble the keywords. However, it would be safer not to mix up the variable names and the keywords. The keywords are also called ‘Reserved words’.
There are only 32 keywords available in C. Following is the list of keywords:
The First C Program
Let us now write down our first C program. It would simply calculate simple interest for a set of values representing principle, number of years and rate of interest.
/* Calculation of simple interest */
/* Author gekay Date: 25/05/2004 */
main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}
Now a few useful tips about the program...
− Comment about the program should be enclosed within /* */. For example, the first two statements in our program are comments.
− Though comments are not necessary, it is a good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written.
− Comments are completely ignored by the compiler.
− Comments cannot be nested.
− A comment can be split over more than one line, as in,
/* This is
a jazzy
comment */
Such a comment is often called a multi-line comment.
− main( ) is a collective name given to a set of statements. This name has to be main( ), it cannot be anything else. All statements that belong to main( ) are enclosed within a pair of braces { } as shown below.
main( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
− Technically speaking main( ) is a function. Every function has a pair of parentheses ( ) associated with it.
− Any variable used in the program must be declared before using it. For example,
int p, n ;
float r, si ;
− Any C statement always ends with a ;
− Once the value of si is calculated it needs to be displayed on the screen. Unlike other languages, C does not contain any instruction to display output on the screen. All output to screen is achieved using readymade library functions. One such function is printf( ). We have used it display on the screen the value contained in si.
printf( ) Function
printf() is a standard input/output function. It is used to display the value of a variable or a message on a screen. The general form of printf( ) function is,
printf ( "<format string>", <list of variables> ) ;
<format string> can contain,
%f for printing real values
%d for printing integer values
%c for printing character values
In addition to format specifiers like %f, %d and %c the format string may also contain any other characters. These characters are printed as they are when the printf( ) is executed.
The output of the last statement would look like this...
Prin = 1000
Rate = 8.5
What is ‘\n’ doing in this statement?
It is called newline and it takes the cursor to the next line. Therefore, you get the output split over two lines. ‘\n’ is one of the several Escape Sequences available in C.
Compilation and Execution
Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor. Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler. Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler.
Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program…
(a)Start the compiler at C> prompt. The compiler (TC.EXE is usually present in C:\TC\BIN directory).
(b)Select New from the File menu.
(c)Type the program.
(d)Save the program using F2 under a proper name (say Program1.c).
(e)Use Ctrl + F9 to compile and execute the program.
(f)Use Alt + F5 to view the output.
Note that on compiling the program its machine language equivalent is stored as an EXE file (Program1.EXE) on the disk. This file is called an executable file. If we copy this file to another machine we can execute it there without being required to recompile it. In fact the other machine need not even have a compiler to be able to execute the file.
Google Yahoo
No comments:
Post a Comment