Tuesday, April 1, 2008

Fundamentals of Programming in C

Bits & Bytes

For a good understanding of C , knowledge of bits is essential .

Decimal System

We are all used to a decimal number system in which the base is 10. All place values are powers of 10. This system is called as the Decimal system.

Base :- 10

Digits:- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Place Values:-

Thousands

Hundreds

Tens

Units

103

102

101

100

x 1000

x 100

x 10

x 1

The value of a digit in a number depends on its position in the number.

e.g:- Suppose a number = 256

Interpretation:- 2 x 100 + 5 x 10 +6 x 1 = 200 + 50 + 6 = 256

Binary System

In this system the base is 2. The place values are all powers of 2. We can only use 2 digits 0 or 1. The word bit stands for Binary digit which is either 0 or 1.

Base : - 2

Digits :- 0 , 1

Place Values:-

23

22

21

20

x 8

x 4

x 2

x 1

The value of a digit in a number depends on its position in the number .

For e.g. :- Suppose the number is 01011001

27

26

25

24

23

22

21

20

x128

X 64

x 32

x 16

x 8

x 4

x 2

x 1

0

1

0

1

1

0

0

1

This number is interpreted as:- 64 + 16 + 8 + 1 = 89

Thus (01011001) 2 = (89) 10

You may wonder how to express a decimal in binary ? It is very simple.

e.g. 65 = 64 + 1 i.e., 100 0001 or 0100 0001

133 = 128 + 4 + 1 i.e., 1000 0101

Bits

The binary digits 0 , 1 are called Bits

Why are bits that important for a computer?

The computer is an electronic device. It can only recognize 2 electrical states - whether voltage is coming or not coming. The two states are the ON state and the OFF state. The ON state is considered as the TRUE state i.e., 1, the OFF state is considered as the FALSE state i.e., 0.

ON

1

OFF

0

Thus we have a method of communicating with the machine using bits. 2 bits are too small, so we use 8 bits / 16 bits / 32 bits / 64 bits to communicate with the machine.

Exercise : Express 0 to 15 using a 4 - bit notation:-

Decimal No

Binary Equivalent

0

0000

1

0001

2

0010

3

0011

4

0100

5

0101

6

0110

7

0111

8

1000

9

1001

10

1010

11

1011

12

1100

13

1101

14

1110

15

1111

Thus we see that with a 4 bit notation, we will be able to express numbers from 0 to 15 . If we want to express 16 we need more bits. The number of bits defines the number of combinations and the range possible.

Number of bits = 4

Combinations = 16 ( 0 , 1 2 , ….. 15 ) i.e., 24

Range = 0 to 15 i.e., 0 to (24 – 1)

Generalizing this for a n bit notation

Number of bits = n

Combinations = 2n

Range = 0 to (2n – 1)

Thus the number of bits used in a data representation limits the range of values for that data.

8 Bit notation:-

Least number is 0

Greatest number is 1111 1111

128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255

The greatest 8 bit number is 255

Thus with a 8 bit notation we can express at the most 256 different combinations of 0‘s and 1’s.

ASCII Notation:- (8 bit Code) American Standard Code for Information Interchange

The ASCII standard is accepted worldwide. In this code all alphabets, numbers, special characters have a number associated with them . This number is called as the ASCII code.

For e.g. :

ASCII code of ‘A’ is 65 ---- > 0100 0001

‘B’ is 66 ---- > 0100 0100

‘Z’ is 90 ---- > 0101 1010

and so on . Thus we can express any data / expression / statement using the ASCII notation - i.e., 1’s and 0’s

The ASCII code is an 8 bit code. Every character is a stream of 8 bits.

ASCII Codes & C:-

A good knowledge of ASCII is essential for a C programmer. Some common ASCII codes are:-

Note:^A to ^Z 1 to 26

‘a’ – ‘z’ 97 to 122

‘A’- ‘Z’ 65 to 90

‘0’ – ‘9’ 48 to 57

Space 32

Enter Key 13

New line 10

Escape Key 27

Extended ASCII Set:-

ASCII character 128 to 255 are known as the Extended ASCII characters set. The characters are machine & printer dependent. ASCII 0, 255 are both NULL characters.

Unicode:

The Unicode character set is a 2 byte code. Each character is represented by a 16 bit stream.

Advantage: More variety of characters (a total of 65536) can be represented.

The binary concept can be extended to any base .

Octal numbers:

The base is 8 and the digits are 0 to 7 and the place holders are all powers of 8

e.g (467)8 = 4 X 82 + 6 X 81 + 7 X 80

