Sunday, 7 July 2013

INSTRUCTIONS IN C LANGUAGE

A program is a set of instructions. Instructions are also known as commands or statements. They are the sentences in program. Just like any other language C also provides variety of instructions.

Types of instructions
l   Data type declaration instruction
l   Input/Output instruction
l   Arithmetic instruction
l  Control instruction
 
Data type declaration instruction

From the list of keywords few are designed to use in declaration instruction. They are required to declare variables and functions. These keywords are
int, float, char, double, void

Here we are only concern with variable declaration. Declaration of functions will be deal in later chapter.

Consider the following statements
int x=5, y;
float k=2.34;
char a=’y’, ch;

These are examples of data type declaration instruction. Compiler when read these instructions, it would get aware of four things:

1)      Name of the variables
Here names are x ,y, k, a, ch
2)      Size of memory block
Size of int type block is 2 bytes, size of float block is 4 bytes, size of char block is 1 byte, size of double block is 8 bytes
3)      Type of content
int variable can contain integer constant, float and double variable can contain real constant and char variable can contain character constant.
4)      Value in variable
x contains 5, k contains 2.34 and a contains ‘y’.
Variables y and ch are not initialized but they are not empty. They contain unpredictable values which are of no use in the program, so called them garbage values.


Modifiers
Modifiers are the keywords used to modify the properties of data type.
Modifiers are short, long, signed, unsigned.

Here is the list of all possible combination of use of modifiers with data type:
Create variables to store integers:
  int,  short int, signed int, long int, unsigned int, unsigned short int, unsigned long int.
Create variables to store real constant
 float, double, long double
Create variables to store character constant
 char, unsigned char   

Modifiers affect size of data type (short and long), data range (signed and unsigned)

Following data type chart helps you to learn different properties modified in data type
Type
Size
Format
specifier
Content  Type
Range
unsigned char
8 bits
%c
Character
0 to 255
Char
8 bits
%c
Character
-128 to 127
unsigned int
16 bits
%u
Integer
0 to 65,535
short int
16 bits
%d
Integer
-32,768 to 32,767
Int
16 bits
%d
Integer
-32,768 to 32,767
unsigned long
32 bits
%lu
Integer
0 to 4,294,967,295
Long
32 bits
%ld
Integer
-2,147,483,648 to 2,147,483,647
Float
32 bits
%f
Real
1.1754*(10^-38) to 3.4028* (10^+38)
Double
64 bits
%lf
Real
2.2250 * (10^-308) to 1.7976 * (10^+308)
long double
80 bits
%Lf
Real
3.4 * (10^-4932) to 1.1 * (10^4932)

Qualifier

const
l   const means that something is not modifiable 
l   const variable must be initialized during its declaration
volatile
l   The volatile keyword is intended to prevent the compiler from applying any optimizations on the code that assume values of variables cannot change "on their own."

Input / Output Instruction:

Output Instruction
User interaction with a program on machine required input/output device. Here, we preferably use keyboard as input device and monitor as output device.

printf()

printf() is a pre-defined function used to display messages on monitor.

Syntax:
printf(“format string”, variable list);

Format string is a message that you want to print on the screen.
Variable list is optional. We can also write expression in place of variable list.

Example:
 printf(“Saurabh Shukla”);
 printf(“Hello SCA”);

Write your first program

main()
{
   printf(“Hello ”);
}

Step 1: Save this program as first.c
Step 2: Compile it
Step 3: Run

Your program executes in a flash and you would not able to see its output.
Press ALT + F5 to see output window or modify your program as:

main()
{
   printf(“Hello  ”);
   getch();
}

When your program runs, first instruction prints a message Hello  .
Second instruction is a call to a function getch() whose job is to take one character from keyboard. Until you press any key this line wouldn’t finish its job. As call to getch() is the last line of the program, termination of program depends on when you enter a character. This let you see output window till you press any key.

You probably wonder why your older output is still on the screen every time you run your program. This is due to your consecutive execution of program using same output window. This problem can be solved by using clrscr() before printf().
Call to function clrscr() erase content of output screen.

Modified program:

main()
{
   clrscr();
   printf(“Hello  ”);
   getch();
}


Write a program to display values of two variables on the monitor. Declare these two variables of any type and assigned some values to them.

main()
{
   int a=3,b=56;
   clrscr();
   printf(“%d %d”,a,b);
   getch();
}

Output:
 3 56

In this example printf is use to display contents of variables a and b. Notice that %d is a special symbol called format specifier. They are used to specify format of data to be printed. In the format string %d will be replaced by values of the variables.

We can write expressions in place of variable list.
main()
{
   int a=3,b=56;
   clrscr();
   printf(“%d %d %d”,a,b, a+b);
   getch();
}

Output
 3 56 59
Notice that printf contains three format specifiers (%d) as there are three expressions in the variable list. First two are simply variables and third one is an expression which first solved then the result gets printed in the place of third %d.

Escape Sequences:
Escape sequences are special symbols prefixed with a back slash ( \ ). These symbols are used in printf to provide a formatted output.

Here is the list of escape sequences:
Escape Sequence
Name
Meaning
\a
Alert
Produces an audible or visible alert.
\b
Backspace
Moves the cursor back one position (non-destructive).
\f
Form Feed
Moves the cursor to the first position of the next page.
\n
New Line
Moves the cursor to the first position of the next line.
\r
Carriage Return
Moves the cursor to the first position of the current line.
\t
Horizontal Tab
Moves the cursor to the next horizontal tabular position.
\v
Vertical Tab
Moves the cursor to the next vertical tabular position.
\'

Produces a single quote.
\"

Produces a double quote.
\\

Produces a single backslash.
\0

Produces a null character.
\ddd

Defines one character by the octal digits (base-8 number). Multiple characters may be defined in the same escape sequence, but the value is implementation-specific.
\xdd

Defines one character by the hexadecimal digit (base-16 number).

Consider the following program:

main()
{
   int a=3,b=56;
   clrscr();
   printf(“%d\n%d”,a,b);
   getch();
}



Output:
3
56

Here output comes in two lines. Notice \n in the printf , first value (3) gets printed then \n moves cursor to the next line and then 56 gets printed.

Use list of different escape sequences in your program and observe the effect.

Input Instruction:

Default input device is keyboard. Input instruction makes possible to input data from keyboard.

scanf()

scanf() is a predefined function. It is used to input data from keyboard and stores in variables of the program.

Syntax:
scanf(“format specifier”, address of variable);

Example:
main()
{
   int a;
   scanf(“%d”,&a);
}

In this program, we have declared a variable a of type int. scanf makes possible one integer value through keyboard. scanf receives this value and stores it in variable a.

Here it is important to notice address of operator (&) before variable a. don’t forget to put this operator before variable name. We will understand this operator in great detail in later chapter.

Multiple input:

main()
{
   int a;
   float y;
   scanf(“%d%f”,&a,&y);
}

For float variable format specifier should be %f. Here we can input two values, first an integer and second one is float.

Write a program to add two numbers.

main()
{
   int a,b,c;
   clrscr();
   printf(“Enter two numbers”);
   scanf(“%d%d”,&a,&b);
   c=a+b;
   printf(“Sum is %d”,c);
   getch();
}

Output:
Enter two numbers5
6
Sum is 11

Now you can use scanf for user input. We will again study scanf in great detail during functions in later chapter.

No comments:

Post a Comment