Académique Documents
Professionnel Documents
Culture Documents
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!
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)
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)
History of C
1960s:
1970s: B
C (K & R)
1980s: C (ANSI)
C++
Objective C
1990s: Java
2010
" 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
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
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
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
< >
q we could write
{ " H e l l o W o r l d \ n "
2010
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
"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.
10
2010
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
\ \ \ \
\ \ \"
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
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
) or )
2010
12
< > " 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 "
}
2010
13
< > {
" 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 "
}
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!
2010
15