= 4 X 64 + 6 X 8 + 7 X 1 = 256 + 48 + 7 = 311

Hexadecimal numbers :

The base is 16 and the digits are 0 to 9 , A to F .

A stands for 10 , B for 11 , C for 12 , D for 13 , E for 14 , F for 15. The place holders are all powers of 16.Let us see what a hex number like 1AE would mean . Digits are not case sensitive 1AE is the same as 1ae

(1AE)16 = 1 X 162 + A X 161 + E X 160

= 1 X 162 + 10 X 161 + 14 X 160

= 1 X 256 + 10 X 16 + 14 X 1 = 256+160+14 = 420

Binary , Octal , Hex Conversions :

1 Octal digit = 3 bits and vice versa

1 Hex degit = 4 bits and vice versa

Thus (1AE)16 = (0001 1010 1110 )2

[ 1 = 0001 A = 1010 E = 1110 ]

or in 16 bit notation 1AE = 0000 0001 1010 1110

(456)8 = 100 101 110

(4 = 100 5 = 101 6 = 110)

or in 16 bit notation , filling up zeroes on the left side

(456)8= 0000 0001 0010 1110

A programmer should have a good knowledge of how data is internally represented in the computer.


Some points about a C program

1. Every C program has a main( ) function. Execution starts from main( ).

2. A program contains key words. Key words are action words. There are 32 key words. The key words in C are:

Auto

break

case

char

const

continue

default

Do

double

else

enum

extern

float

For

goto

if

int

long

register

return

short

signed

sizeof

static

struct

With

typedef

union

unsigned

void

volatile

while





Don’t use key words as identifiers – variable/function names.

3. A program may contain some variables and data .

4. A program may contain some operators like + , - , * , / etc

C has a rich set of 44 operators .

5. A program is made up statements. Every C statement ends with a semicolon ;

6. A statement is made up of keywords, variables and operators

7. C is case sensitive. This means it distinguishes between lower case and upper case. A C program is generally typed in lower case.

for e.g. Age , age , AGE , AgE , agE are all different


8. Library functions are functions that come along with the C compiler. A C program uses library functions like prinf( ), scanf() etc. A program includes calls to these library functions. You need to include appropriate header files to call them in your program. Each function is called to perform a specific job like printf( ) to send the output to the monitor, scanf() to read keyboard input. You use library functions because:

· C does not have any keyword for input /output I/O jobs

· With just 32 words as a beginner you may not be able to write a specific program. So you call library functions that help you to do that.

You need to understand how to call these functions in your program.

8. C under DOS:- When we work under Dos we get a screen known as C’s IDE to type, compile and run a program. IDE stands for Integrated development environment. Study the various menu options & become familiar with them.

Follow these steps for program writing and execution:

- First type the program

- Press F2 to save

- Press ^F9 to compile & run the program

- Press Alt + F5 to see the output of the program

- Debugging - Generally look at the previous line for syntax error

9. C under Unix:- The IDE may be missing here.

Follow these steps:-

a) First type and save the program in any editor like vi

b) Compile at the $ prompt with the command

1. $ cc yourfile.c

c) This generates a file called a.out

d) To run this file either run it with command

1. $ sh a.out

ii. or

e) Set file permission with command

1. $ chmod a.out 755

ii. and then run it as

1. $ a.out

Note :-

Remember to change the name of a.out if you wish to possess the file later

10. Comments:- Comments can be given anywhere in a C program enclosed by /* Comments */

11. Indentation:- means making the program visually easy to understand and read. Giving appropriate spaces, linefeeds make the program readable and easy to debug. Space, new lines do not make a difference to the program.

12.Debugging : means making the program error free . You encounter 2 types of errors :

1. Compiler generated syntax error. They are easy to remove

Usually it is syntax errors like a semicolon ; , quotes “, bracket }is missing etc.

2. Linker generated errors / warnings take some experience to remove them. But you must not ignore the warnings and run your programs.


12 . Structure of a C program:-

# define -------------

# include

main( )

{

--------- ; /* comment */

--------- ;

--------- ;

}

Program 1: The first program in C prints the words “Hello !! welcome to C !! “

# include /* header file inclusion */

main( )

{

clrscr( ) ; /* function to clear screen */

printf(“Hello !! welcome to C !! “ );

return(0);

}

Output:-

Hello !! welcome to C !!

Note:-

1) # include directive is a preprocessor directive.

It places the contents of the concerned file at that point in the program.

2) clrscr( ) is a function to clear the screen.

3) return is a key word.

No comments: