CPG Course File Shiplu

Embed Size (px)

Citation preview

  • 8/19/2019 CPG Course File Shiplu

    1/146

    P a g e  | 1

    Overview to C ProgrammingObjectives

    This section is designed to give you a general overview of the C programming language.

    Although much of this section will be expanded in later sections it gives you a taste of what isto come.

    Why use C?C has been used successfully for every type of programming problem imaginable from

    operating systems to spreadsheets to expert systems - and efficient compilers are available for 

    machines ranging in power from the Apple Macintosh to the Cray supercomputers. The

    largest measure of Cs success seems to be based on purely practical considerations!

    ". the portability of the compiler#

    $. the standard library concept#

    %. a powerful and varied repertoire of operators#

    &. an elegant syntax#

    '. ready access to the hardware when needed#

    (. and the ease with which applications can be optimi)ed by hand-coding isolated procedures

    C is often called a *Middle +evel* programming language. This is not a reflection on its lac,

    of programming power but more a reflection on its capability to access the systems low level

    functions. Most high-level languages e.g. /0T0A12 provides everything the programmer

    might want to do already built into the language. A low level language e.g. assembler2

     provides nothing other than access to the machines basic instruction set. A middle level

    language3 such as C3 probably doesnt supply all the constructs found in high-languages - but

    it provides you with all the building bloc,s that you will need to produce the results you

    want4

    Uses of CC was initially used for system development wor,3 in particular the programs that ma,e-up

    the operating system. 5hy use C6 Mainly because it produces code that runs nearly as fast as

    code written in assembly language. 7ome examples of the use of C might be!

    ". /perating 7ystems

    $. +anguage Compilers

    %. Assemblers

    &. Text 8ditors

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    2/146

    P a g e  | 2

    '. Print 7poolers

    (. 1etwor, 9rivers

    :. Modern Programs

    ;. 9ata nterpreters

    "?. @tilities

    >n recent years C has been used as a general-purpose language because of its popularity with

     programmers. >t is not the worlds easiest language to learn and you will certainly benefit if

    you are not learning C as your first programming language4 C is trendy > nearly said sexy2 -

    many well established programmers are switching to C for all sorts of reasons3 but mainly

     because of the portability that writing standard C programs can offer.

    A Brief History of CC is a general-purpose language which has been closely associated with the @1> operating

    system for which it was developed - since the system and most of the programs that run it are

    written in C.

    Many of the important ideas of C stem from the language nstitute A17>2 established a committee to

     provide a modern3 comprehensive definition of C. The resulting definition3 the A17>

    standard3 or *A17> C*3 was completed late "=;;.

    C for Personal Computers5ith regards to personal computers Microsoft C for >

  • 8/19/2019 CPG Course File Shiplu

    3/146

  • 8/19/2019 CPG Course File Shiplu

    4/146

    P a g e  | 4

    The Eit!Compile!"in#!E$e%ute Pro%ess9eveloping a program in a compiled language such as C reEuires at least four steps!

    ".   editing or writing2 the program

    $.   compiling it

    %.   linking it

    &.   executing it

    5e will now cover each step separately.

    Editing

    Fou write a computer program with words and symbols that are understandable to human beings. This is the editing  part of the development cycle. Fou type the program directly into a

    window on the screen and save the resulting text as a separate file. This is often referred to as

    the source file you can read it with the TYPE command in DO or the c!t command in

    unix2. The custom is that the text of a C program is stored in a file with the extension .c for C

     programming language

    Compiling

    Fou cannot directly execute the source file. To run on any computer system3 the source file

    must be translated into binary numbers understandable to the computers Central Processing@nit for example3 the ;?G;: microprocessor2. This process produces an intermediate obHect

    file - with the extension .obH3 the .obH stands for /bHect.

    Linking

    The first Euestion that comes to most peoples minds is Why is linking necessary? The main

    reason is that many compiled languages come with library routines which can be added to

    your program. Theses routines are written by the manufacturer of the compiler to perform a

    variety of tas,s3 from inputIoutput to complicated mathematical functions. >n the case of C

    the standard input and output functions are contained in a library stdio.h2 so even the most basic program will reEuire a library function. After lin,ing the file extension is .exe which

    are executable files.

    Execut!ble #iles

    Thus the text editor produces .c source files3 which go to the compiler3 which produces .obH

    obHect files3 which go to the lin,er3 which produces .exe executable file. Fou can then run

    .exe files as you can other applications3 simply by typing their names at the DO prompt or

    run using windows menu.

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    5/146

    P a g e  | $

    Using &i%rosoft CEdit st!ge%

    Type program in using one of the Microsoft 5indows editing pac,ages.

    Compile !nd link%

    7elect Building from &!ke menu. Building option allows you to both compile and link  in

    the same option.

    Execute%

    @se the 'un menu and select (o option.

    E))o)s%

    irst error highlighted. @se *ext E))o) from e!)c+ menu for further errors if applicable.

    >f you get an error message3 or you find that the program doesnt wor, when you finally run it

    at least not in the way you anticipated2 you will have to go bac, to the source file - the .c file

    - to ma,e changes and go through the whole development process again4

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    6/146

    P a g e  | ,

    Uni$ systems/n all @nix systems further help on the C compiler can be obtained from the on-line manual.

    Type

    m!n cc

    on your local @nix system for more information.

    Please note that @nix is a case sensitive operating system and files named firstprog.c and

    >07TP0/J.c are treated as two se parate files on these system. n this case the program is run

    as follows!

    !-out

    To change the name of the executable file type!

    cc .o #i)stp)og #i)stp)og-c

    This produces an executable file called firstprog which is run as follows!

    #i)stp)og

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    7/146

    P a g e  | /

    'tru%ture of C ProgramsObjectives

    Daving completed this section you should ,now about!

    ". Cs character set

    $. Cs ,eywords

    %. the general structure of a C program

    &. that all C statement must end in a #

    '. that C is a free format language

    (. all C programs us header files that contain standard library functions.

    C(s Chara%ter 'etC does not use3 nor reEuires the use of3 every character found on a modern computer

    ,eyboard. The only characters reEuired by the C Programming +anguage are as follows!

    ".   0 .

    $.   ! .

    %.   .

    &.   sp!ce - 5 % 6 7 8 9

    '.   : ; < = > ?@ A 8888

  • 8/19/2019 CPG Course File Shiplu

    8/146

    P a g e  | J

    upper and lowercase text to mean different things. >f you are not sure what to use then always

    use lowercase text in writing your C programs. A ,eyword may not be used for any other

     purposes. or example3 you cannot have a variable called !uto.

    The layout of C ProgramsThe general form of a C program is as follows dont worry about what everything means atthe moment - things will be explained later2!

     pre-processor directives

    global declarations

    main2

    K

    local variables to function main #

    statements associated with function main #

    L

    f"2

    K

    local variables to function " #

    statements associated with function " #

    L

    f$2

    K

    local variables to function f$ #

    statements associated with function $ #

    L

    .

    .

    .

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    9/146

    P a g e  |

    etc

     1ote the use of the brac,et set 2 and KL. 2 are used in conHunction with function names

    whereas KL are used as to delimit the C statements that are associated with that function. Also

    note the semicolon - yes it is there3 but you might have missed it4 a semicolon #2 is used to

    terminate C statements. C is a free format language and long statements can be continued3

    without truncation3 onto the next line. The semicolon informs the C compiler that the end of

    the statement has been reached. ree format also means that you can add as many spaces as

    you li,e to improve the loo, of your programs.

    A very common mista,e made by everyone3 who is new to the C programming language3 is to

    miss off the semicolon. The C compiler will concatenate the various lines of the program

    together and then tries to understand them - which it will not be able to do. The error message

     produced by the compiler will relate to a line of you program which could be some distance

    from the initial mista,e.

    Pre!pro%essor )ire%tivesC is a small language but provides the programmer with all the tools to be able to write

     powerful programs. 7ome people dont li,e C because it is too primitive4 +oo, again at the

    set of ,eywords that comprises the C language and see if you can find a command that allows

    you to print to the computers screen the result of3 say3 a simple calculation. 9ont loo, too

    hard because it doesnt exist.

    >t would be very tedious3 for all of us3 if every time we wanted to communicate with the

    computer we all had to write our own output functions. ortunately3 we do not have to. C useslibraries of standard functions which are included when we build our programs. or the

    novice C programmer one of the many Euestions always as,ed is does a function already

    exist for what I want to do? /nly experience will help here but we do include a function

    listing as part of this course.

    All programs you will write will need to communicate to the outside world - > dont thin, >

    can thin, of a program that doesnt need to tell someone an answer. 7o all our C programs

    will need at least one of Cs standard libraries which deals with standard inputting and

    outputting of data. This library is called stdin-+ and it is declared in our programs before the

    m!in function. The .h extension indicates that this is a header file.

    > have already mentioned that C is a free format language and that you can layout your

     programs how you want to using as much white space as you li,e. The only exception are

    statements associated with the pre-processor.

    All pre-processor directives begin with a and the must start in the first column. The

    commonest directive to all C programs is!

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    10/146

    P a g e  | 1

     1ote the use of the angle brac,ets NNNN and 2 around the headers name. These

    indicate that the header file is to be loo,ed for on the system dis, which stores the rest of the

    C program application. 7ome text boo,s will show the above statement as follows!

    :include 9stdio-+9

    The double Euotes indicate that the current wor,ing directory should be searched for the

    reEuired header file. This will be true when you write your own header files but the standard

    header files should always have the angle brac,ets around them.

    *OTE% Hust to ,eep you on your toes - pre-processor statements3 such as include3 9/ 1/T

    use semi-colons as delimiters4

  • 8/19/2019 CPG Course File Shiplu

    11/146

    P a g e  | 11

    The first line is the standard start for all C programs - m!in. After this comes the programs

    only instruction enclosed in curly brac,ets ?@. The curly brac,ets mar, the start and end of

    the list of instructions that ma,e up the program - in this case Hust one instruction.

     1otice the semicolon mar,ing the end of the instruction. Fou might as well get into the habit

    of ending every C instruction with a semicolon - it will save you a lot of trouble4 Also notice

    that the semicolon mar,s the end of an instruction - it isnt a separator as is the custom in

    other languages.

    >f youre pu))led about why the curly brac,ets are on separate lines >d better tell you that its

     Hust a layout convention to help you spot matching brac,ets. C is very unfussy about the way

    you lay it out. or example3 you could enter the Kello o)ld program as!

    m!in?p)int#9Kello o)ldMn96@

     but this is unusual.

    The p)int#  function does what its name suggest it does! it prints3 on the screen3 whatever you

    tell it to. The 9Mn9 is a special symbols that forces a new line on the screen.

    /B3 thats enough explanation of our first program4 Type it in and save it as Kello-c. Then

    use the compiler to compile it3 then the lin,er to lin, it and finally run it. The output is as

    follows!

    Kello o)ld

    A Comments to a ProgramA comment is a note to yourself or others2 that you put into your source code. All comments

    are ignored by the compiler. They exist solely for your benefit. Comments are used primarily

    to document the meaning and purpose of your source code3 so that you can remember later

    how it functions and how to use it. Fou can also use a comment to temporarily remove a line

    of code. 7imply surround the lines2 with the comment symbols.

    >n C3 the start of a comment is signalled by the GH character pair. A comment is ended by HG.

    or example3 this is a syntactically correct C comment!

    GH T+is is ! comment- HG

    Comments can extend over several lines and can go anywhere except in the middle of any C

    ,eyword3 function name or variable name. >n C you cant have one comment within another

    comment. That is comments may not be nested. +ets now loo, at our first program one last

    time but this time with comments!

    m!in GH m!in #unction +e!ding HG

    ?

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    12/146

    P a g e  | 12

    p)int#9Mn Kello5 o)ld= Mn96 GH Displ!N mess!ge on HG

    @ GH t+e sc)een HG

    This program is not large enough to warrant comment statements but the principle is still the

    same.

    )ata TypesObjectives

    Daving read this section you should be able to!

    ". declare name2 a local variable as being one of Cs five data types

    $. initialise local variables

    %. perform simple arithmetic using local variables

     1ow we have to start loo,ing into the details of the C language. Dow easy you find the rest of 

    this section will depend on whether you have ever programmed before - no matter what the

    language was. There are a great many ideas common to programming in any language and C

    is no exception to this rule.

    7o if you havent programmed before3 you need to ta,e the rest of this section slowly and

    ,eep going over it until it ma,es sense. >f3 on the other hand3 you have programmed before

    youll be wondering what all the fuss is about >ts a lot li,e being able to ride a bi,e4

    The first thing you need to ,now is that you can create variables to store values in. A variable

    is Hust a named area of storage that can hold a single value numeric or character2. C is very

    fussy about how you create variables and what you store in them. >t demands that you declare

    the name of each variable that you are going to use and its type3 or class3 before you actually

    try to do anything with it.

    >n this section we are only going to be discussing local  variables. These are variables that are

    used within the current program unit or function2 in a later section we will loo,ing at global  

    variables - variables that are available to all the programs functions.

    There are five basic data types associated with variables!

    ".   int - integer! a whole number.

    $.   #lo!t - floating point value! ie a number with a fractional part.

    %.   double - a double-precision floating point value.

    &.   c+!) - a single character.

    '.   void - valueless special purpose type which we will examine closely in later sections.

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    13/146

    P a g e  | 13

    /ne of the confusing things about the C language is that the range of values and the amount

    of storage that each of these types ta,es is not defined. This is because in each case the

    natural choice is made for each type of machine. Fou can call variables what you li,e3

    although it helps if you give them sensible names that give you a hint of what theyre being

    used for - names li,e sum3 total3 average and so on. >f you are translating a formula then use

    variable names that reflect the elements used in the formula. or example3 $r that should

    read as *$ pi r* but that depends upon how your browser has been set-up2 would give local

    variables names of pi and ). 0emember3 C programmers tend to prefer short names4

    *ote%all C's variables must begin with a letter or a "_" underscore! character .

    ,nteger -um.er /aria.lesThe first type of variable we need to ,now about is of class type int - short for integer. An int 

    variable can store a value in the range -%$:(; to O%$:(:. Fou can thin, of it as a largish

     positive or negative whole number! no fractional part is allowed. To declare an int you usethe instruction!

    intvariable name6

    or example!

    int !6

    declares that you want to create an int variable called !.

    To assign a value to our integer variable we would use the following C statement!

    !I16

    The C programming language uses the ** character for assignment . A statement of the form

    !I16 should be interpreted as take the numerical value # and store it in a memory location

    associated with the integer variable a. The ** character should not be seen as an eEuality

    otherwise writing statements of the form!

    !I!F16

    will get mathematicians blowing fuses4 This statement should be interpreted as take the

    current value stored in a memory location associated with the integer variable a$ add the

    numerical value # to it and then replace this value in the memory location associated with a.

    )e%imal -um.er /aria.lesAs described above3 an integer variable has no fractional part. >nteger variables tend to be

    used for counting3 whereas real  numbers are used in arithmetic. C uses one of two ,eywords

    to declare a variable that is to be associated with a decimal number! #lo!t and double. They

    are each offer a different level of precision as outlined below.

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    14/146

    P a g e  | 14

    #lo!t

    A float3 or floating point3 number has about seven digits of precision and a range of about

    ".8-%( to ".8O%(. A float ta,es four bytes to store.

    double

    A double3 or double precision3 number has about "% digits of precision and a range of about

    ".8-%?% to ".8O%?%. A double ta,es eight bytes to store.

    or example!

    #lo!t tot!l6

    double sum6

    To assign a numerical value to our floating point and double precision variables we would

    use the following C statement!

    tot!lI-6

    sumI12-$6

    Chara%ter /aria.lesC only has a concept of numbers and characters. >t very often comes as a surprise to some

     programmers who learnt a beginners language such as C that C has no understanding of 

     strings but a string is only an array of characters and C does have a concept of arrays which

    we shall be meeting later in this course.

    To declare a variable of type character we use the ,eyword c+!). - A single character stored

    in one byte.

    or example!

    c+!) c6

    To assign3 or store3 a character value in a c+!) data type is easy - a character variable is Hust a

    symbol enclosed by single Euotes. or example3 if c is a c+!) variable you can store the letter 

    0 in it using the following C statement!

    cI707

     1otice that you can only store a single character in a c+!) variable. +ater we will be

    discussing using character strings3 which has a very real potential for confusion because a

    string constant is written between double Euotes.

  • 8/19/2019 CPG Course File Shiplu

    15/146

    P a g e  | 1$

    Assignment 'tatement/nce youve declared a variable you can use it3 but not until it has been declared - attempts to

    use a variable that has not been defined will cause a compiler error. @sing a variable means

    storing something in it. Fou can store a value in a variable using!

    n!me I v!lue6

    or example!

    !I16

    stores the value 1 in the int variable !. 5hat could be simpler6 1ot much3 but it isnt

    actually very useful4 5ho wants to store a ,nown value li,e "? in a variable so you can use it

    later6 >t is "?3 always was "? and always will be "?. 5hat ma,es variables useful is that you

    can use them to store the result of some arithmetic.

    Consider four very simple mathematical operations! add3 subtract3 multiply and divide. +et us

    see how C would use these operations on two float variables ! andb.

    add

    !Fb

    subtract

    !.b

    multiply

    !Hb

    divide

    !Gb

     1ote that we have used the following characters from Cs character set!

    F #o) !dd

    . #o) subt)!ct

    H #o) multiplN

    G #o) divide

    TD A0>TDM8T>C444 5hat is the answer to this simple calculation6

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    16/146

    P a g e  | 1,

    !I1G3

    The answer depends upon how ! was declared. >f it was declared as type int the answer will

     be %# if ! is of type #lo!t then the answer will be %.%%%. >t is left as an exercise to the reader to

    find out the answer for ! of type c+!).

    Two points to note from the above calculation!

    ". C ignores fractions when doing integer division4

    $. 5hen doing #lo!t calculations integers will be converted into #lo!t. 5e will see later how Chandles type conversions.

    Arithmeti% Orering5hilst we are dealing with arithmetic we want to remind you about something that everyone

    learns at Hunior school but then we forget it. Consider the following calculation!

    !I1- F 2- H $- . ,- G 2-

    5hat is the answer6 >f you thin, its $: go to the bottom of the class4 Perhaps you got that

    answer by following each instruction as if it was being typed into a calculator. A computer

    doesnt wor, li,e that and it has its own set of rules when performing an arithmetic

    calculation. All mathematical operations form a hierarchy which is shown here. >n the above

    calculation the multiplication and division parts will be evaluated first and then the addition

    and subtraction parts. This gives an answer of ":.

     Note: %o avoid confusion use brackets. The following are two different calculations!

    !I1- F 2- H $- . ,- G 2-

    !I1- F 2- H $- . ,- G 2-

    Fou can freely mix int3 #lo!t and double variables in expressions. >n nearly all cases the

    lower precision values are converted to the highest precision values used in the expression.

    or example3 the expression #Hi3 where #  is a #lo!t and i is an int3 is evaluated by converting

    the int to a #lo!t and then multiplying. The final result is3 of course3 a #lo!t but this may beassigned to another data type and the conversion will be made automatically. >f you assign to

    a lower precision type then the value is truncated and not rounded. >n other words3 in nearly

    all cases you can ignore the problems of converting between types.

    This is very reasonable but more surprising is the fact that the data type c+!) can also be

    freely mixed with ints3 #lo!ts and doubles. This will shoc, any programmer who has used

    another language3 as its another example of C getting us closer than is customary to the way

    the machine wor,s. A character is represented as an A7C>> or some other code in the range /

    to $''3 and if you want you can use this integer code value in arithmetic. Another way of

    thin,ing about this is that a c+!) variable is Hust a single-byte integer variable that can hold anumber in the range / to $''3 which can optionally be interpreted as a character. 1otice3

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    17/146

    P a g e  | 1/

    however3 that C gives you access to memory in the smallest chun,s your machine wor,s

    with3 i.e. one byte at a time3 with no overheads.

    &ore On ,nitialising /aria.lesFou can assign an initial value to a variable when you declare it. or example!

    int iI16

    sets the int variable to one as soon as its created. This is Hust the same as!

    int i6

    iIl6

     but the compiler may be able to speed up the operation if you initialise the variable as part ofits declaration. 9ont assume that an uninitialised variable has a sensible value stored in it.

    7ome C compilers store ? in newly created numeric variables but nothing in the C language

    compels them to do so.

    umm!)N

    Qariable names!

    ". should be lowercase for local variables

    $. should be @PP80CA78 for symbolic constants to be discussed later2

    %. only the first %" characters of a variables name are significant

    &. must begin with a letter or R under score2 character

    ,nput an Output +un%tionsObjectives

    Daving read this section you should have a clearer idea of one of Cs!

    ". input functions3 called sc!n# 

    $. output functions3 called p)int# 

    On The Run8ven with arithmetic you cant do very much other than write programs that are the

    eEuivalent of a poc,et calculator. The real brea, through comes when you can read values

    into variables as the program runs. 1otice the important words here! *as the program runs*.Fou can already store values in variables using assignment. That is!

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    18/146

    P a g e  | 1J

    !I16

    stores "?? in the variable ! each time you run the program3 no matter what you do. 5ithout

    some sort of input command every program would produce exactly the same result every

    time it was run. This would certainly ma,e debugging easy4 or the moment dont worry about what the ;d or the n this way3 each time the program is run the user

    gets a chance to type in a different value to the variable and the program also gets the chance

    to produce a different result4

    The final missing piece in the Higsaw is using the p)int#  function3 the one we have already

    used to print *Dello 5orld*3 to print the value currently being stored in a variable. To display

    the value stored in the variable ! you would use!

    p)int#9T+e v!lue sto)ed in ! is ;d95!6

    The ;d3 both in the case of sc!n#  and p)int# 3 simply lets the compiler ,now that the value

     being read in3 or printed out3 is a decimal integer  - that is3 a few digits but no decimal point.

    *ote% the sc!n#  function does not prompt for an input. Fou should get in the habit of always

    using a p)int#  function3 informing the user of the program what they should type3 before a

    sc!n#  function.

    ,nput an Output +un%tions in &ore

    )etail/ne of the advantages of C is that essentially it is a small  language. This means that you can

    write a complete description of the language in a few pages. >t doesnt have many ,eywords

    or data types for that matter. 5hat ma,es C so powerful is the way that these low-level

    facilities can be put together to ma,e higher level facilities.

    The only problem with this is that C programmers have a tendency to reinvent the wheel each

    time they want to go for a ride. >t is also possible to write C programs in a variety of styleswhich depend on the particular tric,s and devices that a programmer chooses to use. 8ven

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    19/146

    P a g e  | 1

    after writing C for a long time you will still find the occasionally construction which ma,es

    you thin,3 *> never thought of that4* or3 *what is that doing6*

    /ne attempt to ma,e C a more uniform language is the provision of standard libraries of

    functions that perform common tas,s. 5e say standard but until the A17> committee actually

     produced a standard there was3 and still is3 some variation in what the standard  libraries

    contained and exactly how the functions wor,ed. Daving said that we had better rush in

    Euic,ly with the reassurance that in practice the situation isnt that bad and most of the

    functions that are used freEuently really are standard on all implementations. >n particular the

    >I/ functions vary very little.

    >t is now time to loo, at exactly how sc!n#  and p)int#  wor, and what they can do - you might

     be surprised at Hust how complex they really are4

    The original C specification did not include commands for input and output. >nstead the

    compiler writers were supposed to implement library functions to suit their machines. >n practice all chose to implement p)int#  and sc!n#  and after a while C programmers started to

    thin, of them as if these functions were >I/ ,eywords4 >t sometimes helps to remember that

    they are functions on a par with any other functions you may care to define. >f you want to

    you can provide your own implementations of p)int#  or sc!n#  or any of the other standard

    functions - well discover how later.

    The p)int#  and sc!n# 2 functions do differ from the sort of functions that you will created for

    yourself in that they can ta,e a variable number of parameters. >n the case of p)int#  the first

     parameter is always a string c.f. *Dello 5orld*2 but after that you can include as many

     parameters of any type that you want to. That is3 the p)int#  function is usually of the form!

    p)int#string,variable,variable,variable...

    where the ... means you can carry on writing a list of variables separated by commas as long

    as you want to. The string  is all-important because it specifies the type of each variable in the

    list and how you want it printed. The string is usually called the control string  or the format

     string . The way that this wor,s is that p)int#  scans the string from left to right and prints on

    the screen3 or any suitable output device3 any characters it encounters - except when it

    reaches a ; character. The ; character is a signal that what follows it is a specification for

    how the next variable in the list of variables should be printed. p)int#  uses this information toconvert and format the value that was passed to the function by the variable and then moves

    on to process the rest of the control string and anymore variables it might specify. or

    example!

    p)int#9Kello o)ld96

    only has a control string and3 as this contains no ; characters it results in Kello o)ld being

    displayed and doesnt need to display any variable values. The specifier ;d means convert

    the next value to a signed decimal integer  and so!

    p)int#9Tot!l I ;d95tot!l6

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    20/146

  • 8/19/2019 CPG Course File Shiplu

    21/146

    P a g e  | 21

    means a long integer variable usually four bytes2 and ;+d means short int. 1otice that there

    is no distinction between a four-byte #lo!t and an eight-byte double. The reason is that a #lo!t

    is automatically converted to a double precision value when passed to p)int#  - so the two can

     be treated in the same way. >n pre-A17> all #lo!ts were converted to double when passed to

    a function but this is no longer true.2 The only real problem that this poses is how to print the

    value of a pointer6 The answer is that you can use ;x to see the address in hex or ;o to see

    the address in octal. 1otice that the value printed is the segment offset and not the absolute

    address - to understand what we am going on about you need to ,now something about the

    structure of your processor.

    The 0 +ormat 'pe%i1ersThe ; specifiers that you can use in A17> C are!

    su!l v!)i!ble tNpe Displ!N

    ;c c+!) single c+!)!cte)

    ;d ;i int signed intege)

    ;e ;E #lo!t o) double exponenti!l #o)m!t

    ;# #lo!t o) double signed decim!l

    ;g ;( #lo!t o) double use ;# o) ;e !s )eQui)ed

    ;o int unsigned oct!l v!lue

    ;p pointe) !dd)ess sto)ed in pointe)

    ;s !))!N o# c+!) seQuence o# c+!)!cte)s

    ;u int unsigned decim!l

    ;x ;R int unsigned +ex v!lue

    +ormatting *our OutputThe type conversion specifier only does what you as, of it - it convert a given bit pattern into

    a seEuence of characters that a human can read. >f you want to format the characters then you

    need to ,now a little more about the p)int#  functions control string .

    8ach specifier  can be preceded by a modifier  which determines how the value will be printed.

    The most general modifier is of the form!

     flag i!th.precision

    The flag  can be any of!

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    22/146

    P a g e  | 22

    #l!g me!ning

    . le#t justi#N

    F !lS!Ns displ!N sign

    sp!ce displ!N sp!ce i# t+e)e is no sign

    p!d Sit+ le!ding e)os

    : use !lte)n!te #o)m o# speci#ie)

    The i!th specifies the number of characters used in total to display the value and precision 

    indicates the number of characters used after the decimal point.

    or example3 ;1-3#  will display the #lo!t using ten characters with three digits after thedecimal point. 1otice that the ten characters includes the decimal point3 and a . sign if there is

    one. >f the value needs more space than the i!th specifies then the additional space is used -

    i!th specifies the smallest space that will be used to display the value. This is Euiet

    reassuring3 you wont be the first programmer whose program ta,es hours to run but the

    output results cant be viewed because the wrong format width has been specified42

    The specifier ;.1Od will display an int left Hustified in a ten character space. The specifier

    ;F$d will display an int using the next five character locations and will add a F or . sign to

    the value.

    The only complexity is the use of the : modifier. 5hat this does depends on which type of

    format it is used with!

    ;:o !dds ! le!ding to t+e oct!l v!lue

    ;:x !dds ! le!ding x to t+e +ex v!lue

    ;:# o)

    ;:e ensu)es decim!l point is p)inted

    ;:g displ!Ns t)!iling e)os

    7trings will be discussed later but for now remember! if you print a string  using the ;s 

    specifier then all of the characters stored in the array up to the first null  will be printed. >f you

    use a i!th specifier then the string  will be right Hustified within the space. >f you include a

     precision specifier then only that number of characters will be printed.

    or example!

    p)int#9;s5Kello9

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    23/146

    P a g e  | 23

    will print Kello3

    p)int#9;2$s 5Kello9

    will print $' characters with Kello right Hustified and

    p)int#9;2$-3s5Kello9

    will print Kello right Hustified in a group of $' spaces.

    Also notice that it is fine to pass a constant value to p)int#  as in p)int#9;s5Kello9.

    inally there are the control  codes!

    Mb b!cksp!ce

    M# #o)m#eed

    Mn neS line

    M) c!))i!ge )etu)n

    Mt +o)iont!l t!b

    M7 single Quote

    M null

    >f you include any of these in the control string then the corresponding A7C>> control code is

    sent to the screen3 or output device3 which should produce the effect listed. >n most cases you

    only need to remember Mn for new line.

    sc!n# 

     1ow that we have mastered the intricacies of p)int#  you should find sc!n#  very easy. The

    sc!n#  function wor,s in much the same way as the p)int# . That is it has the general form!

    sc!n#control string,variable,variable,...

    >n this case the control string  specifies how strings of characters3 usually typed on the

    ,eyboard3 should be converted into values and stored in the listed variables. Dowever there

    are a number of important differences as well as similarities between sc!n#  and p)int# .

    The most obvious is that sc!n#  has to change the values stored in the parts of computers

    memory that is associated with parameters variables2.

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    24/146

    P a g e  | 24

    To understand this fully you will have to wait until we have covered functions in more detail.

  • 8/19/2019 CPG Course File Shiplu

    25/146

    P a g e  | 2$

    :include 8888stdio-+sc!n#9;d95

  • 8/19/2019 CPG Course File Shiplu

    26/146

    P a g e  | 2,

    Program Control>t is time to turn our attention to a different problem - conditional execution. 5e often need to

     be able to choose which set of instructions are obeyed according to a condition. or example3

    if youre ,eeping a total and you need to display the message O  if the value is greater than

    )ero you would need to write something li,e!

    i# tot!li# b8888! p)int#9econd numbe) is less t+!n #i)stMnMn96

    @ S+ile ! 8888 6

    @

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    27/146

    P a g e  | 2/

    Sprogram

    Deres another program using an i#  ,eyword and a compound statement  or a block !

    :include 8888stdio-+sc!n#9;d9 5

  • 8/19/2019 CPG Course File Shiplu

    28/146

    P a g e  | 2J

    form of the i#  statement.

    >n this case st!tement1 is carried out if the con!ition is true and st!tement2 if the con!ition 

    is false.

     1otice that it is certain that one of the two statements will be obeyed because the con!ition has to be either true or false4 Fou may be pu))led by the semicolon at the end of the i#  part of 

    the statement. The i# con!ition st!tement1 part is one statement and the else st!tement2 

     part behaves li,e a second separate statement3 so there has to be semi-colon terminating the

    first statement

    "ogi%al E$pressions7o far we have assumed that the way to write the con!itions used in loops and i#  statements is

    so obvious that we dont need to loo, more closely. >n fact there are a number of deviations

    from what you might expect. To compare two values you can use the standard symbols!

  • 8/19/2019 CPG Course File Shiplu

    29/146

    P a g e  | 2

    m!in

    ?

    int num15 num26

    p)int#9MnEnte) #i)st numbe) 96

    sc!n#9;d95

  • 8/19/2019 CPG Course File Shiplu

    30/146

    P a g e  | 3

    Using .rea# an %ontinue Within "oopsThe b)e!k  statement allows you to exit a loop from any point within its body3 bypassing its

    normal termination expression. 5hen the b)e!k  statement is encountered inside a loop3 the

    loop is immediately terminated3 and program control resumes at the next statement following

    the loop. The b)e!k  statement can be used with all three of Cs loops. Fou can have as manystatements within a loop as you desire. >t is generally best to use the b)e!k  for special

     purposes3 not as your normal loop exit. b)e!k  is also used in conHunction with functions and

    c!se statements which will be covered in later sections.

    The continue statement is somewhat the opposite of the b)e!k  statement. >t forces the next

    iteration of the loop to ta,e place3 s,ipping any code in between itself and the test condition

    of the loop. >n S+ile and do.S+ile loops3 a continue statement will cause control to go

    directly to the test condition and then continue the looping process. >n the case of the #o) 

    loop3 the increment part of the loop continues. /ne good use of continue is to restart a

    statement seEuence when an error occurs.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    31/146

    P a g e  | 31

    ?

    int t 6

    #o) 6 6 ?

    sc!n#9;d9 5

  • 8/19/2019 CPG Course File Shiplu

    32/146

    P a g e  | 32

    5hen a match is found3 the statement se$uence associated with that c!se are executed until

    b)e!k  is encountered.

    An example program follows!

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    33/146

    P a g e  | 33

    @ GH end o# sSitc+ HG

    @

    This simple program recogni)es the numbers " to & and prints the name of the one you enter.

    The sSitc+ statement differs from i# 3 in that sSitc+ can only test for eEuality3 whereas the i#  conditional expression can be of any type. Also sSitc+ will wor, with only int and c+!) 

    types. Fou cannot for example3 use floating-point numbers. >f the statement seEuence

    includes more than one statement they will have to be enclosed with ?@ to form a compound

     statement .

    Algorithms an +low%hart

     Algorithms

    1. A sequential solution of any program that written in human

    language,called algorithm.

    2. Algorithm is first step of the solution process, after the

    analysis of problem, programmer write the algorithm of that

    problem.

    3. Example of Algorithms:

    . !rite a algorithem to find out number is odd or e"en#

    Ans.

    step 1 : start

    step 2 : input number

    step 3 : rem$number mod 2

    step % : if rem$& then

      print 'number e"en'

      else

      print 'number odd'

      endif

    step ( : stop

    Flowchart

    1. )raphical representation of any program is called flowchart.

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    34/146

    P a g e  | 34

    2. *here are some standard graphics that are used in flowchart as

    following:

    Figure: Start/Stop terminal box

    Figure: Input/Output box

    Figure: Process/Instruction box

    Figure: Lines or Arrows

    Figure: Decision box

    Figure: Connector box

    Figure: Comment box

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    35/146

    P a g e  | 3$

    Figure: Preparation box

    Figure: Separate box

    V. Ma,e a flowchart to input temperature3 if temperature is less than %$ then print *below free)ing*

    otherwise print *above free)ing*6

    Ans.

    Figure: +lowchart example of program

    +low%hart for prime num.erQ.-raw the flowchart diagram for chec a

    number is prime number or not.

     Ans.

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    36/146

    P a g e  | 3,

    +lowchart for chec a number is prime or

    not as following:

    Figure: Flowchart for chec gi!en number is

    prime number or not

    lowchart is a diagrammatic representation of an algorithm. lowchart are very helpful in

    writing program and explaining program to others.

    'ym.ols Use ,n +low%hart9ifferent symbols are used for different states in flowchart3 or example! >nputI/utput and

    decision ma,ing has different symbols. The table below describes all the symbols that are

    used in ma,ing flowchart

    Nmbol Pu)pose Desc)iption

    low line @sed to indicate the flow of logic by connecting symbols.

    Terminal7topI7tart2 @sed to represent start and end of flowchart.

    >nputI/utput @sed for input and output operation.

    Processing @sed for airthmetic operations and data-manipulations.

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    37/146

    P a g e  | 3/

    Nmbol Pu)pose Desc)iption

    9esicion@sed to represent the operation in which there are two

    alternatives3 true and false.

    /n-page Connector @sed to Hoin different flowline

    /ff-page Connector @sed to connect flowchart portion on different page.

    Predefined

    ProcessIunction

    @sed to represent a group of statements performing one

     processing tas,.

    Examples of owcharts in programming

    D)!S ! #loSc+!)t to !dd tSo numbe)s ente)ed bN use)-

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    38/146

    P a g e  | 3J

    D)!S #loSc+!)t to #ind t+e l!)gest !mong t+)ee di##e)ent numbe)s ente)ed bN use)-

    D)!S ! #loSc+!)t to #ind !ll t+e )oots o# ! Qu!d)!tic eQu!tion !x2 FbxFcI

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    39/146

    P a g e  | 3

    D)!S ! #loSc+!)t to #ind t+e Uibon!cci se)ies till te)mV1-

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    40/146

    P a g e  | 4

    Though3 flowchart are useful in efficient coding3 debugging and analysis of a program3

    drawing flowchart in very complicated in case of complex programs and often ignored.

    D!t! TNpes P!)t

    Objectives

    7o far we have loo,ed at local  variable now we switch our attention to other types ofvariables supported by the C programming language!

    ". Jlobal Qariables

    2. Global variables%. Qariables can be declared as both local variables which can be used inside the

    function it has been declared in more on this in further sections2 and global variables 

    which are ,nown throughout the entire program. *lobal variables are created by

    declaring them outside any function. or example!

    &.   int m!x6

    '.   m!in

    (.   ?

    :.   -----

    ;.   @

    =.   #1

    "?. ?

    "". -----

    "$. @

    "%. The int m!x can be used in both main and function #1 and any changes made to it will

    remain consistent for both functions. The understanding of this will become clearer

    when you have studied the section on functions but > felt > couldnt complete a section

    on data types without mentioning global  and local  variables.

    "&. Constant 9ata Types

    15. Constant Data Types"(. Const!nts refer to fixed values that may not be altered by the program. All the data

    types we have previously covered can be defined as const!nt d!t! tNpes if we so

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    41/146

    P a g e  | 41

    wish to do so. The const!nt data types must be defined before the main function. The

    format is as follows!

    ":. :de#ine CO*T0*T*0&E v!lue

    ";. for example!

    "=. :de#ine 0LET0R -$

    $?. The const!nt name is normally written in capitals and does not have a semi-colon at

    the end. The use of const!nts is mainly for ma,ing your programs easier to be

    understood and modified by others and yourself in the future. An example program

    now follows!

    $". :de#ine 0LET0R -$

    $$. :include 8888stdio-+

    declared !mount3 t!xes and tot!l. 9isplay message to the screen is achieved using

    p)int#  and user input handled by sc!n# . Calculation is then performed and results sent

    to the screen. >f the value of 0LET0R alters in the future it is very easy to change

    the value where it is defined rather than go through the whole program changing the

    individual values separately3 which would be very time consuming in a large program

    with several references. The program is also improved when using constants rather

    than values as it improves the clarity

    . !rrays%&. Objectives

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    42/146

    P a g e  | 42

    %'. Daving read this section you should have a good understanding of the use of arrays in

    C.

    !"vance" Data TypesProgramming in any language ta,es a Euite significant leap forwards as soon as you learn

    about more advanced data types - arrays and strings of characters. >n C there is also a third

    more general and even more powerful advanced data type - the pointe) but more about that

    later. >n this section we introduce the !))!N3 but the first Euestion is3 why bother6

    There are times when we need to store a complete list  of numbers or other data items. Fou

    could do this by creating as many individual variables as would be needed for the Hob3 but this

    is a hard and tedious process. or example3 suppose you want to read in five numbers and

     print them out in reverse order. Fou could do it the hard way as!

    m!in

    ?

    int !l5!25!35!45!$6

    sc!n#9;d ;d ;d ;d ;d95

    p)int#9;d ;d ;d ;d ;d775!$5!45!35!25!16

    @

    9oesnt loo, very pretty does it3 and what if the problem was to read in "?? or more values

    and print them in reverse order6 /f course the clue to the solution is the use of the regular

    variable names !13 !2 and so on. 5hat we would really li,e to do is to use a name li,e !Ai 

    where i is a variable which specifies which particular value we are wor,ing with. This is the

     basic idea of an !))!N and nearly all programming languages provide this sort of facility -

    only the details alter.

    >n the case of C you have to declare an !))!N before you use it - in the same way you have to

    declare any sort of variable. or example3

    int !A$6

    declares an array called ! with five elements. Uust to confuse matters a little the first element

    is !A and the last !A4. C programmers always start counting at )ero4 +anguages varyaccording to where they start numbering arrays. +ess technical3 i.e. simpler3 languages start

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    43/146

    P a g e  | 43

    counting from " and more technical ones usually start counting from ?. Anyway3 in the case

    of C you have to remember that

    tNpe !))!NAsie

    declares an !))!N of the specified type and with si)e elements. The first !))!N element is!))!NA and the last is !))!NAsie.1.

    @sing an !))!N3 the problem of reading in and printing out a set of values in reverse order

     becomes simple!

    m!in

    ?

    int !A$6

    int i6

    #o)i I6i 8888 $6 FFi sc!n#9;d95

    #o)i I46i

  • 8/19/2019 CPG Course File Shiplu

    44/146

    P a g e  | 44

    is a hard and tedious process. or example3 suppose you want to read in five numbers and

     print them out in reverse order. Fou could do it the hard way as!

    m!in

    ?

    int !l5!25!35!45!$6

    sc!n#9;d ;d ;d ;d ;d95

    p)int#9;d ;d ;d ;d ;d775!$5!45!35!25!16

    @

    9oesnt loo, very pretty does it3 and what if the problem was to read in "?? or more valuesand print them in reverse order6 /f course the clue to the solution is the use of the regular

    variable names !13 !2 and so on. 5hat we would really li,e to do is to use a name li,e !Ai 

    where i is a variable which specifies which particular value we are wor,ing with. This is the

     basic idea of an !))!N and nearly all programming languages provide this sort of facility -

    only the details alter.

    >n the case of C you have to declare an !))!N before you use it - in the same way you have to

    declare any sort of variable. or example3

    int !A$6

    declares an array called ! with five elements. Uust to confuse matters a little the first element

    is !A and the last !A4. C programmers always start counting at )ero4 +anguages vary

    according to where they start numbering arrays. +ess technical3 i.e. simpler3 languages start

    counting from " and more technical ones usually start counting from ?. Anyway3 in the case

    of C you have to remember that

    tNpe !))!NAsie

    declares an !))!N of the specified type and with si)e elements. The first !))!N element is!))!NA and the last is !))!NAsie.1.

    @sing an !))!N3 the problem of reading in and printing out a set of values in reverse order

     becomes simple!

    m!in

    ?

    int !A$6

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    45/146

    P a g e  | 4$

    int i6

    #o)i I6i 8888 $6 FFi sc!n#9;d95

    #o)i I46i

  • 8/19/2019 CPG Course File Shiplu

    46/146

    P a g e  | 4,

    such a nuisance that C uses the simple convention that the end of a string of characters is

    mar,ed by a null  character. A null  character is3 as you might expect3 the character with A7C>>

    code ?. >f you want to store the null  character in a character variable you can use the notation

    M - but most of the time you dont have to actually use the null  character. The reason is that C

    will automatically add a null  character and store each character in a separate element when

    you use a string constant. A string constant is indicated by double Euotes as opposed to a

    character constant which is indicated by a single Euote. or example!

    909

    is a string constant3 but

    707

    is a character constant. The difference between these two superficially similar types of text is

    confusing at first and the source of many errors. All you have to remember is that *A*consists of two characters3 the letter 0 followed by M whereas A is Hust the single character

    A. >f you are familiar with other languages you might thin, that you could assign string

    constants to character arrays and wor, as if a string was a built-in data type. >n C however the

    fundamental data type is the array and strings are very much grafted on. or example3 if you

    try something li,e!

    c+!) n!meA46

    n!meI9Kello9

    it will not wor,. Dowever3 you can print strings using p)int#  and read them into character

    arrays using sc!n# . or example3

    m!in

    ?

    st!tic c+!) n!meA4 I9+ello96

    p)int#9;s95n!me6

    sc!n#9;s95n!me6

    p)int#9;s95n!me6

    @

    S program

    This program reads in the text that you type3 terminating it with a null  and stores it in the

    character array n!me. >t then prints the character array treating it as a string3 i.e. stopping

     Department of Computer Science & Technology, BBIT 

    http://www.le.ac.uk/cc/rjm1/c/programs/array2.chttp://www.le.ac.uk/cc/rjm1/c/programs/array2.c

  • 8/19/2019 CPG Course File Shiplu

    47/146

    P a g e  | 4/

    when it hits the first null  string. 1otice the use of the *;s* format descriptor in sc!n#  and

    p)int#  to specify that what is being printed is a string.

    At this point the way that strings wor, and how they can be made a bit more useful and

    natural depends on understanding pointe)s which is covered in the next section.

    +un%tions an PrototypesObjectives

    Daving read this section you should be able to!

    ". program using correctly defined C functions

    $. pass the value of local variables into your C functions

    #$nctions % C&s '$il"ing 'loc(s7ome programmers might consider it a bit early to introduce the C function - but we thin,

    you cant get to it soon enough. >t isnt a difficult idea and it is incredibly useful. Fou could

    say that you only really start to find out what C programming is all about when you start

    using functions.

    C functions are the eEuivalent of what in other languages would be called subroutines or

     procedures. >f you are familiar with another language you also need to ,now that C only has

    functions3 so dont spend time loo,ing for the definition of subroutines or procedures - in C

    the function does everything4

    A function is simply a chun, of C code statements2 that you have grouped together and

    given a name. The value of doing this is that you can use that *chun,* of code repeatedly

    simply by writing its name. or example3 if you want to create a function that prints the word

    *Kello* on the screen and adds one to variable called tot!l then the chun, of C code that you

    want to turn into a function is Hust!

    p)int#9Kello96

    tot!l I tot!l F l6

    To turn it into a function you simply wrap the code in a pair of curly brac,ets to convert it

    into a single compound statement  and write the name that you want to give it in front of the

     brac,ets!

    demo

    ?

    p)int#9Kello96

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    48/146

    P a g e  | 4J

    tot!l I tot!l F 16

    @

    9ont worry for now about the curved brac,ets after the functions name. /nce you have

    defined your function you can use it within a program!

    m!in

    ?

    demo6

    @

    >n this program the instruction demo # is entirely eEuivalent to writing out all of thestatements in the function. 5hat we have done is to create an new C function and this3 of

    course3 is the power of functions. 5hen you are first introduced to the idea of functions3 or

    their eEuivalent in other languages3 it is easy to fall into the trap of thin,ing that they are only

    useful when you want to use a bloc, of code more than once.

    unctions are useful here but they have a more important purpose. >f you are creating a long

     program then functions allow you to split it into *bite-si)ed* chun,s which you can wor, on

    in isolation. As every C programmer ,nows3 * functions are the building blocks of programs.*

    +un%tions an "o%al /aria.les 1ow that the philosophy session is over we have to return to the details - because as it stands

    the demo function will not wor,. The problem is that the variable tot!l isnt declared

    anywhere. A function is a complete program sub-unit in its own right and you can declare

    variables within it Hust as you can within the m!in program. >f you loo, at the m!in program

    we have been using you will notice it is in fact a function that Hust happens to be called

    *main*4 7o to ma,e demo wor, we have to add the declaration of the variable total!

    demo

    ?

    int tot!l6

    p)int#9Kello96

    tot!lItot!lF16

    @

     1ow this raises the Euestion of where exactly tot!l is a valid variable. Fou can certainly usetot!l within the function that declares it - this much seems reasonable - but what about other

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    49/146

    P a g e  | 4

    functions and3 in particular3 what about the m!in program6 The simple answer is that tot!l is

    a variable that belongs to the demo function. >t cannot be used in other functions3 it doesnt

    even exist in other functions and it certainly has nothing to do with any variable of the same

    name that you declare within other functions.

    This is what we hinted at when we said that functions were isolated chunks of code. Their

    isolation is such that variables declared within the function can only be used within that

    function. These variables are ,nown as local variables and as their name suggests are local to

    the function they have been declared in. >f you are used to a language where every variable is

    usable all the time this might seem silly and restrictive - but it isnt. >ts what ma,es it possible

    to brea, a large program down into smaller and more manageable chun,s.

    The fact that tot!l is only usable within the demo function is one thing - but notice we said

    that it only existed within this function3 which is a more subtle point. The variables that a

    function declares are created when the function is started and destroyed when the function is

    finished. 7o if the intention is to use tot!l to count the number of times the

  • 8/19/2019 CPG Course File Shiplu

    50/146

    P a g e  | $

    The only way in which parameters are any different is that you can give them initial values

    when the function starts by writing the values between the round brac,ets. 7o

    suml526

    is a call to the sum function with ! set to " and b set to $ and so )esult is set to %. Fou canalso initialise parameters to the result of expressions such as!

    sumxF25H16

    which will set ! eEual to whatever xF2 wor,s out to be and b eEual to whatever H1 wor,s

    out to be.

    As a simpler case you can also set a parameter to the value in a single variable - for example!

    sumx5N6

    will set ! to the value stored in x and b to the value stored in N.

    Parameters are the main way of getting values into a function3 but how do we get values out6

    There is no point in expecting the

  • 8/19/2019 CPG Course File Shiplu

    51/146

    P a g e  | $1

    The simplest way to get a value out of a function is to use the )etu)n instruction. A function

    can return a value via its name - its as if the name was a variable and had a value. The value

    that is returned is specified by the instruction!

    )etu)nvalue6

    which can occur anywhere within the function3 not Hust as the last instruction - however3 a

    )etu)n always terminates the function and returns control bac, to the calling function. The

    only complication is that as the functions name is used to return the value it has to be given a

    data type. This is achieved by writing the data type in front of the functions name. or

    example!

    int sum!5b6

    7o now we can at last write the correct version of the sum function!

    int sumint !5 int b

    ?

    int )esult6

    )esult I ! F b6

    )etu)n )esult6

    @

    and to use it you would write something li,e!

    )Isum1526

    which would add " to $ and store the result in ). Fou can use a function anywhere that you

    can use a variable. or example3

    )Isum152H3

    is perfectly /B3 as is

    )I3Fsum152Gn.1

    /bviously3 the situation with respect to the number of inputs and outputs of a function isnt

    eEual. That is you can create as many parameters as you li,e but a function can )etu)n only a

    single value. +ater on we will have to find ways of allowing functions to return more than

    one value.2

    7o to summarise! a function has the general form!

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    52/146

  • 8/19/2019 CPG Course File Shiplu

    53/146

  • 8/19/2019 CPG Course File Shiplu

    54/146

    P a g e  | $4

    The 'tanar "i.rary +un%tions7ome of the *commands* in C are not really *commands* at all but are functions. or

    example3 we have been using p)int#  and sc!n#  to do input and output3 and we have used

    )!nd to generate random numbers - all three are functions.

    There are a great many standard functions that are included with C compilers and while these

    are not really part of the language3 in the sense that you can re-write them if you really want

    to3 most C programmers thin, of them as fixtures and fittings. +ater in the course we will

    loo, into the mysteries of how C gains access to these standard functions and how we can

    extend the range of the standard library.

  • 8/19/2019 CPG Course File Shiplu

    55/146

    P a g e  | $$

    &   cos returns cosine of arg

    '   exp returns natural logarithim e

    (   #!bs returns absolute value of num

    :   sQ)t returns sEuare root of num

    $   time-+% Time !nd D!te #unctions

    "   time returns current calender time of system

    $   di##time returns difference in secs between two times

    %   clock returns number of system cloc, cycles since program execution

    $   stdlib-+%&iscell!neous #unctions

    "   m!lloc provides dynamic memory allocation3 covered in future sections

    $   )!nd as already described previously

    %   s)!nd used to set the starting point for rand2

    En" of the Co$rseFouve now reached the end of this online tutorial. 5e have covered a lot of ground - but this

    has been a first course in C and there is still plenty to learn. Dowever3 as long as you ,eep in

    mind that C is an essentially simple language and how new features are built from this

    simplicity you shouldnt have many problems.

    Fou also need to be aware of the fact that C is a very low-level language and as a result

    allows programmers to confuse data types and muc, around with the bit patterns of the data

    in a way that higher level languages would disown4 Fou probably need to ma,e sure that you

    understand binary and the way that values are represented to get the best from C.

    The final Euestion is3 where should you go next6 >f you have enHoyed learning C then you

     probably need to move on to COO the language most li,ely to reign supreme in the future4

    5ithout any recommendation try these COO tutorials!

    ,nput an Output programsThis section contains example programs demonstrating the input and output functions.

    This program uses the sc!n#  function.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    56/146

    P a g e  | $,

    m!in

    ?

    #lo!t ! 5 b 5 c6

    p)int#9nput tSo numbe)s--- Mn96

    sc!n#9;#;#9 5

    c I ! H b 6

    p)int#9;# times ;# I ;#Mn9 5 ! 5 b 5 c6

    @

    This program combines p)int#  and sc!n#  whereby p)int#  displays the input from sc!n# .

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    57/146

    P a g e  | $/

    @ S+ile c+ =I 7-76

    p)int#9MnMn96

    p)int#9Ente) some text tNpe ! pe)iod to Quit---Mn96

    S+ile c+ I getc+!) =I 7-7 putc+!)c+.16

    p)int#9MnMn96

    @

    Control "oop programsThis section contains example programs demonstrating the loop construction.

    Program using c+!) data type and simple #o) loop!

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    58/146

    P a g e  | $J

    #lo!t #!+) 5 celsius6

    loSe) I 6

    uppe) I 36

    step I 2 6

    #!+) I loSe)6

    S+ile #!+) 8888I uppe) ?

    celsius I $- G - H #!+) . 32-6

    p)int#9;4-# ;,-1#Mn9 5 #!+) 5 celsius6

    #!+) I #!+) F step6

    @

    @

    This program uses do.S+ile and #o) loop constructions and standard library mathematical

    functions.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    59/146

    P a g e  | $

    iIiHj 6

    p)int#9;dMn9 5 i6

    @

    @

    Conitional E$e%ution programsThis section contains example programs demonstrating i# else and select statements also

    b)e!k  and continue statements2.

    This program demonstrates the i# else statement.

    :include 8888stdio-+else p)int#9--- c!nnot divide bN e)oMn96

    @

    Another example of i# else statement.

    :include 8888stdio

  • 8/19/2019 CPG Course File Shiplu

    60/146

    P a g e  | ,

    c+!) c+6

    p)int#9Do Nou S!nt to% Mn96

    p)int#90dd5 subt)!ct5 &ultiplN5 o) DivideXMn96

    GH #o)ce use) to ente) v!lid )esponse HG

    do ?

    p)int#9Ente) #i)st lette)% 96

    c+Igetc+!)6

    p)int#9Mn96

    @ S+ile c+=I707 sc!n#9;d95

  • 8/19/2019 CPG Course File Shiplu

    61/146

    P a g e  | ,1

    c+!) c+6

    p)int#9Do Nou S!nt to%Mn96

    p)int#90dd5 ubt)!ct5 &ultipN5 o) DivideMn96

    GH #o)ce use) to ente) v!lid )esponse HG

    do

    p)int#9Ente) #i)st lette)% 96

    c+ Igetc+!)6

    p)int#9Mn96

    @ S+ile c+=I707 sc!n#9;d95

  • 8/19/2019 CPG Course File Shiplu

    62/146

    P a g e  | ,2

    The statement seEuence associated with a c!se may be empty allowing two or more cases to

    share a common statement seEuence.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    63/146

    P a g e  | ,3

    int x 6

    #o) xI 6 x8888I1 6 xFF ?

    i# x;2 continue6 GH using modulus ope)!tion HG

    p)int#9;dMn9 5 x6

    @

    This program Humps out of an infinite loop using the b)e!k  statement.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    64/146

    P a g e  | ,4

    int i 5 j6

    #o) iI2 6 i88881 6 iFF ?

    #o) jI2 6 j8888I iG2 6 jFF

    i# =i;j b)e!k6

    i# jp)int#9Ui)st numbe) is less t+!n secondMnMn96

    i# b8888!

    p)int#9econd numbe) is less t+!n #i)stMnMn96

    @ S+ile ! 8888 6

    @

    This program uses nested #o) loop and i# else construction.

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    65/146

  • 8/19/2019 CPG Course File Shiplu

    66/146

    P a g e  | ,,

    p)int#9t+e !nsSe) is ;d-Mn95 countFcount6

    @ GH end o# else p!)t HG

    @ GH end o# outte) #o) loop HG

    @

    +un%tions an Prototype programsThis section contains example programs to demonstrate the use of #unctions.

    This is a program to demonstrate the use of #unctions

    ".   Sit+ no p!)!mete)s

    $.   Sit+ p!)!mete)s

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    67/146

    P a g e  | ,/

    int sum I 16

    int i 6

    #o) i I 1 6 i 8888I t 6 iFF

    sum I sumHi6

    p)int#9U!cto)i!l ;d is ;dMn9 5 t 5 sum6

    @

    Dere is a volume computing program using a function prototype.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    68/146

    P a g e  | ,J

    void m!invoid

    ?

    )ecu)se6

    @

    void )ecu)seint i

    ?

    i# i88881 ?

    )ecu)seiF16

    p)int#9;d 95i6

    @

    @

    This program prints

    J/,$4321

    on the screen.

    Array programsThis section contains example programs demonstrating the use of arrays.

    A simple program to demonstrate the definition and use of an array.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    69/146

    P a g e  | ,

    iAj I j 6

    #o) jI 6 j88881 6 jFF

    p)int#9;dMn9 5 iAj 6

    @

    A program to demonstrate the use of an array.

    :include 8888stdio-+v!lue 6

    GH Uind t+e m!ximum v!lue HG

    m!x>v!lue I 6

    #o) iI 6 i88881 6 iFF

    i# listAi

  • 8/19/2019 CPG Course File Shiplu

    70/146

    P a g e  | /

    p)int#9&!ximum v!lue gene)!ted is ;dMn9 5 m!x>v!lue 6

    @

    This program uses a bubble sort.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    71/146

    P a g e  | /1

    @

    GH displ!N so)ted list HG

    #o) tI6 t8888count6 tFF p)int#9;d 95 itemAt6

    @

    An example program using a two-dimensional array now follows.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    72/146

    P a g e  | /2

    Pointer programsThis section contains example programs demonstrating the use of pointers.

    An example program using simple pointers.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    73/146

    P a g e  | /3

    Hp..6

    p)int#9;dMn9 5 num6

    @

    An example program demonstrating pointer Arithmetic.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    74/146

    P a g e  | /4

    p)int#9Ente) ! sentence% 96

    getsst)6

    p I st)6

    S+ile Hp ?

    Q I token6

    S+ile Hp =I 7 7

  • 8/19/2019 CPG Course File Shiplu

    75/146

    P a g e  | /$

    p)int#9Mn96

    @

    The follow program reEuests the input of two strings3 then demonstrates the four string

    functions with them.

    :include 8888st)ing-+

  • 8/19/2019 CPG Course File Shiplu

    76/146

    P a g e  | /,

    p)int#9;sMn95 st)16

    @

    GH copN st)2 to st)1 HG

    st)cpNst)15 st)16

    p)int#9;s ;sMn95 st)15 st)26

    @

    'tru%ture programsThis section contains example programs to demonstrate the use of structures.

    This program stores data in a structure and displays the values.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    77/146

    P a g e  | //

    :include 8888stdio-+tNpe temp6

    temp-iI16

    temp-d I 123-236

    )etu)n temp6

    @

    This program passes a structure to a function.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    78/146

    P a g e  | /J

    void #st)uct s>tNpe temp6

    void m!invoid

    ?

    v!)1-iI6

    v!)1-d I J-,6

    #v!)16

    @

    void #st)uct s>tNpe temp

    ?

    p)int#9;d ;1#95 temp-i5 temp-d6

    @

    The following program illustrates how to use a pointer to a structure.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    79/146

    P a g e  | /

    p)int#9;d ;d ;s95 s-i5 p.

  • 8/19/2019 CPG Course File Shiplu

    80/146

    P a g e  | J

    Comple$ programs %om.ining se%tionsThis example program creates and accesses a simple database.

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH 0 simple D!t! B!se P)og)!m HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    : include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    81/146

    P a g e  | J1

    c!se 2% del6 GH delete !n ent)N HG

    b)e!k6

    c!se 3% s+oS>list6 GH displ!N t+e list HG

    b)e!k6

    c!se 4% se!)c+6 GH #ind !n ent)N HG

    b)e!k6

    c!se $% s!ve6 GH s!ve to disk HG

    b)e!k6

    c!se ,% lo!d6 GH )e!d #)om disk HG

    b)e!k6

    c!se /% exit6

    @

    @

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH Uunction del HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    del

    ?

    int i6

    c+!) st)A2$$6

    inputs9ente) n!me% 9 5 st) 5 36

    i I #indst)6

    i# i

  • 8/19/2019 CPG Course File Shiplu

    82/146

    P a g e  | J2

    else p)int#9not #oundMn9 6

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH Uunction displ!N HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    displ!Nint i

    ?

    p)int#9;sMn9 5 listAi-n!me6

    p)int#9;sMn9 5 listAi-st)eet6

    p)int#9;sMn9 5 listAi-toSn6

    p)int#9;sMn9 5 listAi-countN6

    p)int#9;sMn9 5 listAi-code6

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH Uunction ente) HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    ente)

    ?

    int i6

    #o)66 ?

    i I #ind>#)ee6 GH #ind ! #)ee st)uctu)e HG

    i#i8888 ?

    p)int#9list #ullMn96

    )etu)n6

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    83/146

    P a g e  | J3

    @

    inputs9ente) n!me% 95 listAi-n!me536 i#=HlistAi-n!me b)e!k6 GH stop ente)ing HG

    inputs9ente) st)eet% 95 listAi-st)eet5 46

    inputs9ente) toSn% 95 listAi-toSn5 26

    inputs9ente) countN% 95 listAi-countN5 26

    inputs9ente) Post!l code% 95 listAi-code5 16

    @

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH Uunction #ind HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    #indc+!) Hn!me

    ?

    int i6

    #o)iI 6 i8888&0R 6 iFF

    i#=st)cmpn!me 5listAi-n!me b)e!k6

    i#iII&0R )etu)n

    else )etu)n i6

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH Uunction #ind>#)ee HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    #ind>#)ee

    ?

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    84/146

  • 8/19/2019 CPG Course File Shiplu

    85/146

    P a g e  | J$

    @ S+ilest)lenst)

  • 8/19/2019 CPG Course File Shiplu

    86/146

    P a g e  | J,

    c+!) sAJ6

    int c6

    p)int#91- Ente) ! n!meMn9 6

    p)int#92- Delete ! n!meMn9 6

    p)int#93- List t+e Uile Mn96

    p)int#94- e!)c+Mn9 6

    p)int#9$- !ve t+e #ileMn9 6

    p)int#9,- Lo!d t+e #ileMn9 6

    p)int#9/- ZuitMn9 6

    do ?

    p)int#9MnEnte) Nou) c+oice% 96

    getss6

    c I !tois6

    @ S+ilec8888 c

  • 8/19/2019 CPG Course File Shiplu

    87/146

    P a g e  | J/

    )etu)n6

    @

    p)int#9Mns!ving #ileMn96

    #S)itelist 5 sieo# list 5 1 5 #p6

    i# #e))o)#p

    p)int#90n e))o) occu))ed S+ile S)iting #ile-Mn96

    #close#p6

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH Uunction se!)c+ HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    se!)c+

    ?

    int i6

    c+!) n!meA36

    inputs9ente) n!me to #ind% 9 5 n!me 5 36

    i# iI#indn!me8888

    p)int#9not #oundMn96

    else displ!Ni6

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH Uunction s+oS>list HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    s+oS>list

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    88/146

    P a g e  | JJ

    ?

    int i6

    #o)iI 6 i8888&0R 6 iFF ?

    i#HlistAi-n!me ?

    displ!Ni6

    p)int#9MnMn96

    @

    @

    p)int#9MnMn96

    @

    S program

    The second example program uses functions to play a simple game of tic tac toe.

    :include 8888stdio-+

  • 8/19/2019 CPG Course File Shiplu

    89/146

    P a g e  | J

    get>pl!Ne)>move6

    done I c+eck6

    i# done =I 7 7 b)e!k6

    get>compute)>move6

    done I c+eck6

    @ S+ile done II 7 76

    i# done II 7R7 p)int#9MnMnYou Son===Mn96

    else p)int#9 Son===Mn96

    disp>m!t)ix6

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH #uction to initi!lise m!t)ix HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    init>m!t)ix

    ?

    int i 5 j 6

    #o) iI 6 i88883 6 iFF

    #o) jI 6 j88883 6 jFF

    m!t)ixAiAj I 7 76

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH #uction to get pl!Ne)s move HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    get>pl!Ne)>move

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    90/146

    P a g e  |

    ?

    int x 5 N 6

    p)int#9Ente) coo)din!tes o# Nou) R% 96

    sc!n#9;d;d9 5 move6

    @

    else m!t)ixAxAN I 7R76

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH #uction to get compute) move HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    get>compute)>move

    ?

    int i 5 j 6

    #o) iI 6 i88883 6 iFF ?

    #o) jI 6 j88883 6 jFF

    i#m!t)ixAiAjII7 7 b)e!k6

    i# m!t)ixAiAj II 7 7 b)e!k6

    @

    i# iHj II ?

    p)int#9d)!S----Mn96

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    91/146

    P a g e  | 1

    exit6

    @

    else m!t)ixAiAj I 7O76

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH #uction to displ!N m!t)ix HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    disp>m!t)ix

    ?

    int t 6

    p)int#9 1 2 3Mn96

    #o) tI 6 t88883 6 tFF ?

    p)int#9 ;c ;c ;c ;d9 5 m!t)ixAtA5

    m!t)ixAtA15 m!t)ixAtA2 5 tF16

    i# t=I2 p)int#9Mn.........Mn96

    @

    p)int#9Mn96

    @

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    GH #uction to c+eck m!t)ix HG

    GHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHG

    c+eck

    ?

    int i 6

     Department of Computer Science & Technology, BBIT 

  • 8/19/2019 CPG Course File Shiplu

    92/146

    P a g e  | 2

    #o) iI 6 i88883 6 iFF

    i#m!t)ixAiA II m!t)ixAiA1

  • 8/19/2019 CPG Course File Shiplu

    93/146

    P a g e  | 3

    (. 5hat is the difference between top down approach and bottom up approach in programming

    languages6

    :. 5hat is the difference between C and Uava6

    ;. C language has been developed in which language6

    =. 5hich year C language is developed6

    "?. 5hat is meant by programming language and give some examples6

    "". 9escribe about C standards.

    "$. 5hat are the ,ey features of C language or what are the characteristics of C language6

    "%. 5hat is embedded C6

    "&. 5hich level is C language belonging to6

    "'. 5hat do you mean by high level3 middle level and low level languages and give an example

    for each6

    "(. 5hat is the difference between structured oriented3 obHect oriented and non-structure oriented

     programming language6

    ":. 5hat is compiler6

    ";. 5hat is the difference between assembler3 compiler and interpreter6

    "=. 5hat is printf26

    $?. 5hat is scanf26

    $". 5hat is meant by protocol6

    $$. 8xecution of a C program starts from which function6

    $%. 5hat are all the sections that a C program may have and must have6

     Department of Computer Science & Technology, BBIT 

    http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-uses-difference/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-uses-difference/http://fresh2refresh.com/c-interview-questions-answers/c-language-java-difference-year-development/http://fresh2refresh.com/c-interview-questions-answers/c-language-java-difference-year-development/http://fresh2refresh.com/c-interview-questions-answers/c-language-java-difference-year-development/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-standards-characteristics/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-standards-characteristics/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-standards-characteristics/http://fresh2refresh.com/c-interview-questions-answers/c-belonging-level-embedded/http://fresh2refresh.com/c-interview-questions-answers/c-belonging-level-embedded/http://fresh2refresh.com/c-interview-questions-answers/c-belonging-level-embedded/http://fresh2refresh.com/c-interview-questions-answers/c-belonging-level-embedded/http://fresh2refresh.com/c-interview-questions-answers/difference-c-structure-object-assembler-compiler-interpreter/http://fresh2refresh.com/c-interview-questions-answers/difference-c-structure-object-assembler-compiler-interpreter/http://fresh2refresh.com/c-interview-questions-answers/difference-c-structure-object-assembler-compiler-interpreter/http://fresh2refresh.com/c-interview-questions-answers/difference-c-structure-object-assembler-compiler-interpreter/http://fresh2refresh.com/c-interview-questions-answers/printf-scanf-protocol-c/http://fresh2refresh.com/c-interview-questions-answers/printf-scanf-protocol-c/http://fresh2refresh.com/c-interview-questions-answers/printf-scanf-protocol-c/http://fresh2refresh.com/c-interview-questions-answers/c-program-start-function-section-ide/http://fresh2refresh.com/c-interview-questions-answers/c-program-start-function-section-ide/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-uses-difference/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-uses-difference/http://fresh2refresh.com/c-interview-questions-answers/c-language-java-difference-year-development/http://fresh2refresh.com/c-interview-questions-answers/c-language-java-difference-year-development/http://fresh2refresh.com/c-interview-questions-answers/c-language-java-difference-year-development/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-standards-characteristics/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-standards-characteristics/http://fresh2refresh.com/c-interview-questions-answers/c-programming-language-standards-characteristics/http://fresh2refresh.com/c-interview-questions-answers/c-belonging-level-embedded/http://fresh2refresh.com/c-interview-questions-answers/c-belonging-level-embedded/http://fresh2refresh.com/c-interview-questions-answers/c-belonging-level-embedded/http://fresh2refresh.com/c-interview-questions-answers/c-belonging-level-embedded/http://fresh2refresh.com/c-interview-questions-answers/difference-c-structure-object-assembler-compiler-interpreter/http://fresh2refresh.com/c-interview-questions-answers/difference-c-structure-object-assembler-compiler-interpreter/http://fresh2refresh.com/c-interview-questions-answers/difference-c-structure-object-assembler-compiler-interpreter/http://fresh2refresh.com/c-interview-questions-answers/difference-c-structure-object-assembler-compiler-interpreter/http://fresh2refresh.com/c-interview-questions-answers/printf-scanf-protocol-c/http://fresh2refresh.com/c-interview-questions-answers/printf-scanf-protocol-c/http://fresh2refresh.com/c-interview-questions-answers/printf-scanf-protocol-c/http://fresh2refresh.com/c-interview-questions-answers/c-program-start-function-section-ide/http://fresh2refresh.com/c-interview-questions-answers/c-progra