Você está na página 1de 15

The C Programming Language

q refs: ref. books ([King, ], K&R, Afzal), related web links q what is C and why we learn it q history of C q running the helloWorld program q language components: data types, literals, identiers, variables q generating output!

COMP2300 C1: Starting Your C Journey

2010

What is C?
q an imperative programming language s contains a list of instructions or commands (Latin imperare to command) q emphasis is on saying what a program has to do, instead of objects (like Java,
Eiffel or Smalltalk) or functional relationships (like Haskell or Lisp)

functional languages emphasise evaluation of expressions rather than execution of commands

q has statements and basic data-types q the same programming paradigm as assembly language, which also has
instructions and basic data-types (e.g. byte, integer, oat)

s a mid level language; the universal assembly language! q why do we learn C? s s s s


introduction to imperative programming used later in COMP (and other courses); wide usage in the Real World other languages (e.g. C++, Java, csh, . . . ) will be easier to learn the language of computer systems; the fundamental compiled langauge helps understanding of how programs from other languages are executed 2010 2

COMP2300 C1: Starting Your C Journey

History of C

1960s:

CPL (Cambridge Programming Language)

BCPL (Basic CPL)

1970s: B

C (K & R)

1980s: C (ANSI)

C++

Objective C

1990s: Java

COMP2300 C1: Starting Your C Journey

2010

A First Program: helloWorld.c


< >
{ }

" H e l l o W o r l d \ n "

q note: no class or objects mentioned q line 1 includes standard IO library interface (header le) q

is a special function in this case with no arguments to access the command


line parameters

s can use

q prints the string "Hello World followed by a new line q returns the functions result (to the operating system) s signies normal termination
COMP2300 C1: Starting Your C Journey 2010 4

Compiling and Running your C Program


q if your program is in a single le called helloWorld.c, create an executable program
by using the following command:

gcc -Wall -o helloWorld helloWorld.c


where:

gcc -Wall -o helloWorld

the GNU C compiler show all warnings name of executable

q run your program by typing the name of the executable partch:/comp2300/C1> ./helloWorld Hello World partch:/comp2300/C1> q with programs made of multiple C les, separate compilation and linking is best we will discuss this later

COMP2300 C1: Starting Your C Journey

2010

Why is C so popular?
q small and concise (32 keywords - [King, table 2.1]) q portable (ANSI standard) and available (compilers for almost every platform) q efcient (compiler produces efcient machine code) s programmer has more control of data layout and object code produced s examples: optimizedMatrixMult.c, inlineAssemblerEx.c s very convenient for low-level data manipulation e.g. underow.c q arguably the programming language for computer systems s closely tied to Unix (Linux) s system-level control (drivers, etc.) q structured; modular s can support abstract data types and object-oriented design q large user and code base
COMP2300 C1: Starting Your C Journey 2010 6

Basic C Program Structure

< >

q we could write

{ " H e l l o W o r l d \ n "

. . . but it would be bad style!


(and C allows you to do worse. . . e.g. a program producing this graphic)

COMP2300 C1: Starting Your C Journey

2010

Basic Data Types


q integer (signed or unsigned): s (8 bit integer) s (small integer) s (standard integer) s (long integer) q oating-point: s s

(standard precision oat - 32 bit) (higher precision oat - 64 bit)

q typeless/valueless: s q no Boolean data type; instead: is false and non- is true q sizes are not explicitly dened, but relative size is respected
COMP2300 C1: Starting Your C Journey 2010 8

Literals
q integer: decimal (e.g. , ), octal (leading , e.g. , ) or hexadecimal (leading , e.g. , ) q oating point (e.g. , q characters: s s s s s
by symbol (e.g. q, A, \%) by ASCII code (e.g. 012, \xA) by some escape code (e.g. \n new line, \t tab) as an integer (e.g. \n == \x10) note: \000 (or \0) is not equal to 0 , ,

q strings s a string literal (constant) is a sequence of zero or more characters surrounded by double quotes (e.g. "COMP2300") s are automatically terminated with a null character (\0), so "Hello!" will
require 7 bytes of storage s there is no limit on string length! s different character representations valid in one string (e.g. "\x57indows\n") COMP2300 C1: Starting Your C Journey 2010 9

Identiers and Variables


q identiers used for variable names, function names, macro names: s start with a letter or with ; followed by letters, digits or (case-sensitive) s by convention: x starting with is reserved for use by the compiler and software libraries x constants are in upper case, with separators. e.g.

"comp2300"

q variables: s s s s s
must be declared at the beginning of the function they are used in only exist within the function they are declared in (their scope)! global variables can be declared, but should only be used with good reason variables may be qualied as , , or may be initialised at declaration e.g.

" March "


COMP2300 C1: Starting Your C Journey


10

2010

The Output Function:


q a function from the stdio library

q displays the string (characters between the double quotes) to the screen q special characters are displayed using escape sequences;
\ \ \\ \
audible alert (bell) new line backslash percentage

\ \ \ \

back space carriage return single quote tab

\ \ \"

form feed vertical tab double quote

q "format string"

q has a variable number of arguments (parameters) s rst a format string s subsequent arguments are the values to be displayed q the function inserts the values into the format string (in accordance with the
specied format) and then displays it, e.g.:

"Temperature: %d\n"
will display:

Temperature:

24
2010 11

COMP2300 C1: Starting Your C Journey

Format strings

q the format string contains: s ordinary characters, which are displayed without being changed s format speciciers, which are replaced by characters representing the
corresponding value in the subsequent parameters

); unsigned integer as decimal ( )


signed integer as decimal ( unsigned integer as hexadecimal ( oating point number as decimal ( character ( string (or array of characters) ( display the character %

) or )

COMP2300 C1: Starting Your C Journey

2010

12

Formats: Example printDate.c

< > " C O M P 2 3 0 0 " { " March " " H e l l o % s \ n " " T o d a y s d a t e i s % d , % s , % d \ n " " \ n \ n \ t \ t ( i t s T h u r s d a y ) \ n "
}

COMP2300 C1: Starting Your C Journey

2010

13

Fancy Formats: fancyPrintf.c


q look at [King, ch 3] and consider...

< > { " Peter Christen " " c h a r a c t e r % c i n t e g e r % d \ n " < { " c h a r a c t e r % c % d % o % x \ n "
}

" % 1 0 . 2 f % - 1 0 . 2 f % + 1 0 . 2 f \ n " " % s % 2 0 s % 2 0 . 4 s % - 2 0 . 4 s \ n "


}
COMP2300 C1: Starting Your C Journey 2010 14

For Next Lecture!

q whats the value of the following C expression?


q is the following true or false?

< | |
q are the following all equal?

<


q although it might look similar so far, remember C is not Java!

COMP2300 C1: Starting Your C Journey

2010

15

Você também pode gostar