Showing posts with label 1.INTRODUCTION TO C PROGRAMMING. Show all posts
Showing posts with label 1.INTRODUCTION TO C PROGRAMMING. Show all posts

Sunday, December 6, 2009

1.11 SYMBOLIC CONSTANTS

A symbolic constant is a name that substitutes for a sequence of characters.Their character may represent a numeric constant,a character constant or a string constant.

A symbolic constant is defined by writing

#define name text

where name represents a symbolic name and text represents the sequence of characters that is associated with the symbolic name.Note that text does not end with a semicolon.The #define feature is used to define symbolic constants.

e.g
#define PI 3.141593

area=PI*radius*radius;

1.10 STATEMENTS

A statement causes the computer to carry out some action. There are three different classes of statements in C. They are expression statements, compound statements and control statements.

Several statements are shown below

a=3;---> expression statement
c=a+b;---> expression statement
++i;--->expression statement
printf(“area=%f”,area);-->expression statement


{
pi=3.14;
circumference=2*pi*radius;
area=pi*radius*radius;

}

This is a compound statement, which consists of three assignment type expression statements. Note that the compound statement does not end with a semicolon.

The following control statement creates a conditional loop in which several actions are executed repeatedly, until some particular condition is satisfied.

while(count<=n) { printf(“x=”); scanf(“%f”, &x); sum=sum+x; ++count; } This control statement contains a compound statement, which in turn contains four expression statements. The compound statement will continue to be executed as long as the value of count does not exceed the value of n.Note that the count increase in value during each pass through the loop.

1.9 EXPRESSIONS

An expression represents a single data item, such as a number or a character. The expression may consist of a single entity, such as a constant, a variable, an array element or a reference to a function. It may consist of some combination of such entities, interconnected by one or more operators.

Expressions can also represent logical conditions that are either true or false.

Several simple expressions are shown below

a+b

x=y

x= =y

++i

The first expression involves use of addition operator (+). This expression represents the sum of the values assigned to the variables a and b.

The second expression involves the assignment operator (=). In this case, the expression causes the value represented by y to be assigned to x.

The third expression is a test for equality. Thus the expression will have the value 1 (true) if the value of x is equal to y. Otherwise the expression will have the value 0 (false).

The last expression causes the value of the variable I to be increased by 1 (i.e. incremented). Thus the expression is equivalent to

i=i+1

The operator ++ is called a unary operator because it has only one operand i.e. i

1.8 DECLARATIONS

A declaration associates a group of variables with a specific data type.All variables must be declared before they can appear in executable statements.

A C program contains the following type of declarations.

int a,b,c;
float root1,root2;
char flag,text[80];

Thus,a,b and c are declared to be integer variables,root1 and root2 are floating point variables,flag is a char type variable and text is an 80 element char type array.

Saturday, December 5, 2009

1.7 VARIABLES AND ARRAYS

A variable is an identifier that is used to represent some specified type of information within a designated portion of the program.In the simplest form, a variable is an identifier that is used to represent a single data item i.e a numerical quantity or a character constant.

A C program contains the following lines.

int a,b,c;
char d;
..........
a=3;
b=5;
c=a+b;
d='a';
.........
a=4;
b=2;
c=a-b;
d='w';

The first two lines are type declarations,which state that a,b, and c are integer variables,and d is a character type variable.

The next four lines cause the following things to happen: the integer quantity 3 is assigned to a,5 is assigned to b,and the quantity represented by the sum a+b (i.e 8) is assigned to c.The character 'a' is assigned to d.

The last four lines redefine the values assigned to the variables .

Array is another kind of variable that is used extensively in C.An array is an identifier that refers to a collection of data items that all have the same name.The data items must all be of the same type (e.g all integers,all characters,etc.).The individual data items are represented by their corresponding array elements.

Suppose letter[9] is a 10-element array.The first element is referred to as letter[0],the second element as letter[1] and so on.

1.6 CONSTANTS

