1 BASICS OF C LANGUAGE Introduction The C language was developed in early 1970s 1970s at AT&T labs by Brian Brian Kernighan and Dennis Ritchie. The language la nguage was originally developed to write the UNIX operating system. Since that time, literally thousands of applications have been written in C C is a small language with relatively few commands. In order to make programming in C easier, a number of libraries have been written. These libraries allow you to use higher level commands or functions which make it easier to write a program.
Character Set of C C programs are written using a combination of alphanumeric characters, special symbols and white space characters. The following characters are used in C language. Alphabets Numbers Special Symbols White Space
: : : :
a to z, A to Z 0,1 to 9 +– * / \& |% ! ? [] {} > <= . _ :; ^~ '" space, horizontal tab, formfeed, newline, and c arriage return
Following table gives the list of symbols used in C Symbol
+ * / \ | ^ . ~ _ % $ # <
Name
pl us mi nus nus ast er i sk sl ash back sl ash ver t i cal cal bar c ar et per i od t i l de under under sc or e per c ent ag a ge dol l ar si gn number si gn l ess t han or ope openi ng angl angl e br acke ackett
Symbol
: ; ‘ “ ? ! & { } [ ] ( ) >
Name
col col on semi col on apost r ophe quot at i on mar k quest i on mar k excl amat i on mar k amper s and l ef t br ace r i ght br ace l ef t br i ght r i ght br ac ac ket l ef t par ent hesi s r i ght par ent ent hesi s mor e t han han or cl osi ng ang angl e br acke acket
Escape Sequences The output of a C program program may also use a combination of of characters such as \b, \n, \t, etc. These combinations are known as escape sequences. The escape sequence represents a single character, even though it is written as a two or more character. The following table gives the escape sequence used in C 1
Sequence
Meaning
\a \b \f \f \r \t
Sequence
al er t sou sound backspa ckspace f or mf eed newline
ca car r i age r et ur n as t er er i s k
\v \’ \” \? \\ \0
Meaning
ver t i cal cal t ab si ngl e quot e doubl e quot e quest i on mar k ba backsl cksl ash nul l
Keywords of C The C programming language keeps a small set of keywords keywords for its own use. use. These keywords cannot be used as identifiers in the program. The C language s pecifies 32 keywords. Here is the list li st of keywords used in Standard C; you will notice that none of them use upper-case letters.
aut o br eak eak case case char char con const con cont i nue def aul t do
doubl e el se enum ext er n f l oat f or got o if
i nt l ong r egi st er r et ur n sho shor t si gned s i z eo eof stati c
st r uct swi t ch t ype ypedef uni on unsi gned voi d vo vol at i l e whi l e
Following rules must be kept in mind when using keywords.
Keywords are case sensitive. For example, return is a keyword and it must be used as is. So it cannot be used as Return, or RETURN. The keywords have special meaning in the language and cannot be be used for any other purpose such as constant name or variable name.
Identifiers in C An identifier is a word used in the program for any variab le, function, data definition, etc. In the programming language C, an identifier is a combination of alphanumeric characters Following rules must be kept in mind when naming identifiers.
The first character must be a letter letter of the alphabet or an underline, underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline. Both upper and lower case letters can be used. Identifiers are case sensitive. Thus using "salary "salary"" for a variable is not the same a s using "SALARY " SALARY"" and neither of them is same as using "saLAry "saLAry"" for a variable. All three refer to different variables. An identifier can use an underscore sign. For example the identifier " student_name " contains the underscore.
2
In old C up to eight characters can be used for identifiers. If more than eight are used, they may be ignored by the compiler. In Standard C this limit has changed to 31 characters. In practice the actual limit will depend on the C compiler.
Variable Names in C A variable is a identifier used to represent a numerical value or a cha racter. The value of the variable can change during the execution of the program. Following rules must be kept in mind when using variables:
Keywords of C language cannot be used as variable names. A variable name must start with an alphabet or the underscore (_) character. The digits 0 – 9 cannot be used to start the variable name. The first character can be followed followed by a sequence of letters and /or digits. A variable name can be a maximum of 8 characters in length. No special characters such as comma, space period, semicolon (;) are permitted permitted in variable names. A variable name is case sensitive. se nsitive. Thus BOY , boy and and Boy are regarded as three are different variables in C.
Some examples of valid variable names in C language are:
ar ea, ea, r adi adi us, HEI GHT, due_dat _dat e, Mar ks The following are invalid variable names. The The reason why each name is invalid is also given. Variable
br eak eak #s al al ar y due due dat dat e 2008mar ks new. val val ue
Reason why it is invalid keyword must begin with an alphabetic character contains a space begins with a numeric character contains a period
Basic Data Types in C Data in the C programming language are of two different types, namely numbers and characters . Each type of data requires different amount of memory. two types: integer and and real . A number without a Numbers: These Numbers: These are further divided into two decimal point is known as an integer (or a whole number). A number with decimal point is known as a real number. C defines two basic types of integers: int and and long .
int: This int: This requires 2 b ytes of memory and can store 65536 different numbers. The range of these num numbers bers are from – 32768 to +32767 long: This long: This requires 4 bytes of memory and can store 4294967295 different numbers. Thus it can store larger numbers. The range of these numbers are from –2147483648 to +2147483647 +2147483647 3
C defines two basic types of real numbers: float and and double .
float: This float: This requires 4 bytes of memory. It is used to store numbers. In the range 3.4 × 10 –38 to +3.4 × 10 +38 double: This double: This requires 8 bytes of memory. It is used to store numbers in the range 1.7× 10 –308 to 1.7× 10 +308 . Thus it can store larger numbers.
To represent individual characters, C uses uses the char type. char type. This requires only one byte of memory. Each represented character will have a corresponding integer value in the range 0 – 255. Thus a char is a special type of integer. Most machines today use the ASCII character set, in which the letter A is represented by the integer code 65, the digit 1 is represented by integer code 49, the space character is represented by the integer code 32, etc. The following table gives the memory requirement requirement and range of various data types types in C. Data type int long float double char
Memory Used 2 bytes 4 bytes 4 bytes 8 bytes 1 byte
Range – 32768 to +32767 –2147483648 to +2147483647 +2147483647 –38 3.4 × 10 to +3.4 × 10 +38 1.7× 10 –308 to 1.7× 10 +308 value range 0 – 255
Basic Constants in C There are four basic types of constant constant in C. They are: integer integer constant, floating-point constant, character constant and string s tring constant. The first two represent numbers and are hence also known as the numeric constants.
The following rules apply to numeric numeric constants:
A negative constant is preceded by a minus sign. For positive constant the plus sign is optional. blank space and comma is not permitted within a constant. the value of constant must be within the specified range. If the number has two or more digits, the first digit must not be 0.
4
c onstant. Integer Constant: A constant number without a decimal point is an integer constant. The following are examples of invalid numeric numeric constant. The reason why each is invalid is also given. I n t eg er Constant
12,89 0982 32_24 3.14 47 9
Reason why it is invalid contains a comma must not begin with zero must not contain underscore (_) integer constant constant cannot have a period(.) contains a blank space
C allows a number numbe r to be written in three different number sy stems.
system uses the digits from 0 to 9. Decimal: This Decimal: This is the base 10 number system. This system first Octal : This : This is the base 8 number system. This system uses the digits 0 to 7. The first digit must be zero (0) in order to identify it as an octal number. Following are examples of valid octal integer constants. constants.
012
038
04
015
0122
number system. This system uses the digits 0 to 9 Hexadecimal: This Hexadecimal: This is the base 16 number and A, B, C, D, E, F. The letters A through F represent the decimal quantities 10 through 15 respectively. A hexadecimal integer constant constant must begin with a 0x or 0x or 0X 0X.. It can then be followed by sequence of valid digits. Following are examples of valid hexadecimal integer constants.
0x12
0x3f 9
0X4 0X4
0xAB 0xABC
0xFFFF 0xFF FF
Floating Point Constant : A constant number with decimal is a floating point number. Such numbers may also be expressed in exponential (scientific) form. For example the value 356.3 can be written as 3.563e2 where e2 means multiply by 10 2. The mantissa must be real or integer . The exponent must be an integer a a minus or optional plus sign. Following are e xamples of valid floating point constants.
0. 33e33e- 3
12. 12. 001 001
- 1234 123456 56
25. 25. 4E4E- 8
1. 232e 232e+ +3
Character Constant : Character constant is a single character enclosed in apostrophes i.e. single i.e. single quotation marks. The following are examples of valid character constants.
‘ A’
‘ 5’
‘ #’
‘ m’
‘ ’
In above the last constant has a blank space enclosed in apostrophes. String Constant : String constant is a combination of valid character enclosed with quotation marks i.e. double i.e. double quotes. The following are examples of valid string constants.
“ Ar vi ”
“ ARVI ”
“ ReD”
“12345 “12345” ”
“ 3+x”
A string constant can contain the newline character sequence \n. For example ‘ I \ n am\ n happy’ is a valid string constant.
5
Test Yourself: 1. Identify the following as valid vali d or invalid octal integer constants? constants?
12
0319 0319
0. 34
045
02
2. Which of the following are invalid i nvalid hexadecimal integer constants? constants?
12
0x319
0x3. 4
0xCDG
0xabcd 0xabc d
3. Why are the following numbers invalid floating point constants?
$314
3, 19. 2
2e+ 2e+1. 4
2e 2e 24
56E+1, 4
Variable Declaration In general a C program will make use of variables to store data. All variables used in a program must be declared with specific data type. The syntax to declare a variable is:
dat at ype v1, v2, . . . , vn ; where v1, v2,... vn are name of variables. The variables are separated by comma. A declaration statement must end with a semicolon The following lines declares four variables: variables: a of type int , x of type float type float , m of type double and r of type char .
i nt a; a; f l oat x; x; doub doubll e m; c ha har r ; Two or more variables of the the same type can be declared in one line.
i nt a, b, c ; f l oat x , y, wei ght ;
Assignment A variable can be assigned value after declaration. The example shows three variables declared of type integer . The variables are assigned values later in the lines that follow.
i nt a, b, pi ; .. .. .. .. .. .. a = 10; b = 20; pi = 3. 141 1412; We can also assign values v alues to variables at the same time when a variable is declared as follows:
i nt age age = 35; char char cod code = ‘ A’ ; 6
In these lines: age is is an integer variable variable given initial value 35, and code is a char type variable given the initial value ‘A’ C also supports multiple assignments. For multiple assignments the following syntax is used:
i dent i f i er 1 = i dent i f i er
2 = . . . . = expr essi on;
For example the value 55 is assigned to variables a, b a nd c as follows:
a=b=c =55 ;
Arithmetic Expressions In C, arithmetic expression is a combination of constants, variables and operators arranged as per the the syntax of the the language. Some examples of algebraic algebraic expression and C expression is given below: Algebraic Expression
C Expression
p+qr - 35 5x +5y5y- 7 a ab bc+ c +3d b x( a+c +d) x=3. 2x2x- y 1 1 y=x+x2
p+q*r - 35 5* 5* x*x+5*y- 7 a/ ( b*c) +a*b/ a*b/ c+3*d a*( a+b/ c+d) x=3. 2* x- y y=1/ x+1/ ( x*x)
Evaluation of Arithmetic Expressions: The basic evaluation procedure is from the the left to the right. An arithmetic arithmetic expression within parentheses is evaluated eva luated left-to-right using the rules of precedence of operators. There are two priority levels levels for arithmetic operators in C.
High priority : * / % Low priority : + –
The evaluation is done in two lleft-to-right eft-to-right passes. During the first pass the the high priority operators are applied. During the second pass the low priority operators are applied. Consider the expression: x = a + b/4 b/4 – c*4 + 3 where where a = 3, b=8, c =1. The expression for given values is : x = 3 + 8/ 4 – 1*4 1* 4 + 3 Evaluation is as follows: First Pass Second Pass
: x = 3 + 2 – 1* 4 + 3 x =3 +2 – 4 +3 : x =5 – 4 +3
( 8/ 4 eval eval uat uat ed) ed) ( 1*4 1* 4 eval uat ed) ( 3+2 eval uat uat ed) ed) 7
x =1 +3 x =4
( 5- 4 eval eval uat uat ed) ed) ( 1+3 eval uat uat ed) ed)
The order of evaluation can be be changed by using parenthesis. When an expression expression is written in parenthesis it gets highest priority. For example, in the expression : 5 +6/ 3– 1*( 1* ( 4+3) , the expression 4+3 is evaluated first. When parentheses are nested, the innermost parentheses have highest priority. For example :
15- ( 16/ ( 2+2) * 2) +3 15- ( ( 16/ 2) +2) *2) +3 The first expression evaluates to 10, whereas whereas the second expression evaluates to –2. Parentheses can also be used to improve the readability of the program.
Expressions Expression is a single value or a combination of values and variables which are connected by operators. Expression can also be a logical condition that is true or false. In C the true and false condition are represented by integer value value 1 and 0 respectively. Following are examples of expression :
a+b- c ; x=a ; x>6 ; The first expression uses the arithmetic operators operators + and – , the second expression uses the assignment operator =, the third expression uses the relational operator >. These operators are discussed later.
Questions: 1. State four rules for choosing a variable name. 2. What are escape sequences? Give three examples.
8
Some Simple C programs Program 1: A 1: A C program consists of one or more functions. In the example given below main() is such a function. All C programs must have a main() function.
mai n( ) { pr i nt f ( " hel l o, wor l d" ) ; }
Programs written in C language have to be compiled and then executed. There is no single compiler of C. These compilers compilers differ from one another. Even on the same computer there may be several compilers.
Execution of the above program begins at the first statement of m ain. The main function will usually invoke other functions to perform its job, some coming from the same program, and others from libraries. printf is a library function which will fo rmat and print output on the terminal ( unless some other destination is specified). In this case it prints the message ‘ hello, ‘ hello, world’. The world’. The output is shown below.
hel l o, wor l d
p rints their sum. Program 2: Here's a bigger program that adds three integers and prints
mai n ( ) { i nt a, b, c, sum sum; a = 1; b = 2; c = 3; s um = a + b + c ; pr i nt f ( "sum "sum i s %d", sum sum) ; } The output is shown below. below.
sum su mi s 6
s imilar program that adds three floating point numbers and prints Program 3: Here's a similar their sum.
# i ncl ude mai n ( ) { f l oat a, b, c , s um ; a = 1. 0 ; b = 2. 0 ; c = 3. 0; s um = a + b + c ; pr i nt f ( " s um um i s %f " , s um um) ; } 9
The output is shown below. below.
sum su m i s 6. 6. 000000
10