c language: introduction 1 c programming cisy 238 bill klinger

Post on 04-Jan-2016

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

C Language: IntroductionC Language: Introduction 11

C ProgrammingC Programming

CISY 238CISY 238

Bill KlingerBill Klinger

C Language: IntroductionC Language: Introduction 22

RVCC Tutoring CenterRVCC Tutoring Center

Room S-020-CRoom S-020-C

C Language: IntroductionC Language: Introduction 33

OutlineOutline

Computer languagesComputer languages C BackgroundC Background ProgrammingProgramming UNIX environmentUNIX environment EditorEditor C fundamentalsC fundamentals Getting startedGetting started

C Language: IntroductionC Language: Introduction 44

Computer LanguagesComputer Languages Computers

powerful complex use simple instructions

Language development machine language assembler high level languages 4GLs

C Language: IntroductionC Language: Introduction 55

Evolution of Computer Evolution of Computer ComplexityComplexity

machine code assembler C C++, C#, Java

think like a machine think like a human

00110111 add regA, regB c = a + b ; objA.add( objB ) ;

C Language: IntroductionC Language: Introduction 66

Early LanguagesEarly Languages FortranFortran

Formula translationFormula translation Scientific applicationsScientific applications

COBOLCOBOL Common Business Oriented LanguageCommon Business Oriented Language Business / data applicationsBusiness / data applications

C Language: IntroductionC Language: Introduction 77

Language ExplosionLanguage Explosion AlgolAlgol

Designed with programming concepts in mindDesigned with programming concepts in mind BasicBasic

The first interactive languageThe first interactive language SimulaSimula

Extended Algol to facilitate simulations, first O-O languageExtended Algol to facilitate simulations, first O-O language SnobolSnobol

Specially designed to handle string manipulationSpecially designed to handle string manipulation LISPLISP

Used in artificial intelligence applicationsUsed in artificial intelligence applications PL/1PL/1

Language for everyone (therefore no one)Language for everyone (therefore no one)

C Language: IntroductionC Language: Introduction 88

C LanguageC Language Created by Dennis RitchieCreated by Dennis Ritchie

early 1970’searly 1970’s Bell LabsBell Labs

C comes after BC comes after B C++ comes after CC++ comes after C Design principlesDesign principles

high level languagehigh level language allow for detailed control of the systemallow for detailed control of the system

C Language: IntroductionC Language: Introduction 99

C LanguageC Language UNIX implemented in CUNIX implemented in C Distributed to universitiesDistributed to universities Becomes commercial successBecomes commercial success

widely availablewidely available efficientefficient

C/C++ still the language of choice for C/C++ still the language of choice for ‘serious’ applications‘serious’ applications

C Language: IntroductionC Language: Introduction 1010

C Language UsageC Language Usage systems programmers (drivers)systems programmers (drivers) operating systems (Linux, UNIX, Windows, operating systems (Linux, UNIX, Windows,

et.al.)et.al.) embedded systemsembedded systems real-time systemsreal-time systems gamesgames applicationsapplications educationeducation

C Language: IntroductionC Language: Introduction 1111

ANSI CANSI C Standard establishedStandard established

to ensure common language set by vendorsto ensure common language set by vendors allows programs to ‘port’ to different computersallows programs to ‘port’ to different computers

ANSIANSI American National Standards InstituteAmerican National Standards Institute has established standards for many languageshas established standards for many languages

C Language: IntroductionC Language: Introduction 1212

A Word About C++A Word About C++ Extends CExtends C

all of C in C++all of C in C++ C++ adds object oriented programmingC++ adds object oriented programming

incorporates classes from Simulaincorporates classes from Simula does some ‘clean up’does some ‘clean up’

C Language: IntroductionC Language: Introduction 1313

ProgrammingProgramming

The process of translating needs The process of translating needs (requirements) into directions a computer (requirements) into directions a computer can execute.can execute.

C Language: IntroductionC Language: Introduction 1414

ProgrammingProgramming An artAn art

there is a beauty in good programsthere is a beauty in good programs it is a creative processit is a creative process

A scienceA science there is certainty in programsthere is certainty in programs programming requires precisionprogramming requires precision computer will do what it is instructed tocomputer will do what it is instructed to

lather – rinse – repeatlather – rinse – repeat