There are four basic type of constants in C.They are integer constants,floating point constants,character constants and string constants.

The following rules apply to all numeric type constants.

1.Commas and blank spaces cannot be included within the constant.
2.The constant can be preceded by a minus (-) sign if desired.
3.The value of a constant cannot exceed specified minimum and maximum bounds.

Example of valid constants are:

0,5280,999-->integer constants
1.,827.65,2E-8,.1234e12-->floating point constants
'a','X','?','7'-->character constants
"yellow","20.05","washington,dc 2006"-->string constants

invalid constants:

12,345-->illegal character (,)
30 40 50-->illegal character (blank space)
145-654-552-->illegal character (-)
0354-->first digit cannot be zero

1.5 DATATYPES

C supports several different types of data,each of which may be represented differently within computer's memory.The basic data types are listed below.

DATA TYPE------------ DESCRIPTION ----TYPICAL MEMORY REQUIREMENTS

int----------------------- integer quantity---- 2 bytes or one word (varies from compiler to compiler)
char --------------------- single character-------------------------- ----------- 1 byte

float---------------------- a no. containing a decimal----------------- 1 word (4 bytes)
---------------------------point or an exponent

double-------------------- double precision floating point no. --------2 words (8 bytes)

1.4 SOME USEFUL C PROGRAMS

Let us take a program which adds two nos. and shows the output on the screen.

#include

main()
{

int sum;
sum=10+20;
printf("%d\n",sum);

}

This program stores the result of addition of two nos. 10 and 20 into a variable sum.Here sum has been declared of int data type which means possible values of the sum are integers.

Fifth line of the above program assigns the addition of 10 & 2o to a variable sum.Equal to (=) acts as an assignment operator that assigns the value of right hand expression to left hand variable.

The printf statement will print 30 as output on the output screen.The reason of this output is use of "%d" within double quotes and the name of corresponding variable is specified after the comma.

The above written program adds 10 and 20. If we want to add 40 and 50,we have to change the program.If we want to add any two nos. based on user's choice,then the above program can be modified as

#include

main()
{
int num1,num2,sum;
printf("enter two integer values to be added");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("Addition of %d and %d=%d\n",num1,num2,sum);

}


A new instruction scanf has been used in this program, which is used to take inputs from the user.

The output of the program will be as follows :


enter two integer values to be added 30 20
Addition of 30 and 20=50
_

1.3 IDENTIFIER AND KEYWORDS

Identifiers are names given to various program elements,such as variables,functions and arrays.Identifiers consist of letters and digits,in any order,except that the first character must be a letter.Both uppercase and lowercase letters are permitted.The underscore character(_) can also be included.An identifier may also begin with an underscore,though tjis is rarely done in practice.

The following names are valid identifiers

x y12 sum_1 _temp TABLE


There are certain reserved words,called keywords,that have standard,predefined meanings in C.
These keywords can be used only for their intended purpose;they cannot be used as programmer defined identifiers.

These standard keywords are

auto extern sizeof break float static case for struct char goto

switch const if typedef continue int union default long unsigned

do register void double return volatile else short while enum signed

Note that the keywords are all lowercase.

1.2 WRITING THE FIRST PROGRAM OF C

We will start writing a very simple C program and learn the instructions of a C program through it.

ex1.1

#include

main()
{

printf ("welcome to c programming\n")

}


This is a very simple program used to print the message "welcome to c programming" on the output screen.It gives an idea of the the structure of a C program.

#-->used as a compiler derivative
-->standard input/output library
main-->starting point of executing of a program
{,}-->braces are used to start and end every block of a program
printf-->an output statement used to display any message on the screen

1.1 THE C CHARACTER SET

C uses the uppercase letters A to Z, the lowercase letters a to z, the digits 0 to 9 ,certain special characters (all characters that are present in your keyboard) as building blocks to form basic program elements.

C uses certain combinations of these charcters such as \b,\n and \t, to represent special conditions such as backspace,newline and horizontal tab respectively.These character sequences are known as escape sequences.