UGC NET COMPUTER SCIENCE.docx

Embed Size (px)

Citation preview

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    1/18

    Chapter 1Programming in C and C++

    Introduction:

    C is a general-purpose, high-level language, originally developed by Dennis M. Ritchie at Bell abs to

    develop the !"#$ operating system. C %as originally &irst implemented on the D'C PDP-11 computer in

    1()*. C %as initially used &or system development %or, in particular, those that mae-up the operating

    system. C %as adopted as a system development language as it could be eecuted as &ast as those %ritten inassembly language. C program basically consists o& the &ollo%ing parts

    Preprocessor Commands

    /unctions

    0ariables

    tatements 2 'pressions

    Comments

    C program consists o& various toens and a toen is either a ey%ord, an identi&ier, a constant, a string

    literal, or a symbol. #n C program, the semicolon is a statement terminator. 3hat is, each individual statement

    must be ended %ith a semicolon. #t indicates the end o& one logical entity.. C identi&ier is a name used to

    identi&y a variable, &unction, or any other user-de&ined item. n identi&ier starts %ith a letter to 4 or a to 5

    or an underscore 6 &ollo%ed by 5ero or more letters, underscores, and digits 78 to (9.

    C does not allo% punctuation characters such as :, ;, and < %ithin identi&iers. C is a case

    sensitiveprogramming language

    =ey%ords are reveserved %ords in C language %ith a prede&ined &unctionality. >hitespace is the term used

    in C to describe blans, tabs, ne%line characters and comments.

    Data types in C

    Basic 3ypes 3hey are arithmetic types and consists o& the t%o types 7a9 integer types and 7b9 &loating-point

    types.

    'numerated types 3hey are again arithmetic types and they are used to de&ine variables that can only be

    assigned certain discrete integer values throughout the program.

    3he type void indicates that no value is available.

    Derived types3hey include 7a9 Pointer types, 7b9 rray types, 7c9 tructure types, 7d9 !nion types and 7e9

    /unction types.

    Type Storage size Value range

    Char 1 byte -1*? to 1*) or 8 to *@@

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    2/18

    unsigned char 1 byte 8 to *@@

    signed char 1 byte -1*? to 1*)

    #nt * or A bytes -*,)? to *,)) or -*,1A),A?,A? to *,1A),A?,A)

    unsigned int * or A bytes 8 to @,@@ or 8 to A,*(A,(),*(@

    hort * bytes -*,)? to *,))

    unsigned short * bytes 8 to @,@@

    ong A bytes -*,1A),A?,A? to *,1A),A?,A)

    unsigned long A bytes 8 to A,*(A,(),*(@

    Type Storage size Value range Precision

    /loat A byte 1.*'-? to .A'+? decimal places

    Double ? byte *.'-8? to 1.)'+8? 1@ decimal places

    long double 18 byte .A'-A(* to 1.1'+A(* 1( decimal places

    0ariables

    values and Rvalues in C

    3here are t%o inds o& epressions in C

    1. lvalue :n epression that is an lvalue may appear as either the le&t-hand or right-hand side o& an

    assignment.

    *. rvalue :n epression that is an rvalue may appear on the right- but not le&t-hand side o& an

    assignment.

    De&ining Constants

    3here are t%o simple %ays in C to de&ine constants

    1. !sing #definepreprocessor.*. !sing constey%ord.

    3here are the &ollo%ing storage classes, %hich can be used in a C Program

    uto de&ault storage class &or all local variables

    Register used to de&ine local variables that should be stored in a register instead o& RM. 3his

    means that the variable has a maimum si5e eual to the register si5e 7usually one %ord9 and canEt have the

    unary E2E operator applied to it

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    3/18

    cope instructs the compiler to eep a local variable in eistence during the li&e-time o& the program

    instead o& creating and destroying it each time it comes into and goes out o& scope

    'tern used to give a re&erence o& a global variable that is visible to the program &iles. >hen

    you use EeternE, the variable cannot be initiali5ed as all it does is point the variable name at a storage

    location that has been previously de&ined

    Scope :

    0ariable is said to have global scopeF&ile scope i& it is de&ined outside the &unction and %hose visibility is the

    entire program.

    0ariable is said to have Bloc scopeF local scope i& it is de&ined %ithin the &unction or local bloc.

    Operators :

    3he &ollo%ing types o& operators

    rithmetic Gperators

    Relational Gperators

    ogical Gperators

    Bit%ise Gperators

    ssignment Gperators

    Operato

    r Description Example

    + dds t%o operands + B %illgive 8

    - ubtracts second operand &rom the &irst - B %illgive -18

    H Multiplies both operands H B %illgive *88

    F Divides numerator by de-numerator B F %illgive *

    < Modulus Gperator and remainder o& a&ter an integer divisionB < %illgive 8

    ++ #ncrements operator increases integer value by one++ %illgive 11

    -- Decrements operator decreases integer value by one -- %illgive (

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    4/18

    Operato

    r Description Example

    IIChecs i& the values o& t%o operands are eual or not, i& yes

    then condition becomes true.

    7 II B9

    is not

    true.

    JIChecs i& the values o& t%o operands are eual or not, i& values

    are not eual then condition becomes true.

    7 JI B9

    is true.

    KChecs i& the value o& le&t operand is greater than the value o&

    right operand, i& yes then condition becomes true.

    7 K B9

    is not

    true.

    LChecs i& the value o& le&t operand is less than the value o&

    right operand, i& yes then condition becomes true.

    7 L B9

    is true.

    KIChecs i& the value o& le&t operand is greater than or eual to

    the value o& right operand, i& yes then condition becomes true.

    7 KI B9

    is not

    true.

    LIChecs i& the value o& le&t operand is less than or eual to the

    value o& right operand, i& yes then condition becomes true.

    7 LI B9

    is true.

    Operato

    r Description Example

    22Called ogical "D operator. #& both the operands are non-

    5ero, then condition becomes true.

    7 22

    B9 is

    &alse.

    Called ogical GR Gperator. #& any o& the t%o operands is non-

    5ero, then condition becomes true.

    7 B9

    is true.

    J

    Called ogical "G3 Gperator. !se to reverses the logical state

    o& its operand. #& a condition is true then ogical "G3 operator

    %ill mae &alse.

    J7 22

    B9 is

    true.

    Operato

    r Description Example

    2 Binary "D Gperator copies a bit to the result i& it eists in

    both operands.

    7 2 B9

    %ill give

    1*, %hich is

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    5/18

    8888 1188

    Binary GR Gperator copies a bit i& it eists in either

    operand.

    7 B9 %ill

    give 1,

    %hich is

    8811 1181

    NBinary $GR Gperator copies the bit i& it is set in one

    operand but not both.

    7 N B9 %ill

    give A(,

    %hich is

    8811 8881

    OBinary Gnes Complement Gperator is unary and has the

    e&&ect o& E&lippingE bits.

    7O 9 %ill

    give -1,

    %hich is

    1188 8811

    in *Es

    complement

    &orm.

    LL

    Binary e&t hi&t Gperator. 3he le&t operands value is

    moved le&t by the number o& bits speci&ied by the right

    operand.

    LL * %ill

    give *A8

    %hich is

    1111 8888

    KK

    Binary Right hi&t Gperator. 3he le&t operands value is

    moved right by the number o& bits speci&ied by the right

    operand.

    KK * %illgive 1@

    %hich is

    8888 1111

    ssume i& I 8 and B I 1 no% in binary &ormat they %ill be as &ollo%s

    I 8811 1188

    B I 8888 1181

    -----------------

    2B I 8888 1188

    B I 8811 1181

    NB I 8811 8881

    O I 1188 8811

    Operato

    r Description Example

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    6/18

    Iimple assignment operator, ssigns values &rom right sideoperands to le&t side operand

    C I + B%illassignvalue o& + B intoC

    +Idd "D assignment operator, #t adds right operand to thele&t operand and assign the result to le&t operand

    C +I is

    euivalentto C I C+

    -Iubtract "D assignment operator, #t subtracts right operand&rom the le&t operand and assign the result to le&t operand

    C -I iseuivalentto C I C -

    HIMultiply "D assignment operator, #t multiplies rightoperand %ith the le&t operand and assign the result to le&t

    operand

    C HI iseuivalent

    to C I CH

    FIDivide "D assignment operator, #t divides le&t operand %iththe right operand and assign the result to le&t operand

    C FI iseuivalentto C I C F

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    7/18

    integer, %ill return A.

    2 Returns the address o& an variable.2a %ill give actual addresso& the variable.

    H Pointer to a variable.Ha %ill pointer to avariable.

    Q Conditional 'pression#& Condition is true Q 3henvalue $ Gther%ise value

    ategory Operator !ssociativity

    Post&i 79 ST -K . ++ - - e&t to right

    !nary + - J O ++ - - 7type9H 2 si5eo& Right to le&t

    Multiplicative H F < e&t to right

    dditive + - e&t to right

    hi&t LL KK e&t to right

    Relational L LI K KI e&t to right

    'uality II JI e&t to right

    Bit%ise "D 2 e&t to right

    Bit%ise $GR N e&t to right

    Bit%ise GR e&t to right

    ogical "D 22 e&t to right

    ogical GR e&t to right

    Conditional Q Right to le&t

    ssignment I +I -I HI FI

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    8/18

    nested i& statementsou can use one ifor else ifstatement insideanother ifor else ifstatement7s9.

    s%itch statement sitc$statement allo%s a variable to be tested &oreuality against a list o& values.

    nested s%itch statementsou can use one sitc$statement insideanother sitc$statement7s9.

    C programming language provides the &ollo%ing types o& loop to handle looping reuirements. Clic the&ollo%ing lins to chec their detail.

    %oop Type Description

    %hile loopRepeats a statement or group o& statements %hile a givencondition is true. #t tests the condition be&ore eecuting theloop body.

    &or loop 'ecute a seuence o& statements multiple times andabbreviates the code that manages the loop variable.

    do...%hile loopie a %hile statement, ecept that it tests the condition atthe end o& the loop body

    nested loopsou can use one or more loop inside any another %hile, &oror do..%hile loop.

    C supports the &ollo%ing control statements. Clic the &ollo%ing lins to chec their detail.

    ontrol Statement Description

    brea statement

    3erminates the loopor sitc$statement and trans&ers

    eecution to the statement immediately &ollo%ing the loop or

    s%itch.

    continue statementCauses the loop to sip the remainder o& its body and

    immediately retest its condition prior to reiterating.

    goto statement3rans&ers control to the labeled statement. 3hough it is not

    advised to use goto statement in your program.

    loop becomes in&inite loop i& a condition never becomes &alse.

    &unctions

    &unction is a group o& statements that together per&orm a tas.

    3he general &orm o& a &unction de&inition in C programming language is as &ollo%s

    return6type &unction6name7 parameter list 9

    http://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htmhttp://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htmhttp://www.tutorialspoint.com/cprogramming/nested_switch_statements_in_c.htmhttp://www.tutorialspoint.com/cprogramming/c_while_loop.htmhttp://www.tutorialspoint.com/cprogramming/c_for_loop.htmhttp://www.tutorialspoint.com/cprogramming/c_do_while_loop.htmhttp://www.tutorialspoint.com/cprogramming/c_nested_loops.htmhttp://www.tutorialspoint.com/cprogramming/c_break_statement.htmhttp://www.tutorialspoint.com/cprogramming/c_continue_statement.htmhttp://www.tutorialspoint.com/cprogramming/c_goto_statement.htmhttp://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htmhttp://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htmhttp://www.tutorialspoint.com/cprogramming/nested_switch_statements_in_c.htmhttp://www.tutorialspoint.com/cprogramming/c_while_loop.htmhttp://www.tutorialspoint.com/cprogramming/c_for_loop.htmhttp://www.tutorialspoint.com/cprogramming/c_do_while_loop.htmhttp://www.tutorialspoint.com/cprogramming/c_nested_loops.htmhttp://www.tutorialspoint.com/cprogramming/c_break_statement.htmhttp://www.tutorialspoint.com/cprogramming/c_continue_statement.htmhttp://www.tutorialspoint.com/cprogramming/c_goto_statement.htm
  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    9/18

    U

    body o& the &unction

    V

    &unction de&inition in C programming language consists o& afunction headerand afunction body. Were

    are all the parts o& a &unction

    'eturn Type &unction may return a value. 3he return(typeis the data type o& the value the

    &unction returns. ome &unctions per&orm the desired operations %ithout returning a value. #n this

    case, the return6type is the ey%ord void.

    &unction )ame:3his is the actual name o& the &unction. 3he &unction name and the parameter list

    together constitute the &unction signature.

    P arameters: parameter is lie a placeholder. >hen a &unction is invoed, you pass a value to the

    parameter. 3his value is re&erred to as actual parameter or argument. 3he parameter list re&ers to the

    type, order, and number o& the parameters o& a &unction. Parameters are optional that is, a &unctionmay contain no parameters.

    &unction *ody:3he &unction body contains a collection o& statements that de&ine %hat the &unction

    does.

    &unction declarationtells the compiler about a &unction name and ho% to call the &unction. 3he actual

    body o& the &unction can be de&ined separately.

    &unction declaration has the &ollo%ing parts

    return6type &unction6name7 parameter list 9

    >hen a program calls a &unction, program control is trans&erred to the called &unction. called &unction

    per&orms de&ined tas and %hen its return statement is eecuted or %hen its &unction-ending closing brace is

    reached, it returns program control bac to the main program.

    >hile calling a &unction, there are t%o %ays that arguments can be passed to a &unction

    all Type Description

    Call by value

    3his method copies the actual value o& an argument into the&ormal parameter o& the &unction. #n this case, changes madeto the parameter inside the &unction have no e&&ect on theargument.

    Call by re&erence

    3his method copies the address o& an argument into the&ormal parameter. #nside the &unction, the address is used toaccess the actual argument used in the call. 3his means that

    changes made to the parameter a&&ect the argument.

    3here are three places %here variables can be declared in C programming language

    http://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htmhttp://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htmhttp://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htmhttp://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm
  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    10/18

    1. #nside a &unction or a bloc %hich is called localvariables,

    *. Gutside o& all &unctions %hich is called glo+alvariables.

    . #n the de&inition o& &unction parameters %hich is called formalparameters.

    rray

    C programming language provides a data structure called t$e array, %hich can store a &ied-si5e seuential

    collection o& elements o& the same type . rray can be initialised by speci&ying its si5e and the values to each

    inde.

    'g int eamplearrayS@TIU1,*,,A,@V

    Gr int eamplearray STI U,*,,A,@V

    Gr int eample arraySTIA

    n element is accessed by indeing the array name.

    oncept Description

    Multi-dimensional arrays

    C supports multidimensional arrays. 3he simplest &orm

    o& the multidimensional array is the t%o-dimensional

    array.

    Passing arrays to &unctionsou can pass to the &unction a pointer to an array by

    speci&ying the arrayEs name %ithout an inde.

    Return array &rom a

    &unctionC allo%s a &unction to return an array.

    Pointer to an array

    ou can generate a pointer to the &irst element o& an

    array by simply speci&ying the array name, %ithout any

    inde.

    Structures

    Structureis another user de&ined data type available in C programming, %hich allo%s you to combine data

    items o& di&&erent inds. 3o access any member o& a structure, %e use the mem+er access operator ,-..ou

    can pass a structure as a &unction argument in very similar %ay as you pass any other variable or pointer.

    Bit /ields allo% the pacing o& data in a structure. 3his is especially use&ul %hen memory or data storage is

    at a premium. C automatically pacs the above bit &ields as compactly as possible, provided that the

    maimum length o& the &ield is less than or eual to the integer %ord length o& the computer.

    http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htmhttp://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htmhttp://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htmhttp://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htmhttp://www.tutorialspoint.com/cprogramming/c_pointer_to_an_array.htmhttp://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htmhttp://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htmhttp://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htmhttp://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htmhttp://www.tutorialspoint.com/cprogramming/c_pointer_to_an_array.htm
  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    11/18

    !nion unionis a special data type available in C that enables you to store di&&erent data types in the

    same memory location. ou can de&ine a union %ith many members, but only one member can contain a

    value at any given time. !nions provide an e&&icient %ay o& using the same memory location &or multi-

    purpose. 3he memory occupied by a union %ill be large enough to hold the largest member o& the union. 3o

    access any member o& a union, %e use the mem+er access operator ,-..

    3he declaration o& a bit-&ield has the &orm inside a structure

    structU type Smember6nameT%idth V

    Belo% the description o& variable elements o& a bit &ield

    Elements Description

    3ype

    n integer type that determines ho% the bit-&ieldEs value is

    interpreted. 3he type may be int, signed int, unsigned int.

    member6name 3he name o& the bit-&ield.

    >idth3he number o& bits in the bit-&ield. 3he %idth must be less than oreual to the bit %idth o& the speci&ied type.

    3he variables de&ined %ith a prede&ined %idth are called +it fields

    Pointers

    every variable is a memory location and every memory location has its address de&ined %hich can beaccessed using ampersand 729 operator, %hich denotes an address in memory. pointeris a variable

    %hose value is the address o& another variable, i.e., direct address o& the memory location 3o use a pointer

    ,a.%e de&ine a pointer variable ,+.assign the address o& a variable to a pointer and ,c.&inally access the

    value at the address available in the pointer variable. 3his is done by using unary operator /that returns the

    value o& the variable located at the address speci&ied by its operand.

    oncept Description

    C - Pointer arithmetic3here are &our arithmetic operators that can beused on pointers ++, --, +, -

    C - rray o& pointersou can de&ine arrays to hold a number o&

    pointers.

    C - Pointer to pointerC allo%s you to have pointer on a pointer and soon.

    Passing pointers to &unctions in CPassing an argument by re&erence or by address

    both enable the passed argument to be changedin the calling &unction by the called &unction.

    Return pointer &rom &unctions inC

    C allo%s a &unction to return a pointer to local

    variable, static variable and dynamicallyallocated memory as %ell.

    http://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htmhttp://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htmhttp://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htmhttp://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htmhttp://www.tutorialspoint.com/cprogramming/c_return_pointer_from_functions.htmhttp://www.tutorialspoint.com/cprogramming/c_return_pointer_from_functions.htmhttp://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htmhttp://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htmhttp://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htmhttp://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htmhttp://www.tutorialspoint.com/cprogramming/c_return_pointer_from_functions.htmhttp://www.tutorialspoint.com/cprogramming/c_return_pointer_from_functions.htm
  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    12/18

    trings

    3he string in C programming language is actually a one-dimensional array o& characters %hich is terminatedby a nullcharacter EX8E.

    C supports a %ide range o& &unctions that manipulate null-terminated strings

    S-)- &unction 0 Purpose

    1 strcpy,s12 s3.4

    Copies string s* into string s1.

    * strcat,s12 s3.4

    Concatenates string s* onto the end o& string s1.

    strlen,s1.4

    Returns the length o& string s1.

    A strcmp,s12 s3.4

    Returns 8 i& s1 and s* are the same less than 8 i& s1Ls* greater than 8 i& s1Ks*.

    @ strc$r,s12 c$.4

    Returns a pointer to the &irst occurrence o& character ch in string s1.

    strstr,s12 s3.4

    Returns a pointer to the &irst occurrence o& string s* in string s1.

    3ypede&

    3he C programming language provides a ey%ord called typedef, %hich you can use to give a type a ne%

    name

    3he #defineis a C-directive %hich is also used to de&ine the aliases &or various data types similar

    totypedefbut %ith &ollo%ing di&&erences

    3he typedef is limited to giving symbolic names to types only %here as #definecan be used to

    de&ine alias &or values as %ell, lie you can de&ine 1 as G"' etc.

    3he typedefinterpretation is per&ormed by the compiler %here as #definestatements are processed

    by the pre-processor.

    Class

    A class isa data structure with data members and member functions. n objectis an instantiation o& a class. #n

    terms o& variables, a class %ould be the type, and an obYect %ould be the variable.

    Classes are de&ined using either ey%ord class or ey%ord struct, %ith the &ollo%ing synta

    class class6name

    U access6speci&ier61

    member1

    access6speci&ier6*

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    13/18

    member*

    V obYect6names

    n access specifieris either o& the three ey%ords private, public o rprotected. 3hese speci&iers modi&y the

    access rights &or the members that &ollo% them

    private members o& a class are accessible only &rom %ithin other members o& the same class 7or &rom

    their"friends"9.

    protected members are accessible &rom other members o& the same class 7or &rom their "friends"9, but

    also &rom members o& their derived classes.

    Public members are accessible &rom any%here %here the obYect is visible.

    By de&ault the access speci&ier isprivate

    cope resolution operator is used in the de&inition o& &unction, to de&ine a member o& a class outside the

    class itsel&.

    3he only di&&erence bet%een de&ining a member &unction completely %ithin the class de&inition or to Yust

    include its declaration in the &unction and de&ine it later outside the class, is that in the &irst case the &unction

    is automatically considered an inlinemember &unction by the compiler, %hile in the second it is a normal

    7not-inline9 class member &unction. 3his causes no di&&erences in behavior, but only on possible compiler

    optimi5ations

    class can include a special &unction called its constructor, %hich is automatically called %henever a ne%

    obYect o& this class is created, allo%ing the class to initiali5e member variables or allocate storage.

    3his constructor &unction is declared Yust lie a regular member &unction, but %ith a name that matches the

    class name and %ithout any return type not even void. Constructors cannot be called eplicitly as i& they

    %ere regular member &unctions. 3hey are only eecuted once, %hen a ne% obYect o& that class is created.a

    constructor can also be overloaded %ith di&&erent versionsA constructor may be called using either of the four methods

    constructors by enclosing their arguments in parentheses uniform initializationenclosing their arguments inbraces ({}) instead of parentheses (()) with or without

    =.

    constructors with a single parameter can be called using the variable initialization syntax

    Member in a class may be initialled directlyby inserting, be&ore the constructorEs body, a colon 79 and a list

    o& initiali5ations &or class members.

    "ame6o&6classconstructor 7arg 1, arg *9 member17value9, member*7value9 U V

    Pointer to obYects

    expression can be read as*x pointed to by x

    &x address of x

    x.y member yof obect x

    x->y member yof obect pointed to by x

    (*x).y member yof obect pointed to by x(e!uivalent to the previous one)

    x[0] first obect pointed to by x

    x[1] second obect pointed to by x

    x[n] (n+1)th obect pointed to by x

    Macros

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    14/18

    3he Preprocessor is not part o& the compiler, but is a separate step in the compilation process. #n

    simplistic terms, a C Preprocessor is Yust a tet substitution tool and they instruct compiler to do reuired

    pre-processing be&ore actual compilation. >eEll re&er to the C Preprocessor as the CPP.

    ll preprocessor commands begin %ith a pound symbol 7Z9. #t must be the &irst nonblan character, and &or

    readability, a preprocessor directive should begin in &irst column. /ollo%ing section lists do%n all important

    preprocessor directives

    Directive Description

    Zde&ine ubstitutes a preprocessor macro

    Zinclude #nserts a particular header &rom another &ile

    Zunde& !nde&ines a preprocessor macro

    Zi&de& Returns true i& this macro is de&ined

    Zi&nde& Returns true i& this macro is not de&ined

    Zi& 3ests i& a compile time condition is true

    Zelse 3he alternative &or Zi&

    Zeli& Zelse an Zi& in one statement

    Zendi& 'nds preprocessor conditional

    Zerror Prints error message on stderr

    Zpragma#ssues special commands to the compiler, using a standardi5ed

    method

    Prede&ined Macros

    "# C de&ines a number o& macros. lthough each one is available &or your use in programming, theprede&ined macros should not be directly modi&ied.

    5acro Description

    66D3'66 3he current date as a character literal in [MMM DD [ &ormat

    663#M'66 3he current time as a character literal in [WWMM[ &ormat

    66/#'66 3his contains the current &ilename as a string literal.

    66#"'66 3his contains the current line number as a decimal constant.

    663DC66 De&ined as 1 %hen the compiler complies %ith the "# standard.

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    15/18

    3he C preprocessor o&&ers &ollo%ing operators to help you in creating macros

    Macro Continuation 7X9

    macro usually must be contained on a single line. 3he macro continuation operator is used to continue a

    macro that is too long &or a single line.

    'ample Zde&ine message6&or7a, b9 X

    print&7Za [ and [ Zb [ >e love youJXn[9

    3he stringi5e or number-sign operator 7EZE9, %hen used %ithin a macro de&inition, converts a macro

    parameter into a string constant.

    'ampleZinclude Lstdio.hK

    Zde&ine message6&or7a, b9 X

    print&7Za [ and [ Zb [ >e love youJXn[9

    int main7void9

    U

    message6&or7Carole, Debra9

    return 8

    V

    3oen Pasting 7ZZ9

    3he toen-pasting operator 7ZZ9 %ithin a macro de&inition combines t%o arguments. #t permits t%o separate

    toens in the macro de&inition to be Yoined into a single toen

    ZincludeLstdio.hK

    Zde&inetoenpaster7n9print& 7[toen[Zn [ I

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    16/18

    Parameteri5ed Macros

    the ability to simulate &unctions using parameteri5ed macros.

    Zinclude Lstdio.hK

    Zde&ine M$7,y9 779 K 7y9 Q 79 7y99

    int main7void9

    U

    print&7[Ma bet%een *8 and 18 is

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    17/18

    Chapter ##

    Data and /ile tructures

    >hat is data structure Q

    >hy use o& data structuresQ

    3op do%n bottom up

    tructured and modular programming

    tac

    \ueues

    ined list

    ingly lined list

    Doubly lined list

    Deues

    Circular ueues

    Priority ueues

    #n&i to post&i

    #n&i to pre&i

    PostFpre&i evaluation

    orting and searching algorithms

    inear

    Binary

    ort

    #nsertion

    election

    Bubble

    \uic

    Weap

    Merge

    Compleity o& algorithms

  • 7/21/2019 UGC NET COMPUTER SCIENCE.docx

    18/18

    3rees

    B tree

    B+ tree

    vl tree

    B3

    Binary trees

    ]raphs

    dYacency graphs

    DiYstra

    rushals

    panning trees

    Minimal spanning tree

    B&s

    D&s

    Wu&&man code

    Wu&&man tree

    Washing

    bstract data types

    parse matri

    Big o notation

    inear probing