C Basics Unit-01

download C Basics Unit-01

of 13

Transcript of C Basics Unit-01

  • 8/10/2019 C Basics Unit-01

    1/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    1

    Unit-1 BASIC KNOWLEDGE OF C

    1.1 INTRODUCTION

    C is a general-purpose programming language initially developed by Dennis Ritchie between 1969and 1973 at AT&T Bell Labs. Its design provides constructs that map efficiently to typical machineinstructions, and therefore it found lasting use in applications that had formerly been coded inassembly language, most notably system software like the Unix computer operating system.C is one of the most widely used programming languages of all time, and there are very fewcomputer architectures for which a C compiler does not exist.

    Many later languages have borrowed directly or indirectly from C, including C#, D, Go, Java,JavaScript, Limbo, LPC, Perl, PHP, Python, and Unix's C shell. The most pervasive influence onthese languages has been syntactical, and they tend to combine the recognizable expression and

    statement syntax of C with underlying type systems, data models, and semantics that can beradically different. C++ started as a pre-processor for C and is currently nearly a superset of C.

    Before there was an official standard for C, many users and implementers relied on an informalspecification contained in a book by Dennis Ritchie and Brian Kernighan; that version is generallyreferred to as "K&R" C. In 1989 the American National Standards Institute published a standard forC (generally called "ANSI C" or "C89"). The next year, the same specification was approved by theInternational Organization for Standardization as an international standard (generally called "C90").ISO later released an extension to the internationalization support of the standard in 1995, and arevised standard (known as "C99") in 1999. The current version of the standard (now known as"C11") was approved in December of 2011.

    1.1.1 Design

    C is an imperative (procedural) language. It was designed to be compiled using a relatively

    straightforward compiler, to provide low-level access to memory, to provide language constructs that

    map efficiently to machine instructions, and to require minimal run-time support. C was therefore

    useful for many applications that had formerly been coded in assembly language, such as in system

    programming.

    Despite its low-level capabilities, the language was designed to encourage cross-platform

    programming. A standards-compliant and portably written C program can be compiled for a very

    wide variety of computer platforms and operating systems with few changes to its source code. Thelanguage has become available on a very wide range of platforms, from embedded microcontrollers

    to supercomputers.

    1.1.2 First C program

    The "hello, world" example, which appeared in the first edition of K&R, has become the model for anintroductory program in most programming textbooks, regardless of programming language. Theprogram prints "hello, world" to the standard output, which a terminal is usually or screen display.

  • 8/10/2019 C Basics Unit-01

    2/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    2

    The original version was:

    main(){

    printf("hello, world\n");}A standard-conforming "hello, world" program is:[nb 1]

    #include

    int main(void){

    printf("hello, world\n");}

    The first line of the program contains a pre-processing directive, indicated by #include. This causesthe compiler to replace that line with the entire text of the stdio.h standard header, which containsdeclarations for standard input and output functions such as printf. The angle brackets surroundingstdio.h indicate that stdio.h is located using a search strategy that prefers standard headers to otherheaders having the same name. (Double quotes are used to include local or project-specific headerfiles.)

    The next line indicates that a function named main is being defined. The main function serves aspecial purpose in C programs; the run-time environment calls the main function to begin programexecution. The type specifier int indicates that the value that is returned to the invoker (in this casethe run-time environment) as a result of evaluating the main function is an integer. The keywordvoid as a parameter list indicates that this function takes no arguments.

    The opening curly brace indicates the beginning of the definition of the main function. The next linecalls (diverts execution to) a function named printf, which is supplied from a system library. In thiscall, the printf function is passed (provided with) a single argument, the address of the first characterin the string literal "hello, world\n". The string literal is an unnamed array with elements of type char,set up automatically by the compiler with a final 0-valued character to mark the end of the array(printf needs to know this). The \n is an escape sequence that C translates to a newline character,which on output signifies the end of the current line. The return value of the printf function is of typeint, but it is silently discarded since it is not used. (A more careful program might test the returnvalue to determine whether or not the printf function succeeded.) The semicolon; terminates thestatement.

    The closing curly brace indicates the end of the code for the main function. The main function isimplicitly understood to return the integer value 0 upon completion, which is interpreted by the run-time system as an exit code indicating successful execution.

    1.1.3 Compilers* to execute a C program

    *A compiler is a computer program (or set of programs) that transforms source code written in aprogramming language (the source language) into another computer language (the target language,often having a binary form known as object code). The most common reason for wanting totransform source code is to create an executable program. An executable program is in a form

  • 8/10/2019 C Basics Unit-01

    3/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    3

    understandable by the underlying machine, the processor can only then perform the expectedfunction.

    A C program can be compiled and executed across various operating systems like Windows, Linux,

    and Mac OS. Various compilers are written for the purpose.Following are two instances of the compilers which can be used to compile and execute Cprograms.

    1.1.3.1 Running a C program in Windows environment using Turbo C compiler

    1.1.3.1.1 Installing a Turbo C compiler

    Turbo C was an Integrated Development Environment and compiler for the C programminglanguage from Borland. It was first introduced in 1987 and was noted for its integrated developmentenvironment, small size, extremely fast compile speed and comprehensive manuals.

    In May 1990, Borland replaced Turbo C with Turbo C++.

    Here we are going to talk about Turbo C++ 3.0 version. it is best of all version and widely used bystudents.

    Turbo C++ 3.0 was released in 1991. Initially released as an MS-DOS compiler, 3.0 supported C++templates, Borlands inline assembler, and generation of MS-DOS mode.

    To install turbo c compiler

    1. First you need to download the compiler, which is available on internet.2. After finished download unzip the turbo c to folder & open the folder.

    Note: For the time of installation the folder should be present anywhere in Drive C. Like desktop,documents, downloads..3. Run install.exe and press ENTER to continue.

  • 8/10/2019 C Basics Unit-01

    4/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    4

    4. Enter the SOURCE drive to use: C

    5. Enter the SOURCE Path: source path is where you extracted the turbo c.zip file we dont need togive the source it will find and take automatically. you just press ENTER at this step.

  • 8/10/2019 C Basics Unit-01

    5/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    5

    6. Then scroll down by arrow key and Start Installation.

    Now you have successfully installed turbo c compiler.

    Now to open the turbo c compiler

    go to Drive C > TC > Bin > TC .exe

    You can have shortcut on desktop so that no need to follow long path to open TC.

    Just open TC folder in drive C select TC.exe and right click on it, Click on send to> Desktop (createshortcut).

  • 8/10/2019 C Basics Unit-01

    6/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    6

    1.1.3.1.2 Run a C program in Windows environment using command prompt

    Introduction: C Program can be run using Command Prompt .We can use MS-DOS to run cProgram. Every window OS comes with inbuilt Command Prompt. So we are going to use this

    command prompt to run our c program.

    Pre-requisite:

    1 Windows Command Prompt

    2 Turbo C/C++ Compiler

    Why we should run C Program using Command Prompt?

    1 We have Stored our C programs inside directories other than (C:/TC/bin) then we are unable to runc program.

    2

    In order to run C Program we must copy C Program and paste it inside bin folder.

    3 This process is bit time consuming and complex.

    Running C Program using Command Prompt :

    Create One Directory in D Drive (in my case my directory is C Programs)

    Now we must have Turbo C/C++ IDEinstalled in your system.( You may download it fromhere)

    After installing Turbo C/C++ IDE we have this directory Structure

    http://www.box.com/shared/vk78jq5ln2http://www.box.com/shared/vk78jq5ln2http://www.box.com/shared/vk78jq5ln2http://www.box.com/shared/vk78jq5ln2
  • 8/10/2019 C Basics Unit-01

    7/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    7

    Now Right Click on My Computer Iconand Click on Properties

  • 8/10/2019 C Basics Unit-01

    8/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    8

    Click onEnvironment VariableButton.

  • 8/10/2019 C Basics Unit-01

    9/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    9

    Make entry of bin inside Path Variable

    Make Entry of TC>bin inside Path Variable and Click on OK.

    C:\TC\BIN

    Now Open Command Promptby typing cmd inside Run Option.

    Go to Path Where you have Savedyour C Programs.

  • 8/10/2019 C Basics Unit-01

    10/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    10

    Microsoft Windows [Version 6.1.7600]Copyright (c) 2009 Microsoft Corporation. All rightsreserved.

    C:\Users\Santoshi>D:D:\>cd C Programs

    D:\C Programs>Suppose we have Saved hello.c program inside C Programs folder. then type this command toCompile hello.c

    D:\C Programs>tcc hello.cTurbo C++ Version 3.00 Copyright (c) 1992 Borlandhello.c:

    Turbo Link Version 5.0 Copyright (c) 1992 Borland

    Available memory 4106832

    After typing tcc commandwe can compile C Program.

    Now its time to run C Program after Successful Compilation.Just Type Program name and HitEnteryou will get output on the console

    D:\CPROGR~1>helloHello

    Note :

    Actually tcc.exe Command is located inside C:/TC/bin So we have make this entry inside pathvariable so that Command Prompt can find this command from that specified path.

    1.1.3.2 Running C programs on Linux

    When you write a program, it doesn't do anything until you compile it. Many beginner programmerswill use graphical IDEs such as Microsoft Visual Studio to write and compile their code, but if youare told to use a Unix machine, you won't be able to use this software package. GCC is a compilerfor C, C++, Java, Fortan and other program code that can be used in Unix, GNU/Linux machines. It

    is distributed as Free Software under the GNU General Public License (GNU GPL). It is useful toknow how to do at least simple compiling with this compiler. This guide assumes the reader has abasic knowledge of using Unix, GNU/Linux from the command line.

    1. Open up a terminal on Unix or GNU/Linux or a command prompt on Microsoft

    Windows

    In order to see if you have the GNU C/C++ compiler installed on your system

    #: gcc --version

  • 8/10/2019 C Basics Unit-01

    11/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    11

    It should inform you about the version number of the compiler. If the command is not found,it is likely that gcc/g++ isn't installed.

    2. Make sure you have gcc/g++ installed on your system, consult your Unix or GNU/Linux

    distribution documentation for the appropriate installation method.

    3. Create a working directory using the following command:#: mkdir CCPP

    4. Change into the directory#: cd CCPP

    5. Use a text editor such as nano, gedit, vi, notepad,(on Microsoft Windows ) for example,

    6. Unix/GNU/Linux Instructions:#: nano main.c#: gedit main.c#: vi main.c

    7. Windows Instructions:#: notepad main.cYou can use notepad as a text editor on Microsoft Windows

    8. Enter the following source code below:#:

    #include #include int main(){printf("Hello World\n");return(0);}

    Once this task is complete save the source code as a text file in your editor as main.c

    1 Compile the file by issuing the following command

    #: gcc main.c -o HelloWorld

    2 Run the program by issuing the following command

    3 Unix/GNU/Linux instructions:

    #: ./HelloWorld

  • 8/10/2019 C Basics Unit-01

    12/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    12

    4 Microsoft Windows Instructions:

    #: HelloWorld.exe

    On Windows when the HelloWorld.c file is compiled it will usually have an .exe extention.

    5 To compile multiple files:

    #: gcc -o outputfile file1.c file2.c file3.c

    6 To compile with more error checking using the -Wall tag:

    #: gcc -Wall -o outputfile file1.c

    7 To compile files without linking them:

    #: gcc -c file1.c file2.c file3.c

    After the last step, if you want to link them, type: gcc -o outputfile file1.o file2.o file3.o

    8 Fix any errors/warnings the compiler reports.

    Recompile the code if you had to fix errors.

    Execute the program.

    1.1.4 Decoding the program structure

    1.1.4.1 Header files in C

    Header files are included into a program source file by the #include preprocessor directive. The C

    language supports two forms of this directive; the first,

    #include "header"

    is typically used to include a header file header that you write yourself; this would contain definitionsand declarations describing the interfaces between the different parts of your particular application.By contrast,

    #include

    is typically used to include a header file file.h that contains definitions and declarations for a

    standard library.

    Including such a header file gives the current program access to the functions defined in the headerfile. Like in the following example including the header file stdio.h gives access to the functions printf() and scanf() which are defined in the stdio.h header file. Functions are discussed in detail infollowing units

    Consider the following C program:

    #include

    #include

  • 8/10/2019 C Basics Unit-01

    13/13

    C-C++ Programmer

    Unit 1: Basic Knowledge of C

    13

    int main()

    {

    printf("Hello World\n");

    return(0);

    }

    The program have two header files stdio.h and stdlib.h.

    C Library provides several header files, each of which contains the type and macro definitions andvariable and function declarations for a group of related facilities. This means that your programsmay need to include several header files, depending on exactly which facilities you are using.

    One such facility used in the above example is printfstatement. The printf statement outputs thestring which is enclosed in parentheses and double quotes. In the above example(Hello World).

    This behavior of the printf statement is defined in the header file stdio.h.

    1.1.4.2 main() function

    main() is a user-defined function. Normally every C/C++ program should have a main()function.Compilation of a program begins from main() function. main()function calls all the functionsone by one inside its body. The smallest C program is

    void main()

    {

    }

    Consider the following example

    #include

    #include

    intmain()

    {

    printf("HelloWorld\n");

    return(0);}

    There are three functions in the program main(), printf() and return(). The function of printf() andreturn() is defined in the header files, which are included in the program(stdio.h and stdlib.h).

    The space between the parentheses ({....}) of the main() function is the space where theprogrammer can write the instructions which define the behavior of the program.

    http://www.blogger.com/goog_986676351http://www.blogger.com/goog_986676351http://www.blogger.com/goog_986676351http://www.blogger.com/goog_986676351