C Programming Notes.pdf

Embed Size (px)

Citation preview

  • 8/14/2019 C Programming Notes.pdf

    1/10

    1

    1BASICS OF C LANGUAGE

    Introduction

    The C language was developed in early 1970s at AT&T labs by Brian Kernighan andDennis Ritchie. The language was originally developed to write the UNIX operatingsystem. 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 higherlevel 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 : a to z, A to Z

    Numbers : 0,1 to 9Special Symbols : + * / \ & | % ! ? [ ] { } > < = . _ : ; ^ ~ ' "White Space : space, horizontal tab, formfeed, newline, and carriage return

    Following table gives the list of symbols used in C

    Symbol Name Symbol Name

    + pl us : col on- mi nus ; semi col on

    * ast er i sk apost r ophe/ sl ash quot at i on mar k\ back sl ash ? quest i on mark| ver t i cal bar ! excl amat i on mar k^ car et & amper sand. per i od { l ef t br ace~ t i l de } r i ght br ace

    _ underscor e [ l ef t br i ght% per centage ] r i ght bracket$ dol l ar si gn ( l ef t par ent hesi s# number si gn ) r i ght par ent hesi s

    < l ess t han or openi ngangl e br acket > more t han or cl osi ngangl e br acket

    Escape Sequences

    The output of a C program may also use a combination of characters such as \b, \n, \t,

    etc. These combinations are known as escape sequences. The escape sequencerepresents a single character, even though it is written as a two or more character. The

    following table gives the escape sequence used in C

  • 8/14/2019 C Programming Notes.pdf

    2/10

    2

    Sequence Meaning Sequence Meaning

    \ a al er t sound \ v ver t i cal t ab\ b backspace \ si ngl e quot e\ f f or mf eed \ doubl e quot e\ f newline \ ? quest i on mark\ r car r i age r et ur n \ \ backsl ash\ t as ter i sk \ 0 nul l

    Keywords of C

    The C programming language keeps a small set of keywords for its own use. Thesekeywords cannot be used as identifiers in the program. The C language specifies 32keywords. Here is the list of keywords used in Standard C; you will notice that none of

    them use upper-case letters.

    aut o doubl e i nt st r uctbr eak el se l ong swi t chcase enum r egi st er t ypedefchar ext er n r et ur n uni on

    const f l oat shor t unsi gnedcont i nue f or si gned voi ddef aul t got o si zeof vol at i l edo i f stat i c 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 asis. So it cannot be used as Return, or RETURN.

    The keywords have special meaning in the language and cannot be used for any otherpurpose such as constant name or variable name.

    Identifiers in C

    An identifier is a word used in the program for any variable, function, data definition,etc. In the programming language C, an identifier is a combination of alphanumericcharacters

    Following rules must be kept in mind when naming identifiers.

    The first character must be a letter of the alphabet or an underline, and theremaining 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. Thususing "salary" for a variable is not the same as using "SALARY" and neither of themis same as using "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.

  • 8/14/2019 C Programming Notes.pdf

    3/10

    3

    In old C up to eight characters can be used for identifiers. If more than eight areused, they may be ignored by the compiler. In Standard C this limit has changed to31 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 character. The value ofthe 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 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 in

    variable names.

    A variable name is case sensitive. Thus BOY, boyand Boyare regarded as threedifferent variables in C.

    Some examples of valid variable names in C language are:

    area, r adi us, HEI GHT, due_dat e, Marks

    The following are invalid variable names. The reason why each name is invalid is alsogiven.

    Variable Reason why it is invalidbr eak#sal ar y

    due dat e2008mar ksnew. val ue

    keyword

    must begin with an alphabetic character

    contains a spacebegins with a numeric charactercontains a period

    Basic Data Types in CData in the C programming language are of two different types, namely numbers andcharacters. Each type of data requires different amount of memory.

    Numbers:These are further divided into two types: integerand real. A number without a

    decimal point is known as an integer (or a whole number). A number with decimal pointis known as a real number.

    C defines two basic types of integers: intand long.

    int:This requires 2 bytes of memory and can store 65536 different numbers. Therange of these numbers are from 32768 to +32767

    long:This requires 4 bytes of memory and can store 4294967295 differentnumbers. Thus it can store larger numbers. The range of these numbers are from

    2147483648 to +2147483647

  • 8/14/2019 C Programming Notes.pdf

    4/10

    4

    C defines two basic types of real numbers:floatand double.

    float:This requires 4 bytes of memory. It is used to store numbers. In the range3.4 1038 to +3.4 10+38

    double:This requires 8 bytes of memory. It is used to store numbers in the range1.7 10308 to 1.7 10+308. Thus it can store larger numbers.

    To represent individual characters, C uses the chartype. This requires only one byte ofmemory. Each represented character will have a corresponding integer value in therange 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 bythe 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 and range of various data types in C.

    Data type Memory Used Range

    intlongfloat

    doublechar

    2 bytes4 bytes4 bytes

    8 bytes1 byte

    32768 to +327672147483648 to +21474836473.4 1038 to +3.4 10+38

    1.7 10308 to 1.7 10+308value range 0 255

    Basic Constants in C

    There are four basic types of constant in C. They are: integer constant, floating-pointconstant, character constant and string constant. The first two represent numbers and

    are hence also known as the numeric constants.

    The following rules apply to numeric constants:

    A negative constant is preceded by a minus sign. For positive constant the plus signis 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.

  • 8/14/2019 C Programming Notes.pdf

    5/10

    5

    Integer Constant: A constant number without a decimal point is an integer constant.

    The following are examples of invalid numeric constant. The reason why each is invalidis also given.

    I n t eg erConstant Reason why it is invalid

    12,89098232_24

    3.1447 9

    contains a commamust not begin with zeromust not contain underscore (_)

    integerconstant cannot have a period(.)contains a blank space

    C allows a number to be written in three different number systems.

    Decimal:This is the base 10 number system. This system uses the digits from 0 to 9. Octal :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 areexamples of valid octal integerconstants.

    012 038 04 015 0122

    Hexadecimal:This is the base 16 number system. This system uses the digits 0 to 9and A, B, C, D, E, F. The letters A through F represent the decimal quantities 10through 15 respectively.A hexadecimal integerconstant must begin with a 0xor 0X. It can then be followedby sequence of valid digits. Following are examples of valid hexadecimal integerconstants.

    0x12 0x3f 9 0X4 0xABC 0xFFFF

    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 102

    .The mantissa must be real or integer. The exponent must be an integera minus oroptional plus sign. Following are examples of valid floating point constants.

    0. 33e- 3 12. 001 - 123456 25. 4E- 8 1. 232e+3

    Character Constant : Character constant is a single character enclosed in apostrophes

    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 withquotation marks i.e.double quotes. The following are examples of valid string constants.

    Ar vi ARVI ReD 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.

  • 8/14/2019 C Programming Notes.pdf

    6/10

    6

    Test Yourself:

    1. Identify the following as valid or invalid octal integerconstants?12 0319 0. 34 045 02

    2. Which of the following are invalid hexadecimal integerconstants?12 0x319 0x3. 4 0xCDG 0xabcd

    3. Why are the following numbers invalid floating point constants?$314 3, 19. 2 2e+1. 4 2e 24 56E+1, 4

    Variable DeclarationIn 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. Adeclaration statement must end with a semicolon

    The following lines declares four variables: a of type int , x of typefloat, m of type double

    and r of type char.

    i nt a;f l oat x;doubl e m;char r ;

    Two or more variables of 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 variablesdeclared 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. 1412;

    We can also assign values to variables at the same time when a variable is declared as

    follows:

    i nt age = 35;char code = A ;

  • 8/14/2019 C Programming Notes.pdf

    7/10

    7

    In these lines: ageis an integervariable 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 isused:

    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 and c as follows:

    a=b=c=55 ;

    Arithmetic Expressions

    In C, arithmetic expression is a combination of constants, variables and operatorsarranged as per the syntax of the language. Some examples of algebraic expression andC expression is given below:

    Algebraic Expression C Expression

    p+qr - 35 p+q*r - 355x +5y- 7 5*x*x+5*y- 7

    abc+

    abc +3d

    a/ ( b*c) +a*b/ c+3*d

    x( a+bc+d)

    a*( a+b/ c+d)

    x=3. 2x- y x=3. 2*x- y

    y=1x+

    1x2

    y=1/ x+1/ ( x*x)

    Evaluation of Arithmetic Expressions:

    The basic evaluation procedure is from the left to the right. An arithmetic expressionwithin parentheses is evaluated left-to-right using the rules of precedence of operators.

    There are two priority levels for arithmetic operators in C.

    High priority : * / % Low priority : +

    The evaluation is done in two left-to-right passes. During the first pass the high priority

    operators are applied. During the second pass the low priority operators are applied.

    Consider the expression: x = a + b/4 c*4 + 3 where a = 3, b=8, c =1.

    The expression for given values is : x = 3 + 8/ 4 1*4 + 3

    Evaluation is as follows:

    First Pass : x = 3 + 2 1*4 + 3 ( 8/ 4 eval uat ed)x = 3 + 2 4 + 3 ( 1*4 eval uated)

    Second Pass : x = 5 4 + 3 ( 3+2 eval uat ed)

  • 8/14/2019 C Programming Notes.pdf

    8/10

    8

    x = 1 + 3 ( 5- 4 eval uat ed)x = 4 ( 1+3 eval uat ed)

    The order of evaluation can be changed by using parenthesis. When an expression is

    written in parenthesis it gets highest priority. For example, in the expression : 5+6/ 31*( 4+3) , the expression 4+3is evaluated first.

    When parentheses are nested, the innermost parentheses have highest priority.

    For example :

    15- ( 16/ ( 2+2) *2) +315- ( ( 16/ 2) +2) *2) +3

    The first expression evaluates to 10, 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 areconnected by operators. Expression can also be a logical condition that is true or false.

    In C the true and false condition are represented by integervalue 1 and 0 respectively.

    Following are examples of expression :

    a+b- c ;x=a ;x>6 ;

    The first expression uses the arithmetic operators + and , the second expression usesthe 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/14/2019 C Programming Notes.pdf

    9/10

    9

    Some Simple C programs

    Program 1:A C program consists of one or more functions. In

    the example given below main() is such a function. All Cprograms must have a main() function.

    mai n( ){

    pr i nt f ( " hel l o, wor l d" ) ;}

    Execution of the above program begins at the first statement of main. The main function

    will usually invoke other functions to perform its job, some coming from the sameprogram, and others from libraries.

    printf is a library function which will format and print output on the terminal (unless

    some other destination is specified). In this case it prints the message hello, world.Theoutput is shown below.

    hel l o, wor l d

    Program 2: Here's a bigger program that adds three integers and prints their sum.

    mai n ( ){

    i nt a, b, c, sum;a = 1; b = 2; c = 3;

    sum = a + b + c;pr i nt f ( "sum i s %d", sum) ;

    }

    The output is shown below.

    sum i s 6

    Program 3: Here's a similar program that adds three floating point numbers and prints

    their sum.

    # i ncl ude mai n ( ){

    f l oat a, b, c, sum ;a = 1. 0 ; b = 2. 0 ; c = 3. 0;sum = a + b + c ;pr i nt f ( "sum i s %f ", sum) ;

    }

    Programs written in Clanguage have to becompiled and thenexecuted. There is nosingle compiler of C.

    These compilers differfrom one another.Even on the samecomputer there may

    be several compilers.

  • 8/14/2019 C Programming Notes.pdf

    10/10

    10

    The output is shown below.

    sum i s 6. 000000