01 C

Embed Size (px)

DESCRIPTION

cs

Citation preview

  • Introduction to C Programming Introduction

  • Books

    The Waite Groups Turbo C Programming for PC, Robert Lafore, SAMS

    C How to Program, H.M. Deitel, P.J. Deitel, Prentice Hall

  • What is C?C A language written by Brian Kernighan and Dennis Ritchie. This was to be the language that UNIX was written in to become the first "portable" languageIn recent years C has been used as a general-purpose language because of its popularity withprogrammers.

  • Why use C?Mainly because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be: Operating Systems Language Compilers Assemblers Text Editors Print Spoolers Network Drivers Modern Programs Data Bases Language Interpreters Utilities Mainly because of the portability that writing standard C programs can offer

  • HistoryIn 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming Language by Kernighan & Ritchie caused a revolution in the computing world

    In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988.

  • Why C Still Useful?C provides:Efficiency, high performance and high quality s/wsflexibility and powermany high-level and low-level operations middle levelStability and small size codeProvide functionality through rich set of function librariesGateway for other professional languages like C C++ Java

    C is used:System software Compilers, Editors, embedded systemsdata compression, graphics and computational geometry, utility programsdatabases, operating systems, device drivers, system level routinesthere are zillions of lines of C legacy codeAlso used in application programs

  • Software Development MethodRequirement Specification Problem DefinitionAnalysis Refine, Generalize, Decompose the problem definitionDesign Develop Algorithm Implementation Write CodeVerification and Testing Test and Debug the code

  • Development with CFour stagesEditing: Writing the source code by using some IDE or editorPreprocessing or libraries: Already available routines compiling: translates or converts source to object code for a specific platform source code -> object codelinking: resolves external references and produces the executable module

    Portable programs will run on any machine but..

    Note! Program correctness and robustness are most important than program efficiency

  • Programming languages Various programming languagesSome understandable directly by computersOthers require translation steps Machine languageNatural language of a particular computerConsists of strings of numbers(1s, 0s)Instruct computer to perform elementary operations one at a timeMachine dependant

  • Programming languagesAssembly Language

    English like abbreviations

    Translators programs called Assemblers to convert assembly language programs to machine language.

    E.g. add overtime to base pay and store result in gross pay LOADBASEPAY ADDOVERPAY STOREGROSSPAY

  • Programming languagesHigh-level languages

    To speed up programming even furtherSingle statements for accomplishing substantial tasksTranslator programs called Compilers to convert high-level programs into machine language

    E.g. add overtime to base pay and store result in gross paygrossPay = basePay + overtimePay

  • History of CEvolved from two previous languagesBCPL , BBCPL (Basic Combined Programming Language) used for writing OS & compilersB used for creating early versions of UNIX OSBoth were typeless languagesC language evolved from B (Dennis Ritchie Bell labs)** Typeless no datatypes. Every data item occupied 1 word in memory.

  • History of CHardware independentPrograms portable to most computersDialects of CCommon C ANSI C ANSI/ ISO 9899: 1990Called American National Standards Institute ANSI CCase-sensitive

  • C Standard LibraryTwo parts to learning the C worldLearn C itselfTake advantage of rich collection of existing functions called C Standard LibraryAvoid reinventing the wheelSW reusability

  • Basics of C EnvironmentC systems consist of 3 partsEnvironmentLanguageC Standard LibraryDevelopment environment has 6 phasesEditPre-processorCompileLinkLoadExecute

  • Basics of C Environment

  • Basics of C Environment

  • Simple C Program/* A first C Program*/

    #include void main() { printf("Hello World \n"); }

  • Simple C ProgramLine 1: #include

    As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from your source file. In this case, the directive #include tells the preprocessor to include code from the file stdio.h. This file contains declarations for functions that the program needs to use. A declaration for the printf function is in this file.

  • Simple C ProgramLine 2: void main()

    This statement declares the main function. A C program can contain many functions but must always have one main function.A function is a self-contained module of code that can accomplish some task. Functions are examined later. The "void" specifies the return type of main. In this case, nothing is returned to the operating system.

  • Simple C ProgramLine 3: {

    This opening bracket denotes the start of the program.

  • Simple C ProgramLine 4: printf("Hello World From About\n");

    Printf is a function from a standard C library that is used to print strings to the standard output, normally your screen. The compiler links code from these standard libraries to the code you have written to produce the final executable. The "\n" is a special format modifier that tells the printf to put a line feed at the end of the line. If there were another printf in this program, its string would print on the next line.

  • Simple C ProgramLine 5: } This closing bracket denotes the end of the program.

  • Escape Sequence\nnew line\ttab\rcarriage return\aalert\\backslash\double quote

  • Memory conceptsEvery variable has a name, type and valueVariable names correspond to locations in computer memoryNew value over-writes the previous value Destructive read-inValue reading called Non-destructive read-out

  • Arithmetic in CC operationAlgebraic CAddition(+)f+7f+7Subtraction (-)p-cp-cMultiplication(*)bmb*mDivision(/)x/y, x , x yx/yModulus(%) r mod sr%s

  • Precedence order Highest to lowest()*, /, %+, -

  • ExampleAlgebra:z = pr%q+w/x-yC:z = p * r % q + w / x y ;Precedence:12435

  • ExampleAlgebra:a(b+c)+ c(d+e)C:a * ( b + c ) + c * ( d + e ) ;Precedence: 3 1 5 42

  • Decision MakingChecking falsity or truth of a statementEquality operators have lower precedence than relational operatorsRelational operators have same precedence Both associate from left to right

  • Decision MakingEquality operators==!=Relational operators=

  • Summary of precedence orderOperatorAssociativity

    () left to right* / % left to right+ - left to right< >= left to right== != left to right= left to right

  • Assignment operators=+=-=*=/=%=

  • Increment/ decrement operators++++a++a++----a--a--

  • Increment/ decrement operatorsmain(){int c;c = 5;printf(%d\n, c);printf(%d\n, c++);printf(%d\n\n, c);

    c = 5;printf(%d\n, c);printf(%d\n, ++c);printf(%d\n, c);

    return 0;}556

    566

  • Thank YouThank You

    Typeless no datatypes. Every data item occupied 1 word in memory.