C AND C++ BOOK FINAL STUDENT

Embed Size (px)

Citation preview

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    1/126

    InfoSoft Computer Education

    Building Successful Careers.

    PREFACE

    The computer is a versatile and powerful tool for you and

    your organisation. It enables you to work more effectively and

    to communicate with people around the world.

    The challenges of the 21st century will require new ways

    of thinking about and understanding the complex,

    interconnected and rapidly changing world in which we liveand work. And the IT field of complexity science is providing

    the insights and we need to push our thinking in new

    directions."

    In a rapidly changing technological world, we need

    experts that make it simple for individuals, businesses and

    companies to get the job done without having to wonder how

    it is done.

    This module is prepared with an intention to provide best

    theory knowledge and practical skill persisting these

    Computer Fundamentals. InfoSoft believes in providing up to

    date knowledge and thus the book has been designed by C.A.

    & MCAs keeping in mind an individuals future and

    commercial requirement of the Industry.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 1 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    2/126

    InfoSoft Computer Education

    Building Successful Careers.

    InfoSoft is proud of its R & D Team for developing such a

    Book that enhances the in-built quality of individuals aspiring

    future in the field of computer sector.

    Regards:-

    Research & Development Team InfoSoft

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 2 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    3/126

    InfoSoft Computer Education

    Building Successful Careers.

    Chapter 1: Introduction to Programming

    What Is a Program? To Program Is An Art.

    The Art cannot be learnt without really making art pieces.

    Similarly, Programming cannot be learnt without just by knowing or reading syntax

    of a particular programming language.

    To learn a programming, one must write programs on computer.

    The later part of chapter discusses the two common problem solving methods:

    (1) Algorithm

    (2) Flowchart

    Types Of Programming language:

    (1) Machine Language Programming

    (2) Assembly Language Programming

    (3) Higher Level Language Programming

    What Is Machine Language Programming?

    Machine Language Programming is done using binary codes of underlying Processor. As a

    binary codes (instruction set) varies from Machine to Machine (processor to processor),

    programs Written using binary codes of one machine language are not portable

    Another problem is that machine language is to remember binary codes for each

    instruction.

    For Example, addition of to numbers may be done by using binary codes 010110101. It is

    very difficult for human being to remember such codes for 100 to 200 different operations.

    The solution is to use short name called mnemonics for each binary codes .

    For Example :

    Addition is represented by ADD.

    What is Assembly Language Programming?

    The solution is to use short name called mnemonics for each binary codes .For Example:- Addition is represented by ADD. The program written by using suchmnemonics is known as assembly language program. Assembly language programming

    thus solved problem of remembering binary codes.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 3 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    4/126

    InfoSoft Computer Education

    Building Successful Careers.After Writing Assembly languages program, A Software tool called Assembler is convert

    into machine language program because computer understand only binary languages.

    The advantages of machine and assembly is that programs written using them are faster.

    What is higher level languages?

    The higher level languages are like natural languages as they use phrases of natural

    languages. It make them easy to understand the program development also becomes

    simpler and faster.

    A single statement in a higher level language denotes several operation in a machine or

    assembly languages. There number of higher level languages are available.

    For Example,

    BASIC ,FORTON , COBOL, C, C++, JAVA All are higher level languages.

    A Software tool called compiler is used to translate higher level languages into machinelanguages.

    A separate compiler is required for each language .the BC (Boarland-C) is very popular

    compiler available for PC machine.

    Characteristics Of Higher Level languages:(1) Interactive

    (2) Variety of data type

    (3) Rich set of operators

    (4) Flexible control Structure

    (5) Readability(6) Modularity

    (7) File Handling

    (8) Memory Management

    (9) Procedural Vs. Object-Oriented

    (10) Procedure or event-driven

    (11) Availability of Library Support

    (12) Interface to other Language

    Interactive:

    The major advantage of higher level languages is that they are interactive. They Allow

    user to interface with program through sophisticated input-output statements while

    program is being executed on a system

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 4 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    5/126

    InfoSoft Computer Education

    Building Successful Careers.

    Variety of data type :Data types are broadly categories in to two ways:

    (1)Basic

    (2) User DefinedThe Basic type include data types for characters ,integers, and real

    values.The user defined types include data types for more complex information.

    Example of the user defined data types are array, structure or records,

    unions etc.

    C supports four basic types :(1) char

    (2) int

    (3) float

    (4)double

    Rich set of operators:An operator defines operation to be performed on data. For example ,+ defined

    arithmetic Addition of two numbers.

    Commonly There are Five types of operators:(1) Arithmetic Operators(+,-,*,/)

    (2) Logical Operators(AND,OR,NOT)

    (3) Relational Operators(>,=,

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    6/126

    InfoSoft Computer Education

    Building Successful Careers.The selection statement or decision Statement are very important and used

    for deciding further executing path based on logical condition .

    A decision Making programs Skip a some part of program.

    The C Programming Languages Support Some selection statement or decision

    Statement like if, if-else , Multiple if-else , Switch etc

    For Example,If(x>y)

    Printf (%d ,x);

    Else

    Printf (%d ,y);

    (3) Repetition or looping:Many times, it is necessary that same part of process if the program is to be

    repeated .for this purpose , Languages support three different types of

    looping statement:

    (1)For loop

    (2) While loop(3) Do-While loop

    For Example,

    For(int i=0;i

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    7/126

    InfoSoft Computer Education

    Building Successful Careers.In many Application, input to program is taken form another file which is already

    available

    Or output of program is stored in file for feature use.

    In a C programming ,file management is supported through rich set of the

    file handling functions which allows user to open a file, read from file , write into

    file etc

    Memory Management:The memory management can be supported either through library calls or directly

    using system calls. The C programming Support Memory Management Function in library

    known as . it provides function like malloc() to allocate memory dynamically

    and free() to release the memory.

    In language like c++ and java, memory management is done through operators

    New And Delete.

    Procedural Vs. Object-Oriented:Procedural language emphasize on Procedures, while object oriented languagesemphasize on object oriented principles like object ,classes ,encapsulation ,inheritance

    etc.

    The C is procedural language , While C++ Is Object oriented language Another

    Example of Object oriented languages is that java.

    Procedural Vs. Event driven :In procedure driven programming, Execution is strictly done by calling procedures

    i.e. one procedure call another which may be some procedure.

    For Example, execution of C program begins by calling function main() which acts

    as entry function and calls other function in a program.

    Modern soft wares are GUI(graphical user interface) soft wares which are developed

    using visual languages like Microsoft visual basic, VC++,sun JAVA etc All GUI programs

    are event driven.

    Availability of Library Support:

    The C Compilers are available with rich Supported library function. A very common

    library is standard I/O Which provides functions related to Input/output. It is supported

    through header file #include the other common C libraries are string Maths ,

    Ctype ,etc

    Interface to other Language:Many programming languages supported interface to other language i.e. they

    provide extension to other languages. Such interface or extension is very useful for many

    reasons.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 7 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    8/126

    InfoSoft Computer Education

    Building Successful Careers.For Example, c support interface to assembly languages statement within C

    program itself. It then facilitates to access operating system services directly or some

    critical portion can be written in assembly for efficiency purpose.

    Problem Solving Methods:There Are two types of Problem Solving Methods:

    (1) Algorithm(2) Flowchart

    (1)Algorithm: An algorithm is a step wise solution of program. Algorithm gives a

    sequence of steps which makes complete solution of a programs in hend.

    For, Example: Write a program to find ares of circle.

    So, We find out:

    A=PIE*R*R

    Algorithm:

    Input R

    Compute A=3.14 *R*R

    Print ASTOP

    (2)Flowchart:

    A flow Chart is a Pictorial representation of Algorithm. Flowchart consider

    Some Symbol are as follows:

    Symbol Use

    Start/stop

    Input/output

    Process or Computation

    For Example:- Draw the flowchart to find a area of circle:

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 8 ~

    start

    Input

    Start

    Flow Direction

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    9/126

    InfoSoft Computer Education

    Building Successful Careers.

    Introduction To CC is a relatively small language, but one which it higher level language.

    C's small ,it has some advantage: there's less to learn; there isn't excess baggage in the

    way when you don't need it.(you can give a comment if some part is not used in

    program).It has also be a disadvantage: since it doesn't do everything for you, there's a lot you have

    to do yourself.

    (Actually, this is viewed by many as an additional advantage: anything the language

    doesn't do for you, it doesn't dictate to you, either, so you're free to do that something

    however you want.)

    C is referred to a ``high-level assembly language.''

    Some people think that's an insult, but it's actually a deliberate and significant aspect of

    the language.

    If you have programmed in assembly language, you'll probably find C very natural and

    comfortable (although if you continue to focus too heavily on machine-level details, you'llprobably end up with unnecessarily non portable programs).

    If you haven't programmed in assembly language, you may be frustrated by C's lack of

    certain higher-level features. In either case, you should understand why C was designed

    this way: so that seemingly-simple constructions expressed in C would not expand to

    arbitrarily expensive (in time or space) machine language constructions when compiled.

    If you write a C program simply, it is likely to result in a efficient machine language

    executable. If you find that the executable program resulting from a C program is not

    efficient, it's probably because of something silly you did not because of something the

    compiler did behind your back which you have no control over.

    Feature of C:(1) C is portable(2) C does not certain input/output statements.

    (3) C is modular language.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 9 ~

    A=Pi*R

    Print

    Stop

    Input

    A=Pi*R*

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    10/126

    InfoSoft Computer Education

    Building Successful Careers.

    Example:A programming language is a tool. If you're building a house, and I'm teaching you how to

    use a hammer, and you ask how to assemble rafters and trusses into gables, that's a

    legitimate question, but the answer has fallen that ``How do I use a hammer?'' and into

    ``How do I build a house?''. In the same way, we'll see that C does not have built-infeatures to perform every function that we might ever need to do while programming.

    As mentioned above, C imposes relatively few built-in ways of doing things on the

    programmer. Some common tasks, such as manipulating strings, allocating memory, and

    doing input/output (I/O), are performed by calling on library functions.

    Other tasks which you might want to do, such as creating or listing directories, or

    interacting with a mouse, or displaying windows or other user-interface elements, or doing

    color graphics, are not defined by the C language at all.

    You can do these things from a C program, of course, but you will be calling on services

    which are peculiar to your programming environment (compiler, processor, and operating

    system) and which are not defined by the C standard. Since this course is about portable Cprogramming, it will also be steering clear of facilities not provided in all C environments.

    This aspect of the language which it's fairly pointless to complain about. If you take care

    and pay attention, you can avoid many of the pitfalls. These notes will point out many of

    the obvious (and not so obvious) trouble spots.

    Example:The first example program in C language is:-

    print or display a simple string, and Exit ``hello, world'' program:

    #include

    #include

    main()

    {

    Clrscr();

    printf("Hello, world!\n");

    getch ();

    }

    If you have a C compiler, the first thing to do is figure out how to type this program in and

    compile it and run it and see where its output went. (If you don't have a C compiler yet,

    the first thing to do is to find one.)

    The first two line #include And #include is practically

    boilerplate; it will appear in almost all programs we write. It asks that some definitionshaving to do with the ``Standard I/O Library'' be included in our program; these definitions

    are needed if we are to call the library function printfcorrectly.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 10 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    11/126

    InfoSoft Computer Education

    Building Successful Careers.The second line says that we are defining a function named main. Most of the time, wecan name our functions anything we want, but the function name main is special: it is the

    function that will be ``called'' first when our program starts running.

    The empty pair of parentheses ( And ) indicates that our main function accepts no

    arguments, that is, there isn't any information which needs to be passed in when the

    function is called.The braces { and } surround a list of statements in C. Here, they surround the list of

    statements making up the function main.

    The line

    printf("Hello, world!\n");

    is the first statement in the program. It asks that the function printf be called; printf is a

    library function which prints formatted output. The parentheses surround printf's

    argument list: the information which is handed to it which it should act on. The semicolon

    at the end of the line terminates the statement.

    (printf's name reflects the fact that C was first developed when Teletypes and other

    printing terminals were still in widespread use. Today, of course, video displays are farmore common. printf's ``prints'' to the standard output, that is, to the default location for

    program output to go. Nowadays, that's almost always a video screen or a window on that

    screen. If you do have a printer, you'll typically have to do something extra to get a

    program to print to it.)

    printf's first (and, in this case, only) argument is the string which it should print. The

    string, enclosed in double quotes "", consists of the words ``Hello, world!'' followed by a

    special sequence: \n. In strings, any two-character sequence beginning with the

    backslash \ represents a single special character. The sequence \n represents the ``new

    line'' character, which prints a carriage return or line feed or whatever it takes to end one

    line of output and move down to the next. (This program only prints one line of output, but

    it's still important to terminate it.)

    The second line in the main function is

    Getch();

    In general, a function may return a value to its caller, and main is no exception. When

    main returns (that is, reaches its end and stops functioning), the program is at its end, and

    the return value from main tells the operating system (or whatever invoked the program

    that main is the main function of) whether it succeeded or not. By convention, a return

    value of 0 indicates success.

    This program may look so absolutely trivial that it seems as if it's not even worth typing it

    in and trying to run it, but doing so may be a big (and is certainly a vital) first hurdle. On

    an unfamiliar computer, it can be arbitrarily difficult to figure out how to enter a text filecontaining program source, or how to compile and link it, or how to invoke it, or what

    happened after (if?) it ran. The most experienced C programmers immediately go back to

    this one, simple program whenever they're trying out a new system or a new way of

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 11 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    12/126

    InfoSoft Computer Education

    Building Successful Careers.entering or building programs or a new way of printing output from within programs. As

    Kernighan and Ritchie say, everything else is comparatively easy.

    Howyou compile and run this (or any) program is a function of the compiler and operating

    system you're using. The first step is to type it in, exactly as shown; this may involve using

    a text editor to create a file containing the program text. You'll have to give the file a

    name, and all C compilers (that I've ever heard of) require that files containing C sourceend with the extension .c. So you might place the program text in a file called hello.c.

    The second step is to compile the program. (Strictly speaking, compilation consists of two

    steps, compilation proper followed by linking, but we can overlook this distinction at first,

    especially because the compiler often takes care of initiating the linking step

    automatically.) On many Unix systems, the command to compile a C program from a

    source file hello.c is

    cc -o hello hello.c

    You would type this command at the Unix shell prompt, and it requests that the cc (C

    compiler) program be run, placing its output (i.e. the new executable program it creates)

    in the file hello, and taking its input (i.e. the source code to be compiled) from the filehello.c.

    The third step is to run (execute, invoke) the newly-built hello program. Again on a Unix

    system, this is done simply by typing the program's name:

    hello

    Depending on how your system is set up (in particular, on whether the current directory is

    searched for executables, based on the PATH variable), you may have to type

    ./hello

    to indicate that the hello program is in the current directory (as opposed to some ``bin''

    directory full of executable programs, elsewhere).

    You may also have your choice of C compilers. On many Unix machines, the cc command

    is an older compiler which does not recognize modern, ANSI Standard C syntax. An oldcompiler will accept the simple programs we'll be starting with, but it will not accept most

    of our later programs. If you find yourself getting baffling compilation errors on programs

    which you've typed in exactly as they're shown, it probably indicates that you're using an

    older compiler. On many machines, another compiler called acc or gcc is available, and

    you'll want to use it, instead. (Both acc and gcc are typically invoked the same as cc; that

    is, the above cc command would instead be typed, say, gcc -o hello hello.c .)

    (One final caveat about Unix systems: don't name your test programs test, because

    there's already a standard command called test, and you and the command interpreter

    will get badly confused if you try to replace the system's test command with your own, notleast because your own almost certainly does something completely different.)

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 12 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    13/126

    InfoSoft Computer Education

    Building Successful Careers.Under MS-DOS, the compilation procedure is quite similar. The name of the command you

    type will depend on your compiler (e.g. cl for the Microsoft C compiler, tc or bcc for

    Borland's Turbo C, etc.). You may have to manually perform the second, linking step,

    perhaps with a command named link or tlink. The executable file which the compiler/linker

    creates will have a name ending in .exe (or perhaps .com), but you can still invoke it by

    typing the base name (e.g. hello). See your compiler documentation for complete details;one of the manuals should contain a demonstration of how to enter, compile, and run a

    small program that prints some simple output, just as we're trying to describe here.

    In an integrated or ``visual'' programming environment, such as those on the Macintosh or

    under various versions of Microsoft Windows, the steps you take to enter, compile, and run

    a program are somewhat different (and, theoretically, simpler). Typically, there is a way to

    open a new source window, type source code into it, give it a file name, and add it to the

    program (or ``project'') you're building. If necessary, there will be a way to specify what

    other source files (or ``modules'') make up the program. Then, there's a button or menu

    selection which compiles and runs the program, all from within the programmingenvironment. (There will also be a way to create a standalone executable file which you

    can run from outside the environment.) In a PC-compatible environment, you may have to

    choose between creating DOS programs or Windows programs. (If you have troubles

    pertaining to the printf function, try specifying a target environment of MS-DOS.

    Supposedly, some compilers which are targeted at Windows environments won't let you

    call printf, because until you call some fancier functions to request that a window be

    created, there's no window for printf to print to.) Again, check the introductory or tutorial

    manual that came with the programming package; it should walk you through the steps

    necessary to get your first program running.

    Data Types:There are only a few basic data types in C. The first ones we'll be encountering and using

    are:

    char (a character)

    int (an integer, in the range -32,767 to 32,767 )

    long int (a larger integer (up to +-2,147,483,647))

    float( a floating-point number )

    double (a floating-point number, with more precision and perhaps greater range

    than float )

    C Character set And Tokens:

    Character :

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 13 ~

    http://www.eskimo.com/~scs/cclass/notes/sx2a.htmlhttp://www.eskimo.com/~scs/cclass/notes/sx2a.html
  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    14/126

    InfoSoft Computer Education

    Building Successful Careers.The Words(tokens) and statement used in any language are from their basic characterset. For Example, the word used in English language are formed from its alphabet having

    26 different symbols i.e. A,B,C..,Z or a,b,c..,z the character in the c are divided into

    four categories.

    (1) Letters or alphabets

    (2) Digits(3) Special character

    (4) White Spaces

    The letters include the uppercase(A,B . . . .,Z) And lowercase (a,b,c . . . .,z) Alphabets of

    Engilsh Language. The Digits include the 0,1,2,3. . .,9.

    The C also use special character like ;(semicolon),(single quotes),(double quotes),+,-,*,/,

    %,>,= etc. for different purposes.

    The white space are used to separate the word or tokens. they are blank, tab or newline.

    Tokens :

    The character together makes special symbol or word known as token. The Example of

    the token are the words used to define data types like int , float ,etcSimilarly anyoperator i.e. +,-,*,/,%,> etc. are also tokens. The constants used in program like 12,12.67

    are also tokens.

    Keywords:The keywords are the special words used in C programming having specific meaning .the

    meaning of the key word are not changed. They are also known as reserve words. The

    keyword can not be used as identifiers.

    Some Example of Keywords are:

    (1) Auto

    (2) Break(3) Case

    (4) Int

    (5) Struct

    (6) Long

    (7)Float

    (8) If(9) For

    (10) Whil

    e

    (11) Got

    o(12) Voi

    d

    (13) Stat

    ic

    (14) Do

    (15) Switch

    Identifiers:The identifiers are user defined names used in programs for providing name to

    variables, arrays

    And function . The identifiers are made up of letters(upper case and lower case) ,digits and

    underscore .

    The C is case sensitive language.

    For Example: max And Max are not same.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 14 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    15/126

    InfoSoft Computer Education

    Building Successful Careers.Constants:

    The constants are the values Which Are never change.

    The C is Use Following types of constants:

    (1) Numeric Constants.

    a. Integer

    b. Real

    (2) Non- Numeric Constants.

    a. Character

    b. String

    Integer:Integer constants represent the Whole Number.it is used 0 to9 and option sign -

    and + before number.

    For Example,

    123-23

    0

    +38

    Real:Real constants represent the number with fractional parts i.e. digits after decimal

    Part

    For Example,

    0.0008

    -0.158

    +581.5568.

    .58

    Character:Character constants represent single character and are always enclosed in single

    quotes.

    For Example,A

    a

    ?

    :8

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 15 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    16/126

    InfoSoft Computer Education

    Building Successful Careers.Within character and string constants, the backslash character \ is special, and is

    used to represent characters not easily typed on the keyboard or for various reasons not

    easily typed in constants. The most common of these ``character escapes'' are:

    \n a `newline'' character

    \b a backspace

    \r a carriage return (without a line feed)\' a single quote (e.g. in a character constant)

    \" a double quote (e.g. in a string constant)

    \\ a single backslash

    \t a horizontal tab

    \0 a Null

    For example, "he said \"hi\"" is a string constant which contains two double quotes, and '\''

    is a character constant consisting of a (single) single quote. Notice once again that the

    character constant 'A' is very different from the string constant "A".

    String:A string is represented in C as a sequence or array of characters. (We'll have more to say

    about arrays in general, and strings in particular, later.) A string constant is a sequence of

    zero or more characters enclosed in double quotes

    For Example:

    "1992"

    "hello, world"

    "this is a test"

    D (Not that D And D Are not same)

    Variable: The variables represent the quantities which changes with time. Each variable

    identified by a unique identifier in a program called variable name

    For Example, length , area , max etc Are Variable

    Variable Declarations

    Informally, a variable (also called an object) is a place you can store a value. So that youcan refer to it unambiguously, a variable needs a name. You can think of the variables in

    your program as a set of boxes or cubbyholes, each with a label giving its name; you

    might imagine that storing a value ``in'' a variable consists of writing the value on a slip of

    paper and placing it in the cubbyhole.A declaration tells the compiler the name and type of a variable you'll be using in your

    program. In its simplest form, a declaration consists of the type, the name of the variable,

    and a terminating semicolon:

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 16 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    17/126

    InfoSoft Computer Education

    Building Successful Careers.char c;

    int i;

    float f;

    You can also declare several variables of the same type in one declaration,

    separating them with commas:

    int i1, i2;Later we'll see that declarations may also contain initializers, qualifiers and storage

    classes, and that we can declare arrays, functions, pointers, and other kinds of data

    structures.

    The placement of declarations is significant. You can't place them just anywhere (i.e. they

    cannot be interspersed with the other statements in your program). They must either be

    placed at the beginning of a function, or at the beginning of a brace-enclosed block of

    statements (which we'll learn about in the next chapter), or outside of any function.

    Furthermore, the placement of a declaration, as well as its storage class, controls several

    things about its visibilityand lifetime, as we'll see later.

    Operators:There are five types of Operators:

    (1) Arithmetic Operators

    (2) Relational Operators

    (3) Logical Operators

    (4) Assignment Operators

    (5) Inc/Dec Operators

    (6) Ternary Operators

    (7) Bit-Wise Operators

    (8) Comma Operator(9) Sizeof Opearator

    Arithmetic Operators:The C include following types of Arithmetic Operators:

    Operator

    s

    Meaning

    +

    -

    *

    /%

    Addition or Unary

    plus

    Subtraction or Unary

    minusMultiplication

    Division

    Modulo division

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 17 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    18/126

    InfoSoft Computer Education

    Building Successful Careers.Relational Operators:When There are a comparison of age of two person, comparison of Marks of two Students

    etc

    Like (a>b),(3>2).

    The C include following types of Relational Operators:

    Operators

    Meaning

    >

    >=

    Y)

    Max=x;Else

    Max=y;

    Is written in using ternary operator as follows.

    Max=(x~

    Bit-Wise AND

    Bit-Wise OR

    Bit-wise Exclusive OR

    Left shift

    Right shift1s Complement

    Comma Operator:

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 20 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    21/126

    InfoSoft Computer Education

    Building Successful Careers.The comma operator is used to combine the multipal statement which are related to a

    single statement. Consider as Following:

    X=5;

    Y=10;

    Z=X+Y;

    These Statement Assign the values to X and Y and then stores their sum into Z.they canbe written using comma operator as follows:

    Z=(X=5, Y=7, X+Y);

    Size Of Operator:

    The size Of Opertor is used to find the size of the operand occupied in memory in bytes.

    For Example,

    m= sizeof(int);

    will make m=2 as integer occupies 2 bytes in memory.

    Printf:Printf's name comes from print Formatted. It generates output under the control of aformat string (its first argument) which consists of literal characters to be printed and also

    special character sequences--format specifiers--which request that other arguments be

    fetched, formatted, and inserted into the string. Our very first program was nothing more

    than a call to printf, printing a constant string:

    printf("Hello, world!\n");

    Our second program also featured a call to printf:

    printf("i is %d\n", i);

    In that case, whenever printf ``printed'' the string "i is %d", it did not print it verbatim; it

    replaced the two characters %d with the value of the variable i.

    There are quite a number of format specifies for printf. Here are the basic ones :

    %d print an int argument in decimal

    %ld print a long int argument in decimal

    %c print a character

    %s print a string

    %f print a float or double argument

    %e same as %f, but use exponential notation

    %g use %e or %f, whichever is better

    %o print an int argument in octal (base 8)%x print an int argument in hexadecimal (base 16)

    %% print a single %

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 21 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    22/126

    InfoSoft Computer Education

    Building Successful Careers.It is also possible to specify the width and precision of numbers and strings as they are

    inserted (somewhat like FORTRAN format statements); we'll present those details in a

    later chapter. (Very briefly, for those who are curious: a notation like %3d means to print

    an int in a field at least 3 spaces wide; a notation like %5.2f means to print a float or

    double in a field at least 5 spaces wide, with two places to the right of the decimal.)

    To illustrate with a few more examples: the call

    printf("%c %d %f %e %s %d%%\n", '1', 2, 3.14, 56000000., "eight", 9);

    would print

    1 2 3.140000 5.600000e+07 eight 9%

    The call

    printf("%d %o %x\n", 100, 100, 100);

    would print

    100 144 64

    Successive calls to printf just build up the output a piece at a time, so the calls

    printf("Hello, ");printf("world!\n");

    Would also print Hello, world! (on one line of output).

    Earlier we learned that C represents characters internally as small integers corresponding

    to the characters' values in the machine's character set (typically ASCII). This means that

    there isn't really much difference between a character and an integer in C; most of the

    difference is in whether we choose to interpret an integer as an integer or a character.

    printf is one place where we get to make that choice: %d prints an integer value as a

    string of digits representing its decimal value, while %c prints the character corresponding

    to a character set value. So the lines

    char c = 'A';int i = 97;

    printf("c = %c, i = %d\n", c, i);

    would print c as the character A and i as the number 97. But if, on the other hand, we

    called

    printf("c = %d, i = %c\n", c, i);

    we'd see the decimal value (printed by %d) of the character 'A', followed by the character

    (whatever it is) which happens to have the decimal value 97.

    You have to be careful when calling printf. It has no way of knowing how many arguments

    you've passed it or what their types are other than by looking for the format specifiers in

    the format string. If there are more format specifiers (that is, more % signs) than there arearguments, or if the arguments have the wrong types for the format specifiers, printf can

    misbehave badly, often printing nonsense numbers or (even worse) numbers which

    mislead you into thinking that some other part of your program is broken.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 22 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    23/126

    InfoSoft Computer Education

    Building Successful Careers.Because of some automatic conversion rules which we haven't covered yet, you have a

    small amount of latitude in the types of the expressions you pass as arguments to printf.

    The argument for %c may be of type char or int, and the argument for %d may be of type

    char or int. The string argument for %s may be a string constant, an array of characters,

    or a pointer to some characters (though we haven't really covered strings or pointers yet).

    Finally, the arguments corresponding to %e, %f, and %g may be of types float or double.But other combinations do notwork reliably: %d will not print a long int or a float or a

    double; %ld will not print an int; %e, %f, and %g will not print an int.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 23 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    24/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 24 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    25/126

    InfoSoft Computer Education

    Building Successful Careers.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 25 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    26/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 26 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    27/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 27 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    28/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 28 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    29/126

    InfoSoft Computer Education

    Building Successful Careers.

    Chapter 2 : Decision Making.

    If Else Statement:

    Introduction:The Important and essential part of a programming language is their control structures.

    The programming language is their control structure. The important control structure are:

    (1)Sequence

    (2)Decision

    (3)Looping

    In This chepter. We will study the decision making statement of c programming

    language.

    if-else multiple If-else

    switch

    goto

    If-else Statement :-

    The formet of if-else is given below.

    if(condition)

    statement1 ;else

    stement2 ;

    Here, the condition is represented by relation or logical expression.if condition is true,

    then statment1 is executed and if Condition is false, then statement2 is executed.

    The statement1 and/or statment2 can be either single statement or computed

    statement i.e. group of statements. the else part in if-else statement is optional. The ifstatement without else looks like:

    if(condition)statement ;

    In this case ,if condition is true, then statement is executes, otherwise control is

    transferred to the statement that follows the ifstructure.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 29 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    30/126

    InfoSoft Computer Education

    Building Successful Careers.Fig. shows the execution the execution of the if else statement.

    Switch Statement :

    The Switch in C is a multi-choice statement.it provides one choice for each value of

    variable or expression. The block of statements associated with the choice whose value issame as the value of variable or expression is exeqted.the format of the switch statement

    is given below:-

    Switch(variable or expression )

    {

    case lable1: statement_block1;

    break ;

    case lable2: statement_block2;

    break ;

    .

    .

    .default : default_block ;

    }

    Here, label1 , label2 . . .show the possible values of the variable or expression. Foreach value of variable or expression, one statement block is provided. For Example

    statement block1 is associated with the lable1. The statement block consist of one

    or more statement. After every statement block, there is breakwhich send control to thenext statement after switch statement. The last choice is default which is chosen when

    value of variable or expression does not match to any of the labels.in this case, the defaultstatement is executed.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 30 ~

    Condition

    Statment1 Statment2

    T F

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    31/126

    InfoSoft Computer Education

    Building Successful Careers.

    Goto Statement:

    The goto statement in C programming is used to transfer the control unconditionally from

    one part of the program to another part of the program. The c is structure programminglanguage where the use of the goto statement is very dangerous because it can easily

    violate the rules of structured programming.

    The format of the goto Statement is as follows:

    goto label ;

    Here, label is the label to the statement to which goto transfer control. The meaning isthat the execution begins from this labeled or target statement.

    Following format shows the two possible use of goto statement :forward and

    backward.

    In forward, the target statement comes after the goto, while in backward target

    statement comes before goto statement. Note that label must be followed bycolon(:) at the target statement.

    (a) Goto forward

    goto label;

    . . . . .

    . . . . .

    Label :statement;

    (b) Goto backward

    Label;

    Statement ;

    . . . . .

    . . . . .

    goto label;

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 31 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    32/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs:-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 32 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    33/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 33 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    34/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 34 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    35/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 35 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    36/126

    InfoSoft Computer Education

    Building Successful Careers.

    Chapter 3 : Looping

    Introduction:

    A Looping is an important control structure in higher level language Programming. Theobjective of looping is to provide repetition of the part of the program i.e. block of

    statement in a program , number of times. the looping statement provided by the higher

    level language gives necessary control to repeat the block of statements. Consider the

    following program segment which makes sum of 1 to 10.

    sum = 0;

    I = 1;

    Sum = sum + I ;

    i = i + 1;

    if ( i

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    37/126

    InfoSoft Computer Education

    Building Successful Careers.

    (a) Entry Control (b) Exit Control

    The C language support Following types of looping structure :

    (1) While

    (2) Do-while

    (3) For

    While Loop:

    We have already used the while loop in many programs. The structure of while ts as

    follows.

    While(condition )

    {body ;

    }

    Here , the condition is evaluated first and if it is true, then the statement in the body of

    the loop executed . after executing body, the condition is evaluated first and if it true body

    is executed again this process is repeated as long as the condition is true . the controlmoves out once the condition is false.it is not necessary to enclose body of loop in pair of

    braces { } if body contain only one statement Fig. Shows The Flowchart of While-loop :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 37 ~

    condition

    body

    body

    condition

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    38/126

    InfoSoft Computer Education

    Building Successful Careers.

    For Example, The While loop can be Written as a follows to print 1 to 10.

    . . . . .

    . . . . .

    I=1;

    While (I

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    39/126

    InfoSoft Computer Education

    Building Successful Careers.

    The format of the do - While is given as follows :-

    do

    {

    Body;

    }

    While(condition);

    In do-while ,first the body is executed and then the condition is checked. If the Condition is

    true then body is true body is executed again. The body executed again and again as long

    as condition is true. The control out of the loop, once the condition become false. The Do-

    while loop is an Exit control loop as the condition is checked after executing the body. This

    ensures that body is executed at least once even if the condition is false first time..Fig

    Shows the flow chart of the do-While Statement.

    The Example of theDo while loop Are:

    . . . . .

    . . . . .Do

    {

    Printf(Enter the number);

    Scanf(%d,&n);

    }

    While(num

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    40/126

    InfoSoft Computer Education

    Building Successful Careers.The format offor Loop is as follows:

    for(;;)

    {

    body ;

    }

    Here , is the initialization of variables by initial values. theis executes only

    once when we enter the loop ,if contains more than variables for initialization, they

    must be separated by commas. The is the condition and if it is true, then controlenter into the body of the loop and if it is true, then control enters into the body of the

    loop and executes the statements in the body. After the executing the body of the

    loop , is executed which contains statement which modifies the variable.

    The is then evaluated with the modified values and if it is true, then body is again

    executed. This process is repeated as long as condition represented by is true.Once it becomes false, control comes out of the loop and executing is resumed from next

    statement after for loop, if body of for loop. If the body of for loop contains only onestatement, then pair of brace { } is not required. Fig shows the flow chart of for loop.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 40 ~

    e2?

    body

    e3

    e1

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    41/126

    InfoSoft Computer Education

    Building Successful Careers.Nesting of Loop:

    Loop inside loop is called nested loop For Example:-

    . . . . . .

    . . . . . .

    For( )

    {

    . . . . .

    . . . . .

    For( )

    {

    . . . . .

    . . . . .

    . . . . .

    }. . . . .

    . . . . .

    }

    . . . . .

    . . . . .

    For Example:#include

    #include

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 41 ~

    Inner loop Outer loop

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    42/126

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    43/126

    InfoSoft Computer Education

    Building Successful Careers.{

    if(i % j == 0)

    break;

    if(j > sqrt(i))

    {

    printf("%d\n", i);

    break;

    }

    }

    }

    getch();

    }

    The outer loop steps the variable i through the numbers from 3 to 100; the code tests tosee if each number has any divisors other than 1 and itself. The trial divisor j loops from 2

    up to i. j is a divisor of i if the remainder of i divided by j is 0, so the code uses C's

    ``remainder'' or ``modulus'' operator % to make this test. (Remember that i % j gives the

    remainder when i is divided by j.)

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 43 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    44/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 44 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    45/126

    InfoSoft Computer Education

    Building Successful Careers.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 45 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    46/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 46 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    47/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 47 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    48/126

    InfoSoft Computer Education

    Building Successful Careers.

    Chapter 4 : Arrays And String

    Array is an set of element.

    Arrays is useful concept for representing a group of similar values of under a single name.

    thus, array can be defined simply as the group of values of same type. Whenever a

    particular value from a group is needed,we can access it by name of group i. e.Arrays

    name and the position of the value in the group . For Example, group of Markes of ten

    student. If name of groyp is marks,then the mark of 6th student is accessed by

    mentioning the position 6 in the group marks

    There three types of arrays:

    (1) One dimensional arrays

    (2) Two dimensional arrays(3) Multi dimensional arrays

    One dimensional Arrays:

    The array represents a group of similar values under a single name. The format of the one-

    dimensional array is as follows:

    For Example,int a[10];

    Where a is the name of array of 10 integers.

    For Example:

    So far, we've been declaring simple variables: the declaration

    int i;

    declares a single variable, named i, of type int. It is also possible to declare an arrayofseveral elements. The declaration

    int a[10];declares an array, named a, consisting of ten elements, each of type int. Simply speaking,an array is a variable that can hold more than one value. You specify which of the several

    values you're referring to at any given time by using a numeric subscript. (Arrays in

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 48 ~

    type arrary_name

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    49/126

    InfoSoft Computer Education

    Building Successful Careers.programming are similar to vectors or matrices in mathematics.) We can represent the

    array a above with a picture like this:

    In C, arrays arezero-based: the ten elements of a 10-element array are numbered from 0

    to 9. The subscript which specifies a single element of an array is simply an integer

    expression in square brackets. The first element of the array is a[0], the second element

    is a[1], etc. You can use these ``array subscript expressions'' anywhere you can use thename of a simple variable, for example:

    a[0] = 10;

    a[1] = 20;

    a[2] = a[0] + a[1];

    Notice that the subscripted array references (i.e. expressions such as a[0] and a[1]) can

    appear on either side of the assignment operator.

    The subscript does not have to be a constant like 0 or 1; it can be any integral expression.

    For example, it's common to loop over all elements of an array:

    int i;

    for(i = 0; i < 10; i = i + 1)

    a[i] = 0;

    This loop sets all ten elements of the array.

    Arrays are a real convenience for many problems, but there is not a lot that C will do with

    them for you automatically. In particular, you can neither set all elements of an array at

    once nor assign one array to another; both of the assignments

    a = 0; /* WRONG */and

    int b[10];

    b = a; /* WRONG */

    are illegal.To set all of the elements of an array to some value, you must do so one by one, as in the

    loop example above. To copy the contents of one array to another, you must again do so

    one by one:

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 49 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    50/126

    InfoSoft Computer Education

    Building Successful Careers.int b[10];

    for(i = 0; i < 10; i = i + 1)

    b[i] = a[i];Remember that for an array declared

    int a[10];there is no element a[10]; the topmost element is a[9]. This is one reason that zero-based

    loops are also common in C. Note that the for loop

    for(i = 0; i < 10; i = i + 1)

    ...Does just what you want in this case: it starts at 0, the number 10 suggests (correctly)

    that it goes through 10 iterations, but the less-than comparison means that the last trip

    through the loop has i set to 9. (The comparison i

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    51/126

    InfoSoft Computer Education

    Building Successful Careers.

    Multi-dimensional array:

    The C programming also allows the use of the three or more dimensional array which are

    known as multi-dimensional arrays. The general format for declaration of the multi-

    dimensional arrays is as follows.

    Where d1 is the 1th dimension. for Example,

    int sales[3][4][12];

    What is a String?

    A string is a sequence of characters. string is useful to store the nin-numerical quantities

    such as name,address etc. The C stores the characters ASCII values in memory.Each of the

    spical character called null character and denoted as \0 NULL For Example,

    String Hello is stored as :

    String Function:

    The c does not support operation on string directly except read and print.to perform such

    operations such operations either user should write the required code or uses the library

    functions

    Provided by #include.Some function of the string are:

    Function Meaning

    Strlen(s)

    Strrev(s)

    Strcpy(s1,s2)

    Strcat(s1,s2)Strcmp(s1,s2)

    Computes length of string s

    Reverse the string s

    Copies string s2 into s1

    Concate s2 at the end of s1Compares s1 and s2. Returns 0 if

    s1=s2,returns positive

    If s1>s2 and returns negative if s1

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    52/126

    InfoSoft Computer Education

    Building Successful Careers.

    For Example,

    Since C never lets us assign entire arrays, we use the strcpy function to copy one string to

    another:

    #include

    char string1[] = "Hello, world!";

    char string2[20];

    strcpy(string2, string1);

    The destination string is strcpy's first argument, so that a call to strcpy mimics an

    assignment expression (with the destination on the left-hand side). Notice that we had to

    allocate string2 big enough to hold the string that would be copied to it. Also, at the top of

    any source file where we're using the standard library's string-handling functions (such as

    strcpy) we must include the line

    #include

    Which contains external declarations for these functions.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 52 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    53/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 53 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    54/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 54 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    55/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 55 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    56/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 56 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    57/126

    InfoSoft Computer Education

    Building Successful Careers.

    Chapter 5 : Function

    Function:

    A Function is one form of the sub=program or subroutine.

    Function itself is selfcontained program which implements a small but independent

    task.

    It is not an independent program,but it works as a part of program.

    Defining function:

    The format of function is as follows :

    Return_type name(argument){

    Body;

    }

    Here return_type shows the type of value function returns. If function does not

    return any value,but returns only control, then the return_type is void. the name

    is the name of the function.

    Return statement:

    The format of the return statement is as follows.Return;

    OrReturn(expression);

    The first form of the return does not return any value, but returns only control to the

    caller. Whene return type of the function is void and we want to return control from the

    middle of the function, this from is very useful.

    Void fun()

    {

    . . . . .

    . . . . .

    If( . . . )

    Return;

    . . . .

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 57 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    58/126

    InfoSoft Computer Education

    Building Successful Careers.. . . .

    }

    FOR EXAMPLE :

    #include#include

    Int main(int ,int);

    Void main()

    {

    Int x ,y,z;

    Printf (enter values of x and y);

    Z=max(x ,y)

    Scanf(%d /n z);}

    Int max(int a, int b)

    {

    Return(a > b ? a : b);

    }

    Whene function returns some vaiue , we use the seconed form of the return. In this

    example the function max() returns integer

    Return(a>b?a:b);

    Is written , where a>b?a:b finds the maximua ofa and b which is returned by return

    statement.

    Call by value

    When arguments passed by a value , the calls fuctions creates a new variables of the

    same type as argument and copies the actual argument passed from the main value into

    it, which is called dummy argument. So by using call by value,duplicate copy of original

    variable is created, which will take space in the memory,so it will waste the space.

    For example , call by value method

    Main()

    {

    Int a,b,c,sum;

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 58 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    59/126

    InfoSoft Computer Education

    Building Successful Careers.!

    !

    Sum=calsum(a,b,c);

    !

    !

    }Int calsum(intx,inty,intz)

    {

    !

    !

    !}

    Call by reference

    Call by reference method,from the main function , instead of passing a value ,its adderssis mpassed and at calls function receive the address by using pointer varisble which will

    gives the value at that address,so no duplication occurs. Soc save the space in the

    memory will using Call by reference method.

    For example , Call by reference method:

    Main()

    {

    Int a,b,c;

    !!

    Sum=calsum(&A,&b,&c);

    !

    !}

    Int calsum(int*x ,int*y ,int*z)

    {

    !

    !

    !}

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 59 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    60/126

    InfoSoft Computer Education

    Building Successful Careers.

    Function and Array :

    The array can be passed to function as argument as like individual variables like a char ,

    an integer , a double etc. The array can be passed to function by writing oniy array name

    in function call.

    Following function call shows how an array is passed as function argument.

    Z=max_n(a,n);

    In this function call , a is an array of n integer. The array a is passed as first argument

    while the size of the array is passed as second argument to the function max_n0. The

    function max_n0 is written as follows.

    Int max_n(int v[], int size)

    {

    Int I,max;

    Max=v[0];for(i=0;i

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    61/126

    InfoSoft Computer Education

    Building Successful Careers.Where file_name is the name of file to be included.

    Macro substitution: Macro substitution means to replace the macro by the macro string.

    Macro start with the #defines. The simple macro is defined as follows.

    #define macro_name string

    Conditional inclusion: The conditional inclusion is used to avoid the inclusion the

    inclusion of the same file more than once in a program.

    For example , the header file mts.h is written as follows.

    #include MTS

    #define MTS

    . . . .

    . . . .

    /* content of the mts.h */

    . . . .. . . .

    #endif

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 61 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    62/126

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    63/126

    InfoSoft Computer Education

    Building Successful Careers.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 63 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    64/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 64 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    65/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 65 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    66/126

    InfoSoft Computer Education

    Building Successful Careers.

    Chapter 6 : Poniter

    Introduction:

    Poniter are very useful in accessing memory outside the function, passing function

    parameters to allow two-way communication between calling and called functions,

    dynamic allocation of memory for various simple to compex data structures, hardware

    device programming etc

    Every variable declare in C program is given space in computer memory according to its

    size.For Example, An integer variable occupies two bytes as it is 16-bit in size.

    What is pointer ?

    A pointer is also a variable Which stores address of another variable. Once you get theaddress of variable in pointer, it can be used for different purpose like to access the

    variable value at that address , to increment it two that the address of next element if

    original variable is of array type etc the meaning is that ,once pointer is initialize by

    address of some variable , pointer can be used for our advantage. Before a pointer is

    used , it must be first declared and then initialized. A pointer variable is declared as

    follows:-

    In above , following are important

    (1) Ptr_type denotes type of pointer and is data type of the variable to which

    pointer can point.

    (2) * denotes that it is pointer variable .(3) Pointer_nameis name of pointer variable which can be any valid identifier .

    As an Example,

    int *ptr;

    declares pointer variable with name ptr of type integer. Type of pointer means

    variable type to which pointer pointes. Here,ptr is of type integer means it can

    point to integer variable only.Following are some more Examples of pinter declaration:-

    float *f; // f is pointer to float.

    long int * l; //l is pointer to long integer.

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 66 ~

    Ptr_type

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    67/126

    InfoSoft Computer Education

    Building Successful Careers.char *c; // C is pointer to character.

    Command Line Argments:

    Program can also accept input values through command line arguments when it is started.

    for Example,Whwn turbo C started as :

    C:\tc>tc test.c

    On MS-DOS,tc is a program which takes test.c as command line argument and when tc

    window opens ,it opens file test .c immediately. another example of command line

    argument is DOS copy command written as follows:

    C:\>copy file.c file2.c

    Where copy command takes two files names, source file name (file1.c) and

    destination file name(file2.c) as arguments from command line. In order to pass input to C

    program through command line argument s,we have to use arguments or parameters to

    main ( ) is the first function call when C program start it has two arguments shown below:Void Main (int argc,char *argv[ ] )

    The first argument is args knows as arguments count which tells how many

    arguments are passed.the second argument argv which is array of character pointer

    s.pointers pointing to argument string.in case of Example of DOS copy command ,argc=3

    and argv is as follows:

    For example,

    Write a program which simply displays(echoes) its arguments.\* write this program in file echo.c */

    #include

    #includeVoid main(int argc, char *argv[] )

    {

    Int I;

    For( i=1; i

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    68/126

    InfoSoft Computer Education

    Building Successful Careers.Printf(%s,argv[i]);

    }

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 68 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    69/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 69 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    70/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 70 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    71/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 71 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    72/126

    InfoSoft Computer Education

    Building Successful Careers.

    Chapter 7 : Structures

    Introduction:

    Structure is a collection of different types of variables under a single name which

    is used to represent logically related data pertaining to same entity or object.

    The structure is a very powerful and important tool which fulfills requirements of

    complex problems.

    A simple example of a structure is information about employees in payrool or

    information about students.

    Structure allocates separate memory for each of its variables, while union

    allocates memory for only one and shared by al of its members.Hence,union is

    not a group of variables, but it is a variable,which can take different type.

    Defining structures and structure variables :

    A structureis a group of variables of different types.The general format todefine a structure is given as:

    struct struct_tag

    {

    data_type member_variable1;

    data_type member_variable2;

    .

    .};

    In above , world struct is a reserve word and used to defined a structure template.

    Struct-tag is an optional tag which provides type name to the structure and usefulincreation of variables of the structure type. The member of structures and enclosed in pair

    of { } .Each member consist of a type and variable name followed by semicolon (;). the

    definition is ended with semicolon (;) . Note that such structure definition just declares a

    template or prototype of a structure varicble,but does not occupy any

    memory.Hence,Word declaration is more appropriate.

    Consider following Example of a structure ,defining a student record:-

    Struct student

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 72 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    73/126

    InfoSoft Computer Education

    Building Successful Careers.{

    Int roll_no;

    Char name[20];

    Int age;

    } ;

    In above example, word student is representing a tag. There are three members as :

    (1) Roll number,integer type

    (2) Name of student, string type

    (3) Age in years,integer type

    Accessing structure members:

    Member of a structure can be accessed and used individually in a problem. Following

    syntax is used to access thse members of a structure.

    Structure_name is the name of structure variable and used member_name is the name of

    variable which is member of structure member operator.(dot) conntects member name

    to structure name.

    For example,

    S1.ageDenotes value of variable age of a structure of variable s1 which is of type student.

    Same way s2.age denotes age value associated with student s2. The s1.age ors2.sgeare just like any other integer variable and can be used freely in expression. The following

    statement assigns 20 to s1.age

    S1.age =20;

    The roll-number of student s1 can be read from keyword as

    Scanf(%d,&s1.age);

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 73 ~

    Structure_name.membe

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    74/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 74 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    75/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 75 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    76/126

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    77/126

    InfoSoft Computer Education

    Building Successful Careers.

    Programs :-

    RAOPURA: -112 -113, Sunrise Point, Nr. Hanuman Temple, Opp Raopura Bus Stop, Raopura.

    SAYAJIGUNJ: - 2nd Floor, Nautilus, Opp. Hotel P.M. Regency, above Mahavir Ice - Cream.

    ELLORA PARK:- Chandra Chaya, Above SBI, Opp Vidhya Vikas School, Nr. Cash N Carry.

    0265 6507526 / 9227160174 / 9227853100

    ~ 77 ~

  • 8/8/2019 C AND C++ BOOK FINAL STUDENT

    78/126

    InfoSoft Computer Education

    Building Successful Careers.

    Chapter 8 : File Management

    File Management function :

    The file management function are defined in the header file . They are availablein the program by including the header file . Table gives the list of the file

    management function which are common