C Instructions

Now that we have written a few programs let us look at the instructions that we used in these programs. There are basically three types of instructions in C:

  1. Type Declaration Instruction
  2. Arithmetic Instruction
  3. Control Instruction

The purpose of each of these instructions is given below:

(a) Type declaration instruction : To declare the type of variables used in a C program.
(b) Arithmetic instruction : To perform arithmetic operations between con-stants and variables.
(c) Control instruction : To control the sequence of execution of various state-ments in a C program.

(a) Type Declaration Instruction

This instruction is used to declare the type of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of main( ) function.

int bas;
float rs, grosssal;
char name, code;

There are several subtle variations of the type declaration instruction. These are discussed below:
(a) While declaring the type of variable we can also initialize it as shown below.

int i = 10, j = 25;
float a = 1.5, b = 1.99 + 2.4 * 1.44;

(b)The order in which we define the variables is sometimes important sometimes not. For example,

int i = 10, j = 25;

is same as

int j = 25, j = 10;

However,

float a = 1.5, b = a + 3.1;

is alright, but

float b = a + 3.1, a = 1.5;

is not. This is because here we are trying to use a even before defining it.
(c) The following statements would work.

int a, b, c, d ;
a = b = c = 10 ;

However, the following statement would not work

int a = b = c = d = 10 ;

Once again we are trying to use b (to assign to a) before defining it.

(b) Arithmetic Instruction

A C arithmetic instruction consists of a variable name on the left hand side of = and variable names & constants on the right hand side of =. The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /.

int ad ;
float kot, deta, alpha, beta, gamma ;
ad = 3200 ;
kot = 0.0056 ;
deta = alpha * beta / gamma + 3.2 * 2 / 5 ;

Here,
 
*, /, -, + are the arithmetic operators.
= is the assignment operator.
2, 5 and 3200 are integer constants.
3.2 and 0.0056 are real constants.
ad is an integer variable.
kot, deta, alpha, beta, gamma are real variables.The variables and constants together are called ‘operands’ that are operated upon by the ‘arithmetic operators’ and the result is assigned, using the assignment operator, to the variable on left-hand side. A C arithmetic statement could be of three types. These are as follows:

(a)Integer mode arithmetic statement – This is an arithmetic statement in which all operands are either integer variables or integer constants. Example

int i, king, issac, noteit ;
i = i + 1 ;
king = issac * 234 + noteit - 7689 ;

(b)Real mode arithmetic statement – This is an arithmetic statement in which all operands are either real constants or real variables. Example :

float qbee, antink, si, prin, anoy, roi ;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;

Mixed mode arithmetic statement – This is an arithmetic statement in which some of the operands are integers and some of the operands are real. Example

float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;

It is very important to understand how the execution of an arithmetic statement takes place. Firstly, the right hand side is evaluated using constants and the numerical values stored in the variable names. This value is then assigned to the variable on the left-hand side.

 
Though Arithmetic instructions look simple to use one often commits mistakes in writing them. Let us take a closer look at these statements. Note the following points carefully.
 
(a) C allows only one variable on left-hand side of =. That is, z = k * l is legal, whereas k * l = z is illegal.
(b) In addition to the division operator C also provides a modular division operator. This operator returns the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. Note that the modulus operator (%) cannot be applied on a float. Also note that on using % the sign of the remainder is always same as the sign of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2 yields 1.
(c) An arithmetic instruction is often used for storing character constants in character variables.

char a, b, d ;
a = 'F' ;
b = 'G' ;
d = '+' ;

When we do this the ASCII values of the characters are stored in the variables. ASCII values are used to represent any character in memory. The ASCII values of ‘F’ and ‘G’ are 70 and 71

(d) Arithmetic operations can be performed on ints, floats and chars.
Thus the statements,

char x, y ;
int z ;
x = 'a' ;
y = 'b' ;
z = x + y ;

are perfectly valid, since the addition is performed on the ASCII values of the characters and not on characters themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98, and hence can definitely be added.
 
(e) No operator is assumed to be present. It must be written explicitly. In the following example, the multiplication operator after b must be explicitly written.
a = c.d.b(xy) usual arithmetic statement
b = c * d * b * ( x * y ) C statement
 
Unlike other high level languages, there is no operator for performing exponentiation operation. Thus following statements are invalid.

a = 3 ** 2 ;
b = 3 ^ 2 ;

If we want to do the exponentiation we can get it done this way:

#include <math.h>
main( )
{
int a ;
a = pow ( 3, 2 ) ;
printf ( “%d”, a ) ;
}

Here pow( ) function is a standard library function. It is being used to raise 3 to the power of 2. #include is a preprocessor directive. It is being used here to ensure that the pow( ) function works correctly.

(c) Control Instructions in C

As the name suggests the ‘Control Instructions’ enable us to specify the order in which the various instructions in a program are to be executed by the computer. In other words the control instructions determine the ‘flow of control’ in a program. There are four types of control instructions in C. They are:

  1. Sequence Control Instruction
  2. Selection or Decision Control Instruction
  3. Repetition or Loop Control Instruction
  4. Case Control Instruction

The Sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program. Decision and Case control instructions allow the computer to take a decision as to which instruction is to be executed next. The Loop control instruction helps computer to execute a group of statements repeatedly. In next tutorials we are going to learn these instructions in detail.

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 Figure gives a list of these keywords for your ready reference.

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

Note that compiler vendors (like Microsoft, Borland, etc.) provide their own keywords apart from the ones mentioned above. These include extended keywords like near, far, asm, etc. Though it has been suggested by the ANSI committee that every such compiler specific keyword should be preceded by two underscores (as in __asm ), not every vendor follows this rule.

C Hello World

Following example presents you a Hello World Example.

#include<stdio.h>
int main()
{
    printf("Hello world");
    getchar();
    return 0;
}

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 a statement terminator.

C program compile and execution stages

C programs are written in a high-level language using letters, numbers, and the other symbols you find on a computer keyboard. Computers actually execute a very low-level language called machine code (a series of numbers). So, before a program level can be used, it must undergo several transformations.
 
Programs start out as an idea in a programmer’s head. He uses a text editor to write his thoughts into a file called a source file, containing source code. This file is transformed by the compiler into an object file. Next, a program called the linker takes the object file, combines it with predefined routines from a standard library, and produces an executable program (a set of machine -language instructions). In the following sections, we’ll see how these various forms of the program work together to produce the final program.
 
Following Figure shows the steps that must be taken to transform a program written in a high-level language into a executable program.

Wrappers

Fortunately you don’t have to run the compiler, assembler, and linker individually. Most C compilers use “wrapper” programs that determine which tools need to be run and then run them.
 
Some programming systems go even further and provide the developer with an Integrated Development Environment (IDE). The IDE contains an editor, compiler, linker, project manager, debugger, and more in one convenient package. Both Borland and Microsoft provide IDEs with their compilers. As open source is converned GCC Dev C++ are the famous editors.

Structure of a C Program

To find out the structure of a C program, consider following

Example

#include<stdio.h>
int main()
{
    int sum;
    printf("Hello World, sum is : ");
    sum=sum1(2,2);
    printf("%d",sum);
    getchar();
    return 0;
}

int sum1(int x, int y)
{
    return x+y;
}

Any C program generally having structure as :-

  1. Include Header Files
  2. Define Global Variables
  3. Define main Function
  4. Execute Statements, Call Other Functions
  5. Define Other Functions

If we consider the above program, then above lines are explained as :

Line Number Explanation
1st We have include header files which are necessary for inclusion because of printf() and other standard input output functions.
2nd To define main() function, it’s the first function that compiler executes and also the starting of out program because this functions is executed at first. Other statements are executed later on.
3rd Opening Curly Brace for starting main() function block.
4th A variable (sum) of type Integer is defined.
5th printf() is a standard function that is used to output data to the console output(computer screen).
6th integer sum is assigned value of 2 variables by calling the function sum1 with parameters 2,2
7th to output sum to computer screen.
8th getchar() is a function which is used to stop computer screen after outputting a value, because if we don’t put it the screen disappear after output of text, so getchar() will wait to type a character from keyboard until output window will not be disappeared
9th A return code 0 is returned by function to the Operating system. So that the operating system knows that program executed successfully. (not necessary)
10th main function closed
12th sum1() function is defined with two temporary parameters.
14th sum of x and y is returned

Why C?

During the past three decades, C has become one of the most important and popular programming languages. It has grown because people try it and like it. In the past decade, many have moved from C to the more ambitious C++ language, but C is still an important language in its own right, as well a migration path to C++. As you learn C, you will recognize its many virtues.

Design Features

C is a modern language incorporating the control features found desirable by the theory and practice of computer science. Its design makes it natural for top-down planning, structured programming, and modular design. The result is a more reliable, understandable program.

Efficiency

C is an efficient language. Its design takes advantage of the capabilities of current computers. C programs tend to be compact and to run quickly. In fact, C exhibits some of the fine control usually associated with an assembly language. (An assembly language is a mnemonic representation of the set of internal instructions used by a particular central processing unit design; different CPU families have different assembly languages.) If you choose, you can fine-tune your programs for maximum speed or most efficient use of memory.