C Language: IntroductionC Language: Introduction 1515

The ProcessThe Process Write the codeWrite the code

called ‘source code’called ‘source code’ written using an editorwritten using an editor

Compile the codeCompile the code using a compilerusing a compiler creates object modulescreates object modules

Link the objectsLink the objects put together the piecesput together the pieces creates the executablecreates the executable

Test / debugTest / debug

C Language: IntroductionC Language: Introduction 1616

The ProcessThe Process Write the codeWrite the code

use vi, pico, notepad, TextPad, or some editoruse vi, pico, notepad, TextPad, or some editor CompileCompile

use cc on UNIXuse cc on UNIX LinkLink

cc will also linkcc will also link Test / debugTest / debug

C Language: IntroductionC Language: Introduction 1717

UNIX EditorsUNIX Editors vivi

available on all UNIX / Linux systemsavailable on all UNIX / Linux systems primitive but powerfulprimitive but powerful

picopico simpler than visimpler than vi less powerfulless powerful

C Language: IntroductionC Language: Introduction 1818

The C LanguageThe C Language small, efficient and powerfulsmall, efficient and powerful easy to learn (sort of)easy to learn (sort of) precise computer controlprecise computer control no hidden mechanisms or magicno hidden mechanisms or magic C Standard LibraryC Standard Library

C Language: IntroductionC Language: Introduction 1919

C Source File Creation Use text editor (vi, emacs, pico, TextPad,

NotePad, etc.) to create a file

for C language, the file must have a .c suffix

C Language: IntroductionC Language: Introduction 2020

C Executable Program Creation C is a compiled rather than an interpreted language,

the .c source file can not be directly executed like a shell script

compile using the C compiler cc: $ cc program.c ---> a.out (where a.out is the default executable file name)

run program by entering its name: $ a.out

can choose a different name for the executable: $ cc -o myprogram program.c ---> myprogram- or: $ mv a.out myprogram

C Language: IntroductionC Language: Introduction 2121

C Source File

// a basic program

#include <stdio.h>

main(){ printf(“Hello World”);}

C Language: IntroductionC Language: Introduction 2222

C Source File Contentsmain function; main tells where the

program starts, one and only one main

a left brace { is the beginning and a right brace } is the end of a function or code block

parentheses ( ) indicate a function definition

C Language: IntroductionC Language: Introduction 2323

PrintingPrinting Use the ‘printf’ statementUse the ‘printf’ statement FormForm

printf(“<string>”);printf(“<string>”); Note:Note:

string is enclosed in quotesstring is enclosed in quotes C statement ends in semicolonC statement ends in semicolon

C Language: IntroductionC Language: Introduction 2424

PrintingPrinting printf ‘prints’ a stream of charactersprintf ‘prints’ a stream of characters

whatever is in <string> is outputwhatever is in <string> is output no automatic “new lines”no automatic “new lines”

Use \n to print a new line characterUse \n to print a new line character printf does not actually print to a printerprintf does not actually print to a printer

goes to UNIX standard outputgoes to UNIX standard output default is to terminaldefault is to terminal

C Language: IntroductionC Language: Introduction 2525

Variable BasicsVariable Basics Must declare variables before using themMust declare variables before using them

e.g. e.g. int age;int age;

int sum, average;int sum, average;

Use meaningful names !Use meaningful names !

C Language: IntroductionC Language: Introduction 2626

Statement BasicsStatement Basics Assignment indicated with ‘=‘Assignment indicated with ‘=‘

e.g.e.g.sum = 5 + 4;sum = 5 + 4;

sum = myage + your age;sum = myage + your age;

age = age + 1;age = age + 1;

Note:Note: each statement ends in a semicolon‘;’each statement ends in a semicolon‘;’ = does not mean arithmetic equality= does not mean arithmetic equality

C Language: IntroductionC Language: Introduction 2727

C Source File// a simple program that prints// the number of fruit

#include <stdio.h>

main(){ int oranges; int apples; int fruit;

oranges = 5; apples = 17;

fruit = oranges + apples;

printf(“We have %d pieces of fruit”, fruit);}

C Language: IntroductionC Language: Introduction 2828

Let’s Get StartedLet’s Get Started Do Hello World program togetherDo Hello World program together Exercises listed on webExercises listed on web

top related