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.Possibly why C seems so popular is because it is reliable, simple and easy to use.Moreover, in an industry where newer languages,tools and technologies emerge and vanish day in and day out, a language that has survived for more than 3 decades has to be really good. An opinion that is often heard today is – “C has been already superceded by languages like C++, C# and Java, so why bother to learn C today”.

Why to use C?

C was initially used for system development work, in particular the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:

Constant,Variables and keyword

The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. Let us see what are ‘constants’ and ‘variables’ in C. A constant is an entity that doesn’t change whereas a variable is an entity that may change.

Types of C constants

C constants can be divided into 2 major categories:

  1. Primary Constants
  2. Secondary Constants

(a)Primary constants :

(b) Secondary constants:

Rules for constructing integer constants

  1. An integer constant must have at least one digit.
  2. It must not have a decimal point.
  3. It can be either positive or negative.
  4. If no sign precedes an integer constant it is assumed to be positive.
  5. No commas or blanks are allowed within an integer constant.

Rules for constructing Real constants

  1. A real constant must have at least one digit.
  2. It must have a decimal point.
  3. It must have a decimal point.
  4. Default sign is positive.
  5. Default sign is positive.
  6. Ex.: +325.34 426.0 -32.76 -48.5792

Rules for constructing character constants

  1. A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not.
  2. The maximum length of a character constant can be 1 character.

    Ex.: 'A' 'I' '5' '='

Types of C variables

As we saw earlier, an entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real or character constants.The rules for constructing different types of constants are different. However, for constructing variable names of all types the same set of rules apply. These rules are given below.

Rules for constructing variable names

  1. A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort.
  2. The first character in the variable name must be an alphabet or underscore.
  3. No commas or blanks are allowed within a variable name.
  4. No special symbol other than an underscore (as in gross_sal) can be used in a variable name.
  5. Ex.: si_int m_hra pop_e_89

C Keywords

The following list shows the reserved words in C. These reserved words may not be used as constant or variable or any other identifier names.

auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed

The first C Program

Before we begin with our first C program do remember the following rules that are applicable to all C programs:

  1. Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements.
  2. The statements in a program must appear in the same order in which we wish them to be executed; unless of course the logic of the problem demands a deliberate ‘jump’ or transfer of control to a statement, which is out of sequence.
  3. Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword.
  4. All statements are entered in small case letters.
  5. C has no specific rules for the position at which a statement is to be written. That’s why it is often called a free-form language.
  6. Every C statement must end with a ;. Thus ; acts as statement terminator.

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 ) ; return 0; }

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.

There are several such IDEs available in the market targeted towards different operating systems. For example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS;Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux.