Sunday, January 22, 2012

Overview of a computer and Introduction to C language


Introduction

Overview of a Computer - Hardware

·         Central Processing Unit (CPU) controls the flow of instructions and data and performs the necessary manipulation of data
·         Primary storage (memory) is used to store information for immediate access by the CPU
         Note that there are many levels of cache used in primary storage
·         Secondary storage devices (e.g., the hard drive, CD-ROM drives, tapes, etc.) provide permanent storage of large amounts of data, but are much slower than primary storage
·         Input and Output devices provided interfaces between the computer and the user




Overview of a Computer - Data Representation

·         The first computers used vacuum tubes to hold data
·         Vacuum tubes have two states - ON and OFF
·         An ON state represents a 1
·         An OFF state represents a 0
·         Eight vacuum tubes strung together can represent an 8 digit string of 0s and 1s
·         Put another way, this string is an 8 digit binary (base 2) number
·         We use decimal (base 10) numbers in our daily life
·         A decimal number is a string of digits whose values are drawn from the set {0,1,2,3,4,5,6,7,8,9}
·         In general, a number system is simply a way of representing numbers
·         A number system has a base (the number of digits used in the number system)
·         A binary number has a base of 2 where the valid digits are 0 or 1
·         E.g., 1001 binary == 9 decimal (1*8 + 0*4 + 0*2 + 1)
·         An octal number has a base of 8 where the valid digits are 0 through 7
·         E.g., 031 octal == 25 decimal (3*8 + 1)
·         A decimal number has a base of 10 where the valid digits are 0 through 9
·         E.g., 2000 decimal == 2000 (Y2K bug ;-))
·         A hexadecimal number has a base of 16 where the valid digits are 0 through F, i.e. {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
·         E.g., xABBA hex == 43962 decimal (10*4096 + 11*256 + 11*16 + 10)
If you want to know about basics of conversion then click here

Overview of a Computer - Data Representation (2)

Powers of 2


2^0


1


2^11


2048         2K
2^1
2
2^12
4096         4K
2^2
4
2^13
8192         8K
2^3
8
2^14
16,384      16K
2^4
16
2^15
32,768      32K
2^5
32
2^16
65,536      64K
2^6
64
2^17
131,072    128K
2^7
128
2^18
263,144    256K
2^8
256
2^19
524,288    512K
2^9
512
2^20
1,048,576    1M
2^10
1024        1K
2^21
2,097,152    2M

1 KILO = 2^10 = 1024
1 MEG  = 2^20 = 1024*1024      = 1,048,576
1 GIGA = 2^30 = 1024*1024*1024 = 1,073,741,824


To evaluate a binary number, say 101101, simply add up the corresponding powers of 2:

Overview of a Computer - Hexadecimal Numbers


·       A hexadecimal number is a string of hexadecimal digits
·         Digits A, B, C, D, E, F represent the numbers 10, 11, 12, 13, 14, and 15
·         Hexadecimal is popular in the computer field because it can be used to concisely represent a long string of binary digits
·         Consider a 16 digit binary number:

1011000111000101

·         Break this up into groups of 4:

1011  0001  1100  0101

·         Convert each group of 4 into decimal:

11    1   12   3

·         Then convert each decimal number into hex:

B1C3

·         And you now have the number: Baker 1 Charlie 3

Able    = A = 10 = 1010
Baker   = B = 11 = 1011
Charlie = C = 12 = 1100
Dog     = D = 13 = 1101
Easy    = E = 14 = 1110
Fox     = F = 15 = 1111

·         By the same token, the HEX number 3F2C1596 represents the binary string:

0011 1111 0010 1110 0001 0101 1001 0110

Overview of a Computer - Primary Storage

·         Primary storage, also known as main memory or RAM (random access memory) is used to store information for immediate access by the Central Processing Unit (CPU)
·         Memory can be viewed as a series of memory cells with each cell having its own individual address
·         E.g., think of a bank of mailboxes at a post office
·         The information contained in a memory cell is called the contents of that cell
·         Memory cells can be used to store data, such as characters or numbers
·         Internally, of course, they are all numbers, but we can choose to interpret some numbers as characters
·         They can also be used to store program instructions
·         These are “special” numbers that are meaningful to a CPU!
·         The smallest unit of computer storage is a bit, as in binary digit
·         Most computers group bits together to form larger entities
·         E.g., 8 consecutive bits  often form a byte and 32 consecutive bits often form a word
·         A word on an Intel 286 computer is 16 bits or 2 bytes
·         A word on an Intel 386, 486, and Pentium computers is 32 bits or 4 bytes
·         The next generation of Intel computers (e.g. the Merced) will use 64 bit words, i.e. 8 bytes
·         The DEC Alpha computer is currently using 64 bit words
·         Many mainframe computers, such as the Unisys A-Series, use 48 bit words
·         Memory cells in a computer are typically one byte or one word in size
·         A word is a unit of information that can be transferred to and from memory
·         A kilobyte of memory, as in 1K, is 1024 bytes of memory
·         A megabyte of memory, as in 1M, is 1024K of memory, or 2^20 bytes
·         A gigabyte of memory, as in 1G, is 1024M of memory, or 2^30 bytes
·         Note that a 32 bit word can represent 2^32 different possible values

Programming Languages - Low Level Languages

·         Computers only do what they are told to do
·         Except in Hollywood movies, e.g., 2001, Terminator 2, the Matrix, etc. ;-)
·         In order for a computer to perform a task, it must be given a series of specific instructions in a language it can understand

·         The fundamental language of any computer is its machine language
·         This is typically sequences of zeroes and ones
·         In the very early days of computers, this was the only way one could write programs!!!

·         To relieve the suffering of these early programmers, a higher level language called assembly language was developed
·         Assembly language contains mnemonic words and symbols for the binary machine instructions
·         An assembler maps assembly language instructions into machine language instructions

·         Assembly language programming is indeed a significant improvement over machine language programming
·         However, it has the following drawbacks:
·         Machine dependent - each computer architecture has its own unique assembly language
·         Low level instructions - writing programs is very time consuming, tedious, and error-prone.

Programming Languages - High Level Languages

·         A general trend in computing over the past 4 decades is to elevate programming from low level to higher level languages
·         I.e., high level languages are geared more toward people writing the programs rather than the computer
·         Assembly language instructions map directly to machine instructions
·         High level language instructions must be translated/compiled into machine instructions
·         High level languages are more “problem-oriented” than assembly/machine languages
·         E.g, they require little or no knowledge of the underlying computer architecture
·         Learning how to write/debug programs in high level languages is much easier and less error-prone than learning how to write/debug equivalent programs in assembler
·         E.g., high level languages required fewer statements to do the same thing as assembler
·         Programs written in high level languages can be ported much more easily to different computer architectures
·         E.g., the compiler encapsulates the machine-dependent details of the target assembly language

·         A special program called a compiler is needed to translate a program written in a high level language into assembly code (which is then transformed into native machine code by an assembler)
·         The statement written in the high level language are called source code
·         The compiler/assembler's output is called object code

Language Taxonomy



Significant Points about C

      Small number of keywords
      Widely available, particularly on personal computers and workstations
      Can be used very portably
              Standard library
              Preprocessor may be used to isolate machine dependent code
              Unlike Pascal, which has many dialects
      Native language of UNIX (tm) and Windows NT
      Terse
              Powerful set of operators
              Statements can be very powerful
              Some are bit level operators
              Designed to be implemented efficiently on many machines
      Modular -- functions
              Parameters are typically passed `by value’
              No nested functions
      Syntax is complicated
   Semantics of certain features are complex and error-prone.

A Comparison of Programming Language Philosophy

Pascal

      Strict Parent (a “bondage and discipline” language ;-))
      Restricts programmer for his/her own good
      A white, automatic transmission automobile with lots of safety features (e.g.,  air bags, controls that limit speed to 55 miles per hour and prohibit leaving the lights on or locking the keys in the car)

C

      A permissive, easy going parent (a ‘lassize-faire’ language ;-))
      Assumes that the programmer knows what he/she is doing and will assume responsibility for his/her actions.  (Some describe it as a “gun with which you can shoot yourself in the foot.”)
      A bright red ’65 Corvette  with a big block engine, manual transmission, optional seat belt, and with fuzzy dice hanging from the rear view mirror.

C++

      A less permissive, yet open minded parent (e.g.,  ‘Thomas Huxtable’ ;-))
      Assumes that the programmer generally knows what he/she is doing, but provides more checking by default.  (“With C++ it’s harder to shoot yourself in the foot, but when you do, you’ll blow off both of your legs” – Bjarne Stroustrup)
      A bright red 2000 Corvette  with a 6 speed manual transmission,  air bag, and heads up display, many on board computers

Example C Program


/*
 File: hello.c
 Description: Prints a greeting to stdout.
  */
#include <stdio.h>

int main (void)            /* execution starts in main */
{
    printf ("Hello world.\n");
    return 0; 
}


     All C programs must have a function in it called main
     Execution starts in function main
     C is case sensitive!
     Comments start with /* and end with */.  Comments may span over many lines.
     C is a “free format” language.

          The #include <stdio.h> statement instructs the C compiler to insert the entire contents of file stdio.h in its place and compile the resulting file

Review – Lecture - 1


Introduction to C


Computer Program:

A set of instructions given to the computer to perform a particular task is called Computer program software.

Programming Language:

·         Provides a way of communication between user and computer.
·         Computer program is written in a programming language.
·         There are many programming language. Like C, C++, etc.
·         Each language has its own set of rules to write computer programs.

Computer language is used to compile the software. A software is compiled by computer language and is a program on the computer.

Types of Programming Language:

·         High level programming languages
·         Low level programming languages

ü  High level programming languages are more structured, are closer to spoken language and are more intuitive than low level languages.
ü  Higher level languages are also easier to read and can typically sometimes work on many different computer operating systems.
ü  Some examples of higher level languages are Java, Visual Basic, COBOL, BASIC, C++, and Pascal to name only a few.
ü  Lower level languages are typically assembly languages that are machine specific.

Computers run instructions at the binary level, interpreting zeros and ones as instructions. Rather than writing programming code at this level, we've developed languages that compile into the zeros and ones that computers understand. As these languages become more robust, they get further and further way from zeros and ones, becoming higher level languages.

Characteristics of High level programming languages

ü  Easy to Learn
ü  Machine Independent
ü  Shorter Programes
ü  Well defined Syntax and Standard
ü  Easy to detect Errors

Machine Language:

Machine language is the actual bits used to control the processor in the computer, usually viewed as a sequence of hexadecimal numbers (typically bytes). The processor reads these bits in from program memory, and the bits represent "instructions" as to what to do next. Thus machine language provides a way of entering instructions into a computer (whether through switches, punched tape, or a binary file).

Assembly Languge:

Assembly language is a more human readable view of machine language. Instead of representing the machine language as numbers, the instructions and registers are given names (typically abbreviated words, or mnemonics, eg ld means "load"). Unlike a high level language, assembler is very close to the machine language. The main abstractions (apart from the mnemonics) are the use of labels instead of fixed memory addresses, and comments.

An assembly language program (ie a text file) is translated to machine language by an assembler. A disassembler performs the reverse function (although the comments and the names of labels will have been discarded in the assembler process).

Machine language faster than assembly language even than assembly language depend upon machine language

A programming language that is once removed from a computer's machine language. Machine languages consist entirely of numbers and are almost impossible for humans to read and write. Assembly languages have the same structure and set of commands as machine languages, but they enable a programmer to use names instead of numbers.

Each type of CPU has its own machine language and assembly language, so an assembly language program written for one type of CPU won't run on another. In the early days of programming, all programs were written in assembly language. Now, most programs are written in a high-level language such as FORTRAN or C. Programmers still use assembly language when speed is essential or when they need to perform an operation that isn't possible in a high-level language.

Program Source Code and Object Code:


"Source" and "object" are not well-defined classes of code. They are actually relative terms. Given a device for transforming programs from one form to another, source code is what goes into the device, and object code (or "target" code) is what comes out.  The target code of one device is frequently the source code of another.  For example, both early C++ compilers and the Kyoto Common Lisp compiler produced C code as their output.  C++ (or Common Lisp) was the source language; C was the target language.  This output was then run through a C compiler to produce symbolic assembler code as output.  This then had to be run through yet another program, the "assembler", to produce binary machine code that could be directly executed by a processor.
 
Programs typically go through a series of transformations from higher level to lower level languages. The assembler language code that a C compiler produces as "object" code is source code for the assembler.  Today, the GNU C compiler (known as gcc) will deliver assembler language code instead of binary if the user requests it by specifying the -S switch.
 
 Language Translator:

All computer programs run on machine code that is executed directly on computer.

Types of Language Translator:

Compiler -- reads human-readable source code, produces machine-executable binary code. Examples are C, COBOL, Java, etc. Easiest for humans to program, but does not always produce the most efficient executables.

Interpreter -- Reads human-readable code, line at a time, and produces and executes machine instructions "on the fly". Example is good old BASIC. Good for testing, but is VERY slow.

Assembler -- Converts machine-manipulation coding directly into binary machine instructions. Produces the most efficient executables, but is the most difficult (for humans) to work with.

What is the difference between assemblers interpreters and compilers?

Assemblers use the basic building blocks of the command processor code to write programs and is the language closest to the binary on which all computers operate, although it is difficult to use it does work well for things like networking and communication protocols.

Interpreters are just what they say, they translate the code in real time as you operate the program, then process it, and are therefore the slowest.

Compilers translate the code into a format the computer understands prior to the execution (or distribution) of the code and is therefore the easiest to use as it combines the better attributes of both programming methods into one easy to use package.
 Assemblers convert Assembly code to machine code. Interpreters convert high level code to real-time machine code and store it in the memory for direct execution. Compilers convert high level code to real-time machine code or some intermediate code and store it in a file for later execution.

Compiler convert whole of the sentence once into the machine langauge but the interpreter convert one by one. A compiler translates code from a source language (e.g. C, C++, Java) to a target language, which can then be executed by a (virtual or physical) machine. An interpreter reads code in an interpreted language (e.g. PHP, Perl, JavaScript) and directly executes the contained instructions.

C-Language

C (pronounced like the letter C) is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. Although C was designed for implementing system software, it is also widely used for developing portable application software.

C is one of the most widely used programming languages of all time and there are very few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, most notably C++, which began as an extension to C.

Advantages of C-Language:

ü  Easy to Understand, Learn, Use
ü  Middle-Level Language
ü  Basis for C++

Basic Strutruture of C Programe:

Every C program consists of one or more functions. A function is nothing but a group or sequence of C statements that are executed together. Each C program function performs a specific task. The ‘main()’ function is the most important function and must be present in every C program. The execution of a C program begins in the main() function. 

The basic structure of C program consists of the following main pasrts.
ü  Preprocessor Directives
ü  The main() function
ü  C statements

 

Preprocessor directives:

The instructions that are given to compiler before the beginning of the source code are called preprocessor directivities. It is also known is compiler directivities. The preprocessor directivities begins with a # sign followed by word “include” or “define”.

Header files in C-Language

ü  At the beginning of program headers files are included.
ü  The headers files are the part of the C compiler.
ü  It contains the information of standard library functions.
ü  The functions are called in the main body of the program to perform different tasks.
ü  In C language, there are several header files. Each header file contains declarations of one type of functions. i.e. “math.h” contains the declarations of mathematical functions.

Each header file has an extension “.h”.  Include # is used to add the header file into program. The name of header file is written between angle brackets “< >”

The syntax of header file
# include <name of header file>
For examples;

“#include <stdio.h>”.  “stdio.h” is header file. The word “stdio” stands for standard input and output.
“# include <conio.h>, is use its functions “clrscr()”. The “clrscr()” is used to clear the clear screen of monitor.

The main() Function

ü  C-programs consist of one or more functions.
ü  Every C program must have the main () functions.
ü  The main () functions comes after the preprocessor directives.
ü  That point at which execution of program is started.

The program is executed within an operating system. At the end of program execution, the main () function returns a value to the OS. The OS uses this value to determine whether the program is executed or not.

  • int main() can be used to return integer value to the OS.
  • void main() means that the program will return any value to the OS after its execution.
  • main(void) indicates that program takes no parameters when program is executed from command line.

The syntax of main () function is:

Void main (void)
{
C-program statements, each end with semicolon (;)
}

Statements of C-program is written between Curly brackets i.e. {}

For example:

# include <stdio.h>
#include<conio.h>
main ()
{
clrscr();
printf (“AOA”);
}

In the above program two statements are written in the body of main () functions. Each statements end with semicolon (;).

Writing your first C program 


Learning any programming language becomes easy with a hands-on approach, so let’s get right to it. The following is a simple C program that prints a message ‘Hello, world’ on the screen.


/*
This program prints a message
*/
1.#include<stdio.h>
2.main()
3.{
4.             printf(“Hello, world”);
5.}

Type this program in any text editor and then compile and run it using a C-compiler. However, your task will become much easier if you are using an IDE such as Turbo C (available freely from http://community.borland.com/downloads/). Download and install Turbo C on your machine and follow the steps given below in order to type and run the program given above: 
1.       Go to the directory where you have installed Turbo C.
2.       Type TC at the DOS command prompt.
3.       In the edit window that opens, type the mentioned program above.
4.       Save the program as hello.c by pressing F2 or Alt + ‘S’.
5.       Press Alt + ‘C’ or Alt + F9 to compile the program.
6.       Press Alt + ‘R’ or Ctrl + F9 to execute the program.
7.       Press Alt + F5 to see the output.

If you are using a Linux machine, you have to follow the steps mentioned below:
  1. Go to the Linux command prompt (# or $).
  2. Type vi.
  3. The vi editor will open.
  4. Type the program in the editor.
  5. Press ‘Esc + Shift + ‘:’.
  6. Type ‘w’ + ‘q’ followed by file name ‘hello.c’.
  7. At the command prompt type ‘gcc hello.c’.
  8. Type ‘./a.out’ to run the program.

Note: C is a case-sensitive language. It makes a distinction between letters written in lower and upper case. For example, ‘A’ and ‘a’ would be taken to mean different things. Also, remember that all C language keywords should be typed in lower case.

 

Understanding the program


In the program you saw above, the information enclosed between ‘/* */’ is called a ‘comment’ and may appear anywhere in a C program. Comments are optional and are used to increase the readability of the program. 

The ‘#include’ in the first line of the program is called a preprocessor directive. A preprocessor is a program that processes the C program before the compiler. All the lines in the C program beginning with a hash (#) sign are processed by the preprocessor. 

‘stdio.h’ refers to a file supplied along with the C compiler. It contains ordinary C statements. These statements give information about many other functions that perform input-output roles. 

Thus, the statement ‘#include<stdio.h>’ effectively inserts the file ‘stdio.h’ into the file hello.c making functions contained in the ‘stdio.h’ file available to the programmer. For example, one of the statements in the file ‘stdio.h’ provides the information that a function printf() exists, and can accept a string (a set of characters enclosed within the double quotes). 

The next statement is the main() function. As you already know, this is the place where the execution of the C program begins. Without this function, your C program cannot execute. 

Next comes the opening brace ‘{’, which indicates the beginning of the function. The closing brace ‘}’ indicates the end of the function. 

The statement printf() enclosed within the braces‘{}’ informs the compiler to print (on the screen) the message enclosed between the pair of double quotes. In this case, ‘Hello, world’ is printed. As mentioned earlier, the statement printf() is a built-in function shipped along with the C compiler. Many other built-in functions are available that perform specific tasks. The power of C lies in these functions. 

Be cautious about errors! 

Errors/bugs are very common while developing a program. Errors in the program are called bugs. The process of finding and removing bugs in a program is called debugging. If you don't detect them and correct them, they cause a program to produce wrong results. There are three types of errors — syntax, logical, and run-time errors. Let us look at them: 
  1. Syntax errors: These errors occur because of wrongly typed statements, which are not according to the syntax or grammatical rules of the language. For example, in C, if you don’t place a semi-colon after the statement (as shown below), it results in a syntax error.
printf(“Hello,world”) – error in syntax (semicolon missing)
printf("Hello,world"); - This statement is syntactically correct. 
  1. Logical errors: These errors occur because of logically incorrect instructions in the program. Let us assume that in a 1000 line program, if there should be an instruction, which multiplies two numbers and is wrongly written to perform addition. This logically incorrect instruction may produce wrong results. Detecting such errors are difficult.
  1. Runtime errors: These errors occur during the execution of the programs though the program is free from syntax and logical errors. Some of the most common reasons for these errors are
    1. when you instruct your computer to divide a number by zero.
    2. when you instruct your computer to find logarithm of a negative number.
    3. when you instruct your computer to find the square root of a negative integer.
Unless you run the program, there is no chance of detecting such errors.

Try it out 
1.      Write a C program to print your name on the screen.