Getting Started With C Langage

Embed Size (px)

Citation preview

  • 8/7/2019 Getting Started With C Langage

    1/20

    C 1

    L a n gC

  • 8/7/2019 Getting Started With C Langage

    2/20

  • 8/7/2019 Getting Started With C Langage

    3/20

    C 3

    Machine language

    The programs or instructions written in this language are in the form of binary

    strings of (1) and (0) and these are called the binary code. It is the fundamental

    language. The programs written in this language are executed directly by the

    computer. The executing speed is very fast in machine language. There exists a

    one to one correspondence.

    Assembly language

    Assembly language is similar to machine language in the sense that in most of

    cases between the machine language instruction and the assembly language

    instruction. However, instead of binary codes one may use symbolic codes in the

    assembly language and obviously this is an advantage. For example, one might

    use the symbolic code ADD instead of a binary code say 00011, to may also use

    symbolic name instead of actual addresses. However, like the machine language,

    the assembly language is very much dependent on the system and differs from

    machine to machine.

    It is called the mother language of computer. It was used for application and

    system software developing.

    2. High Level Languages

    The programming languages which are close to human language are known as

    high level languages. High level languages are programmer friendly but difficult

    for hardware. The programs written in these languages are close to English

    language. Other high level languages have their own rules (syntax) and character.

    These languages are used in developing application software system software.

    There are two further types of High Level Languages:

    Procedural Language

    OOP ( Object Oriented Languages)Basic , Cobol, Pascal, Fortran and C language are the examples of Procedural

    Languages whereas Visual Basic, Java, C ++ , etc are the examples of OOP

    languages.

    Q. What are Language processors or Language

    Translators ?

    Ans:

    Language Processor Translators

    When we write any program in high-level language, it does not run directly on

    the computer. Therefore, the program has to be converted into machine language.

  • 8/7/2019 Getting Started With C Langage

    4/20

    Page 4 of20

    The program which converts source program into an object program is called

    language processor has two types.

    Interpreter

    Compiler

    Interpreters

    The language processors which execute a source program by translation andexecute one instruction at a time is called interpreter. If there is any error in the

    instruction it indicates the error and program executing stops. The interpreter

    translates the program instruction one by one and executes them immediately. It

    does not create an object program. Thus the interpreter translates the source

    program each time and the program is executed. In this method of translation and

    execution, the processing speed is slow and times wasting. A basic language

    processor is an example of interpreter.

    Compiler

    The language processor that translates program written in a high levellanguage into the machine language as a whole is called the compiler. If there is

    any error in the source program, the compiler specifier the error at the time of

    compilation. All errors must be removed before the compilation which leads to

    successful compilation of the source program. The compiler generates an

    intermediate kind of file called an object file. The computer directly executes the

    object file.

    Source program/code

    The program which we write in High level language is called source

    code/source program. The source program cannot be run directly on the computer.It is translated into (binary code) or machine language for execution by the

    computer.

    Object Code

    After compiling, the source code/source program, converts into machine

    language and it is called object code/object program. The object program directly

    executes on the computer.

    Q. Write the Characteristics Of High Level Language?

    Ans: The main characteristics of high-level language are.

    1. These languages do not depend on machine but can be run on any

    computer system.

  • 8/7/2019 Getting Started With C Langage

    5/20

    C 5

    2. The program which writes in high-level language is called source

    code. The source program after compiling and execution, can be run an

    any computer system.

    3. An American National Standards Institute (ANSI) published the

    standard for computer programming languages which has a standard

    syntax

    4. These high level languages are very close to human languages. The

    high level languages are very easy to learn and can be understood byanother programmer.

    Q. Write history of C Language.

    Ans : Brief History C Language

    C is a general purpose programming language C was created by Dennis

    Ritchie at AT&T Bell Laboratories in 1972. C language is very popular language.

    C language is a mid level language. It was written as a part of the unix operating

    system. The unix system and its tools written is C. C language was used and

    develop system software.

    ANSI.C Standard

    In 1989 a new version of the language was given by the ANSI (American

    National Standards Institute) standard C. This version consists of many features.

    Inter nationaly, the international standards organization ISO is responsible for

    standardizing C language currently the ISO C standards in thus the final authority

    that constitutes the C language programming language.

    Q. Write advantages of C languages?

    Ans: Advantage of Language

    The C language programming has its superiority. It has many advantages

    these are

    1. C language is unique among programming languages in the same that

    it provides the convenience of a high level language such as Pascal or

    Basic

    2. It also allows much close control of a computers hardware.3. The better C compilers can now generate amazingly fast code. This

    code is so efficient that is often difficult to produce significant speed

    increases by rewriting it in assembly language.

    4. C is also a well-structured language: Its syntax makes it easy to write

    programs are modular and therefore easy to understand and maintain.

  • 8/7/2019 Getting Started With C Langage

    6/20

    Page 6 of20

    5. The C language includes many features that are specifically designed

    to help create large or complex programs.

    6. C is portable It is easier to convert a C program to run on a different

    machine that it is to convert programs written in most other language.

    Q. Write the Features of C Language?

    Ans: Features of C Language

    Some features of C Language are given below:

    Function Oriented Language

    C language is based on function and we use function for programming.

    Precise Compact Code

    C-Language is very precise.

    Vast Language

    C is vast language. Each statement has multiple usage. e.g In other languages

    for loop is used as counter loop but in c, for loop is used as counter and

    condition.

    Versatile Language

    In C any problem can be solved by a variety of different tools.

    Q. Write a note on Structure of C Program?

    Ans: Structure of C Program

    The format on the basic of which a program is written is called the structure of

    the program. The structure of C program is based on the following five parts.

    Preprocessor directives

    The main( ) function

    Begin brace

    C statements or Start C programming

    End braceThe first statement of C program is preprocessor directive. With the helpof this we includes the header file.

    For Example

    # include

  • 8/7/2019 Getting Started With C Langage

    7/20

    C 7

    The second function of C program is main( ) and next step begins with brace

    which indicates to start programming, After begining braces C programming

    starts or writes C statements and ending brace shows C program closing.

    # include

    void main(void)

    {

    printf(Welcome C language)}

    Preprocessor Directives

    The directives can be placed anywhere in a program, but are most often used

    at the beginning of a file, before main( );

    The first line of the structure.

    # include

    might look like a program statements, but it is not . It is not part of a function

    body and does not end with a semicolon, as program statements must. Instead, it

    starts with a number sign (#). It is called a preprocessor directive. Recall that

    program statements or instructions to the computer. A preprocessor directive, on

    the other hand is an instruction to the compiler itself. A part of the compiler called

    the preprocessor deals with these directives before it begins the real compilation

    process.

    Header Files

    The use of header files is common in C. Whenever you use a library function

    predefined, you will need to use a header files that contains appropriate

    declarations. Header files also contain prototypes for the library functions.

    Prototypes provide a way to avoid program errors. Each header file has a (.h) fileextension.

    The #include Directive

    The preprocessor directive # include tells the compiler to insert another file

    into your source file. We should mention that you can use #include in two ways.

    The angle brackets < and > surrounding the filenames.

    # include

    Example indicates that the compiler should begin searching for these files in

    the standard INCLUDE directory. This directory holds the header files supplied

    by Boarland for the system. Instead of angle brackets around the filename, you

    can also use quotation marks, as in

    # include my header is

  • 8/7/2019 Getting Started With C Langage

    8/20

    Page 8 of20

    Quotation marks instruct the compiler to begin its search for the header file in

    the current directory; this is usually the directory that contains the source file. You

    normally use quotation marks for header files you write yourself. Quotation marks

    or angle brackets work in case, but making the appropriate choice speed up the

    compilation process slightly by giving the compiler a hint about where to find the

    file.

    The main( ) FunctionAll C programs are divide into units called Function. The main( ) is used in

    the beginning of every program. It also controls the functions ofC. That main( )

    is a function. Every C program consists of one or more functions this program

    has only one. No matter how many function there are in a C program, main( ) is

    the one to which control is passed from the operating system when the program is

    run; it is the first function executed.

    The word void preceding main specifies that the function main( ) will not

    return a value. The second void in parentheses, specifies that the function takes

    no arguments.

    Curly Braces ({ })

    Aftermain uses this bracket. Also use this curly brace ({) before starting

    the program. We use this curly brace (}) to close the program. The number of

    only curly brackets used for opening the program must be equal to the no of

    closing the program.

    Statements Terminator or Semicolon (;)

    When any line is finished, It is very necessary to put a semicolon on its end

    point. This shows that this line has finished. If semicolon is not used, the program

    will give errors.

    Note:

    Semicolon should not be used at the end of a loop and condition otherwise the

    loop and condition will stop at that point immediately. Detail structure is given

    below:

  • 8/7/2019 Getting Started With C Langage

    9/20

    C 9

    F u n c t i o n n a m e

    v o i d m a i n ( v oO p e n i n gb r a c e t od e l i m i t b o d yo f f u n c t i o n

    {

    p r i n t f ( W e l c o m e C L

    o n e s t a t e ms e m it e r me a c hp r o gs t a t e

    C l o s i n gb r a c e t od e l i m i t b o d yo f f u n c t i o n

    T h i s e n t i r e P r o gc o n s i s t s o f a f uc a l l e d m a i n ( )

    }

    Q. Write a note on C Keywords or Reserved Words

    ANs: Every language has its own terminology which is called keywords or

    reserved words? There are 32 reserve words in C language. C language is case

    sensitive. Its compiler differentiates between upper and lower case. So it is

    necessary to execute program in lower case. Some Keywords or ReservedWords

    printf float const

    char scanf continue

    int void return

    for if switch

    case while staic

    default else struct

    do auto gets

    Q. Discuss IDE of C Language.

    The Integrated Development Environment System (IDE)

    This is the basic structure of C language but we need (IDE) of C for C

    language programming. Source program is written in (IDE). The facilities of

    editing, compiling, linking and program execution of C language are available in

    C (IDE). Where we can make C language setting. We can even debug our

    program within the (IDE). An on line help facility is available through menu

    selecting.

  • 8/7/2019 Getting Started With C Langage

    10/20

    Page 10 of20

    [IDE]

    IDE screen is displayed. However the edit window will automatically opened

    with NONAME00.C. The IDE screen, which initially displays only a menu bar at

    the top of the screen and a status bar below, will appear. The menu bar displayed

    the menu name File, Edit, Search, Run, and so forth. The status bar tells you

    what various function keys will do now, with a blank edit window awaiting us,you type program.

    Explain File Menu

    New

    After the (IDE) display appears, you must open and edit window into

    which you can type your program. You can also open an edit window by

    selecting New from the File menu. This Edit windows will be given the titleNONAME00.C.

    Open

    If you already in the (IDE) and you want to open a File that already exists,

    select Open from the File menu or(F3) on the keyboard. A dialog windowwill appear. Type in the name of the file in the Name Field, or select it from

    the list displayed.Note

    That only source files-those with the (.C) extension are listed.

    Save

    After you typed in the source file for the program, you should save your

    disk. To do this, select Save from the File menu or you can create same effect

    simply by pressing the [F2] function key from keyboard. A dialog box will

    appear. At the top of this dialog box is a field called save editor File As Delete

    temporary Name and type New Name and press [Enter]. The file will be savedunder this name.

    Note

    File Name only minimum one (1) character and maximum eight (8) characters

    and extension (.C).

    Save As

  • 8/7/2019 Getting Started With C Langage

    11/20

    C 11

    If you want to create a new copy of any file. First open file. Then select

    Save As from the File menu. A dialog box will appear. At the top of this box

    is a field call Save AsEditor file As. First file name delete and types new

    Name and press [Enter]. The New file will be saved under this new name.

    Changing Directories

    You can change the current directory by selecting the Change dir..

    option from the File menu. Once you have changed directories, open will list

    the source files in the new directory.Printing

    To send the contents of the file in the active window to the printer, select

    Print from the File menu.Dos Shell

    Selecting Dos Shell from the File menu. In Dos you can delete or list files,

    change directories, and perform any other Dos command. To return to the

    IDE, enter the word Exit at the Dos prompt and press enter.

    Quit

    To exit from the [IDE], select Quit from File menu or Type [Alt + X] and

    return dos prompt.Editing

    The program writing and conversion in (IDE) is called editing. Always use

    C functions whenever a program is written an C language. Its correct

    syntax in not used, program will give errors. This program is called source

    code.

    Basic Editor Commands

    The most important commands in any editor are those that move the cursor on

    the screen and insert and delete text. Here is a list of some of the more importantcursor commands

    Keyboard Key Action

    Cursor up on one line

  • 8/7/2019 Getting Started With C Langage

    12/20

    Page 12 of20

    Cursor down one line

    Cursor left one character

    Cursor right one character

    CTRL Cursor left one word

    CTRL Cursor right one word

    HOME Cursor to start of line

    END Cursor to end of line

    PGUP Scroll one screen up

    PGDN Scroll one screen down

    Enter Insert line, go to next line

    Backspace Delete character to left of cursor

    DEL Delete character under cursor

    INS Toggle insert/write over

    Cut, Copy, Paste, and Clear

    Once a block is marked, you can perform various operations on it. These

    operations can be invoked from the Edit menu, or by typing key combination.

    They make use of an internal text buffer called the clipboard. The following table

    summarizes the possibilities.

    Selection

    from

    Edit menu

    Shortcut Keys Action

    Cut [Shirft] [Del] Delete block and write it to

    clipboard

    Copy [Ctrl] [Ins] Copy block to clipboard

    Paste [Shirft] [Ins] Insert block from clipboard

    at cursor

    Clear [Ctrl] [Del] Delete block, do not writeto clipboard

  • 8/7/2019 Getting Started With C Langage

    13/20

    C 13

    To move a block of text, mark it, and then select Cut. The text will write to the

    clipboard, replacing whatever was there before. Then position the cursor where

    you want the block to go, and select Paste. The block will write at that location.

    Compiling

    The compiler converts the source file into object file. The object file, which has

    an (OBJ) extension, contains machine-language instructions. That can beexecuted by the computer.

    (Press Key F9)

    A Successful Compiling

    Linking

    In linking function make links with their own library file. This is called linking.

    When we make a program, the functions of C language used in this program havelibrary file. It extension in (.LIB). After compiling there function make link with

    their own library file and in this way, an executed file is (.exe) create.

    (Press Key F9)

  • 8/7/2019 Getting Started With C Langage

    14/20

    Page 14 of20

    A successful linking

    Executing a Program

    If there were no errors in the compiling or linking process, your are now ready torun the program. You can do this from the (IDE) by selecting Run from the Run

    menu, or by pressing the [Ctrl + F9] key combination.

    The program has written a message to output screen, but not to the [IDE] screen.

    To see the programs output, select user screen from the window menu, or press

    [Alt+F5]

    output

    Welcome C Language

    Pressing any key take you back to the (IDE) screen.

    Notes

    Compiling and linking F9

    Program Execution or Run program Ctrl + F9

    To see output screen Alt + F5

    Q. Write a note on errors and its types.

    Ans:

    ERRORSNo one writes error free computer programs every time. It may be that you

    were unclear on the syntax of particular statement, or that you made a typing

    error. Such errors can be discovered by the compiler or by the linker, or by

    various run time routines that the compiler builds into your program

    automatically.

  • 8/7/2019 Getting Started With C Langage

    15/20

    C 15

    Compiler ErrorsSuppose you have forgotten to type semicolon at the end of the statement in

    FIRST.C. The result will be that, when you compile window in C. Instead ofdisplaying the success message, will show a positive number in this case, 2 in theerrors field. Click ok in the edit status window, a message window will appear atthe bottom of the screen. It will contain the following figer.

    M E S S A G E

    C o m p i l i n g F I R S T . C :E r r o r F I R S T . C 6 : S t a t e m e n t m i s sE r r o r F I R S T . C 6 : c o m p u n d s t a t e

    6 : 4 M o d i f i e dI n s e r

    Often one typing mistake will lead the compiler to issue several error

    messages as happens here. It sees that the semicolon is missing. In the edit

    window, the program line with the closing brace is highlighted, and the brace

    itself is highlighted in a different color. This indicates the place where the

    compiler found error. When it go to the brace, it realized that there should havebeen a semicolon earlier, so it signaled the errors at that point.

    Linker ErrorsErrors can also appear during the linking process. For instance, suppose you

    spell the word main as MAIN. (Remember that C is case sensitive language). The

    program will compile correctly, but when you build or link it, the compile status

    (or linker) window will show a link error. Now the message window will display

    a line something like.

  • 8/7/2019 Getting Started With C Langage

    16/20

    Page 16 of20

    M E S S A G E

    C o m p i l i n g F I R S T . C :L i n k i n g F I R S T . E X EL i n k e r E r r o r : u n d e f i n e d s y m b o l _ m

    6 : 4 M o d i f i e dI n s e r

    The linker must find a function called main; without this, it cannot create anexecutable file. The linker does not highlight the offending line in the source file,so you may sometimes have difficulty in tracking down such errors. However, inthis case the problem should be obvious.

    Logical ErrorsWhen you start writing more complex programs, you will often find that a

    program compiles and links without errors, but nevertheless fails to operate asintended. In such situations you will probably want to use Cs debugger featuresto analyze whats going on. You can single step through a program, watchvariables change as each.

    Statement is executed, and performs other activities to help you analyze your

    programs aberrant behavior.Note: When all the errors are corrected, press [F2] to save the source file, and

    then [F9] to compile and link again.

  • 8/7/2019 Getting Started With C Langage

    17/20

    C 17

    EXERCISES

    Q1. Fill in the blanks.

    1. The physical devices that constitute a computer are collectively called the

    _________.

    2. The instruction given to the computer to program a task is called computer

    _________.

    3. ROM stands for___________ .

    4. The CPU is the most complex___________ of the computer.

    5. Thus a 300 MHz Pentium is much___________ faster than say a 100 MHz

    Pentium CPU.6. It is the CPU that performs all the___________ within the computer.

    7. The___________ controls the execution of instructions given to the

    computer system.

    8. The ___________ is used to transfer data from one device to another.

    9. ALU stands for___________ .

    10. The devices provide a means of feeding instruction and data into the

    computer.

    11. The main memory is a___________ storage memory.

    12. RAM is, therefore, also known as___________ memory.

    13. RAM stands for___________ .

    14. The memory is one of the most important characteristics of todays PCs.

    15. Main memory consists of several memory cells with___________ address.

    16. The_________ language is called the mother language of computer.

    17. To move a block of text, mark it, and then select _________ command.

    18. A programming language is a source of _________ between the user and

    the computer.

    19. Every language has its own _________ for writing a computer program.

    20. The Rules are called the __________ of the language.

    21. Some programming languages are close to __________ languages are

    called high level languages.

    22. Some programming languages are close to __________ languages arecalled low level languages.

    23. Three categories of computer programming languages are __________,

    __________ and __________.

    24. The executing speed is very fast in __________ language.

  • 8/7/2019 Getting Started With C Langage

    18/20

    Page 18 of20

    25. The two types of low-level languages are __________ language and

    __________ language.

    26. The language which contains both the high level and low level language is

    called __________ level language.

    27. The High level language are programmer friendly but difficult for

    __________.

    28. There high level languages are used in developing __________ and

    __________ software.

    29. Basic stands for __________.

    30. Cobol stands for __________.

    31. Fortran stands for __________.

    32. The processor has two types __________ and __________.

    33. The language processors which executes a source program by translating

    and execute one instruction at a time is called __________.

    34. The program which we write in high level language is called __________

    or __________.

    35. After compiling, the source code converts into machine language, it is

    called __________ code36. The source program after compiling and execution, can be run an any

    __________ system.

    37. C is also a __________ language.

    38. C Language is based on __________.

    39. C language is very __________.

    40. In C language any problem can be solved by a __________ of different

    tools.

    41. Header files also contain prototypes for the __________ functions.

    42. All C programs are divided into units called __________.

    43. The __________ is used in the beginning of every program.44. C language is __________.

    45. IDE stands for __________.

    46. The key (F2) is used to __________ file.

    47. ANSI stands for __________.

    48. To compile a program __________key is used.

    49. To execute a program __________ key is used.

    50. __________ key is used to open a file.

    51. __________ key is used to save a file.

    Q3. Select the most suitable choice.

    1. __________ language is used for business work.

    a. Cobol b. Basic

    c. Assembly d. C

    2. C language is also called __________ language.

    a. High level b. Mid level

  • 8/7/2019 Getting Started With C Langage

    19/20

    C 19

    c. Low level d. Machine

    3. Mistakes in a computer program are also called __________.

    a. Errors b. Syntax

    c. Bugs d. Non of the above

    4. The C statements are normally written in __________ letters

    a. Upper-case b. Reserved word

    c. Lower case d. Non of the above

    5. C language is a __________ language.a. Procedure oriented b. Function oriented

    c. Object oriented d. Non of the above

    6. __________ is an extension of header file.

    a. (.h) b. (.obj)

    c. (.c) d. (.cpp)

    7. __________ is an extension of library file.

    a. (.h) b. (.lib)

    c. (.c) d. Non of the above

    8. To compile a program __________ key is used.

    a. F9 b. F7c. F8 d. None of the above

    9. __________ key is used and save a file.

    a. F5 b. F3

    c. F9 d. F2

  • 8/7/2019 Getting Started With C Langage

    20/20

    Page 20 of20

    ANSWER EXERCISE.

    Q1. Fill in the blanks

    1. Hardware 2. Compute software 3. Read only memory 4. Part 5 faster 6.

    Calculations 7. Control Unit 8. Data bus. 9. Arithmetic logical unit 10. Input 11.

    Temporary 12. Volatile 13. Random Access Memory 14. Secondary 15. Particular

    16. Assembly 17. Cut 18. Commination 19. Rules 20. Syntax 21. Human 22.

    Machine 23. Low level, mid level and high level 24. Machine 25. Machine,

    Assembly 26. Mid 27. Hardware 28. Application, System 29. Beginners all

    purposes symbolic instruction cod 30. Common business oriented language 31.

    Formula Translation 32. Interprets compiler 33. Interpreter 34. Source code,

    source program 35. Object 36. Computer 37. Well structured 38. Functions 39.

    Precise. 40. Variety 41. Library 42. Functions 43. main( ) 44. Case sensitive 45.

    Integrated Development system 46. Save 47. American National standards

    institute 48. F9 49. Ctrl + F9 50. F3 51. F2.

    Q3. Answers

    1. a 2. b 3. a 4. c 5. b. 6. a 7. d 8. a 9 d.