Portability

C is a portable language, which means that C programs written on one system can be run on other systems with little or no modification. If modifications are necessary, they can often be made by simply changing a few entries in a header file accompanying the main program. Most languages are meant to be portable, but anyone who has converted an IBM PC BASIC program to Apple BASIC (and they are close cousins) or has tried to run an IBM mainframe FORTRAN program on a Unix system knows that porting is troublesome at best. C is a leader in portability. C compilers (programs that convert your C code into the instructions a computer uses internally) are available for about 40 systems, running from 8-bit microprocessors to Cray supercomputers. Note, however, that the portions of a program written specifically to access particular hardware devices, such as a display monitor, or special features of an operating system, such as Windows XP or OS X, typically are not portable.
 
Because of C’s close ties with Unix, Unix systems typically come with a C compiler as part of the packages. Linux installations also usually include a C compiler. Several C compilers are available for personal computers, including PCs running various versions of Windows, and Macintoshes. So whether you are using a home computer, a professional workstation, or a mainframe, the chances are good that you can get a C compiler for your particular system.

Power and Flexibility

C is powerful and flexible (two favorite words in computer literature). For example, most of the powerful, flexible Unix operating system is written in C. Many compilers and interpreters for other languages—such as FORTRAN, Perl, Python, Pascal, LISP, Logo, and BASIC—have been written in C. As a result, when you use FORTRAN on a Unix machine, ultimately a C program has done the work of producing the final executable program. C programs have been used for solving physics and engineering problems and even for animating special effects for movies such as Gladiator.

Programmer Oriented

C is oriented to fulfill the needs of programmers. It gives you access to hardware, and it enables you to manipulate individual bits in memory. It has a rich selection of operators that allows you to express yourself succinctly. C is less strict than, say, Pascal in limiting what you can do. This flexibility is both an advantage and a danger. The advantage is that many tasks, such as converting forms of data, are much simpler in C. The danger is that with C, you can make mistakes that are impossible in some languages. C gives you more freedom, but it also puts more responsibility on you.

 
Also, most C implementations have a large library of useful C functions. These functions deal with many needs that a programmer commonly faces.

Shortcomings

C does have some faults. Often, as with people, faults and virtues are opposite sides of the same feature. For example, we’ve mentioned that C’s freedom of expression also requires added responsibility. C’s use of pointers, in particular, means that you can make programming errors that are very difficult to trace. As one computer preliterate once commented, the price of liberty is eternal vigilance.

 

C’s conciseness combined with its wealth of operators make it possible to prepare code that is extremely difficult to follow. You aren’t compelled to write obscure code, but the opportunity is there. After all, what other language has a yearly Obfuscated Code contest?
 
There are more virtues and, undoubtedly, a few more faults. Rather than delve further into the matter, let’s move on to a new topic.

Characteristics of C

C is a general-purpose, procedural programming language. Dennis Ritchie first devised C in the 1970s at AT&T Bell Laboratories in Murray Hill, New Jersey, for the purpose of implementing the Unix operating system and utilities with the greatest possible degree of independence from specific hardware platforms. The key characteristics of the C language are the qualities that made it suitable for that purpose:

  1. Source code portability
  2. The ability to operate “close to the machine”
  3. Efficiency

As a result, the developers of Unix were able to write most of the operating system in C, leaving only a minimum of system-specific hardware manipulation to be coded in assembler.
 
C’s ancestors are the typeless programming languages BCPL (the Basic Combined Programming Language), developed by Martin Richards; and B, a descendant of BCPL, developed by Ken Thompson. A new feature of C was its variety of data types : characters, numeric types, arrays, structures, and so on. Brian Kernighan and Dennis Ritchie published an official description of the C programming language in 1978. As the first de facto standard, their description is commonly referred to simply as K&R.
 
C owes its high degree of portability to a compact core language that contains few hardware-dependent elements. For example, the C language proper has no file access or dynamic memory management statements . In fact, there aren’t even any statements for console input and output. Instead, the extensive standard C library provides the functions for all of these purposes.
 
This language design makes the C compiler relatively compact and easy to port to new systems. Furthermore, once the compiler is running on a new system, you can compile most of the functions in the standard library with no further modification, because they are in turn written in portable C. As a result, C compilers are available for practically every computer system.
 
Because C was expressly designed for system programming, it is hardly surprising that one of its major uses today is in programming embedded systems. At the same time, however, many developers use C as a portable, structured high-level language to write programs such as powerful word processor, database, and graphics applications.