You are on page 1of 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, identifiers, variables q generating output!

COMP2300 C1: Starting Your C Journey

2010 ◭◭ ◭ • ◮ ◮◮ ×

1

. ) will be easier to learn the language of computer systems. instead of objects (like Java. C++. wide usage in the “Real World” other languages (e. . which also has instructions and basic data-types (e.g. the fundamental compiled langauge helps understanding of how programs from other languages are executed 2010 ◭◭ ◭ • ◮ ◮◮ × 2 COMP2300 C1: Starting Your C Journey . Java.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. integer. csh. . byte. float) 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). Eiffel or Smalltalk) or functional relationships (like Haskell or Lisp) s 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.g.

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 ◭◭ ◭ • ◮ ◮◮ × 3 .

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 file) 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 function’s result (to the operating system) s ‘¼’ signifies normal termination COMP2300 C1: Starting Your C Journey 2010 ◭◭ ◭ • ◮ ◮◮ × 4 .

separate compilation and linking is best we will discuss this later COMP2300 C1: Starting Your C Journey 2010 ◭◭ ◭ • ◮ ◮◮ × 5 .c./helloWorld Hello World partch:˜/comp2300/C1> q with programs made of multiple C files.Compiling and Running your C Program q if your program is in a single file called 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> . create an executable program by using the following command: gcc -Wall -o helloWorld helloWorld.

underflow.[King. 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 structured. etc. inlineAssemblerEx.Why is C so popular? q small and concise (32 keywords .c q arguably the programming language for computer systems s closely tied to Unix (Linux) s system-level control (drivers.g.c s very convenient for low-level data manipulation e. table 2.1]) q portable (ANSI standard) and available (compilers for almost every platform) q efficient (compiler produces efficient machine code) s programmer has more control of data layout and object code produced s examples: optimizedMatrixMult.c.

g. . .Basic C Program Structure »∗ { Ò ÐÙ <ººº > ÒØ Ñ Ò ´ ÚÓ µ Ð Ö Ø ÓÒ× ×Ø Ø Ñ ÒØ× Ö ØÙÖÒ ¼ ∗» ÓÑÑ ÒØ Ð Ò Ö Ø Ú × ÙÒ Ø ÓÒ Ñ Ò ´µ Ú Ö Ð × Ð Ö Ò Ó Ý Ó Ñ Ò ´µ Ö ØÙÖÒ ×Ø Ø Ñ ÒØ } q we could write ÒØ Ñ Ò ´ ÚÓ µ { ÔÖ ÒØ ´ " H e l l o W o r l d \ n " µ Ö ØÙÖÒ ¼ } . a program producing this graphic) COMP2300 C1: Starting Your C Journey 2010 ◭◭ ◭ • ◮ ◮◮ × 7 . . e. . but it would be bad style! (and C allows you to do worse.

64 bit) q typeless/valueless: s ÚÓ q no Boolean data type. instead: ¼ is ‘false’ and non-¼ is ‘true’ q sizes are not explicitly defined.Basic Data Types q integer (signed or unsigned): s Ö (8 bit integer) s × ÓÖØ ÒØ (small integer) s ÒØ (standard integer) s ÐÓÒ ÒØ (long integer) q floating-point: s s ÐÓ Ø (standard precision float . but relative size is respected COMP2300 C1: Starting Your C Journey 2010 ◭◭ ◭ • ◮ ◮◮ × 8 .32 bit) ÓÙ Ð (higher precision float .

octal (leading ¼. ’012’. ’\n’ == ’\x10’) note: ’\000’ (or ’\0’) is not equal to ’0’ . ’\n’ new line. ½º¾¿ −¾) q strings s a string literal (constant) is a sequence of zero or more characters surrounded by double quotes (e. −¼Ü½) q floating point (e.g. −¼º¼¼½. −½). ’\xA’) by some escape code (e. ’q’. ’\t’ tab) as an integer (e. −¼º q characters: s s s s s by symbol (e.g. e. "\x57indows\n") COMP2300 C1: Starting Your C Journey 2010 ◭◭ ◭ • ◮ ◮◮ × 9 .g. ¼Ü . ’\%’) by ASCII code (e. ¼½ .g. 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. "COMP2300") s are automatically terminated with a null character (’\0’).g. −¼½ ) or hexadecimal (leading ¼Ü.g.g.g. ¾. e.g. ½¾¿º .Literals q integer: decimal (e.g. ’A’.

function names. ÒØ Ý Ö ÐÓ Ø Ð Ò Ø ÓÒ×Ø ÓÙ Ð Ô ¿º½ ½ Ö ÑÓÒØ ℄ " March " »» ÒÓØ COMP2300 C1: Starting Your C Journey × Ð Ò Ø 10 Ó 2010 ◭◭ ◭ • ◮ ◮◮ × . with ’ ’ separators. but should only be used with good reason variables may be qualified as ÓÒ×Ø.g. 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. followed by letters. ×Ø Ø . Ò ÈÁ ¿º½ ½ Ò ÇÍÊË "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. Ö ×Ø Ö or ÚÓÐ Ø Ð may be initialised at declaration e. macro names: s start with a letter or with ’ ’. e.Identifiers and Variables q identifiers used for variable names.g.

e.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.: ÔÖ ÒØ ´"Temperature: %d\n"¸ ¾ µ will display: Temperature: 24 2010 ◭◭ ◭ • ◮ ◮◮ × 11 COMP2300 C1: Starting Your C Journey . \ \Ò \\ \± 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 first 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 specified format) and then displays it.g.

which are displayed without being changed s format specificiers.ÔÖ ÒØ ´µ – Format strings q the format string contains: s ordinary characters. 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 ( floating point number as decimal ( character ( string (or array of characters) ( display the character % Ö) ÒØ) ÐÓ Ø or ÓÙ Ð Ö ∗) ) COMP2300 C1: Starting Your C Journey 2010 ◭◭ ◭ • ◮ ◮◮ × 12 .

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 .ÔÖ ÒØ ´µ Formats: Example printDate.

4 s \ n " ¸ Ñ ¸ Ñ ¸ Ñ ¸ Ñ µ Ö ØÙÖÒ ¼ } COMP2300 C1: Starting Your C Journey 2010 ◭◭ ◭ • ◮ ◮◮ × 14 .Fancy Formats: fancyPrintf. 2 f \ n " ¸ Ü ¸ Ü ¸ Ü µ ÔÖ ÒØ ´ " % s % 2 0 s % 2 0 .. 4 s % . 2 f % .2 0 . ch 3] and consider.1 0 .. 2 f % + 1 0 .c q look at [King. Ò ÐÙ < ×Ø Ó º > ÒØ Ñ Ò ´µ { ÒØ ¼ ÐÓ Ø Ü º¼»¿º¼ Ö Ñ ℄ " 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 " ¸ } ¸ ’A ’µ ¸ ¸ ¸ µ ÔÖ ÒØ ´ " % 1 0 .

For Next Lecture! q what’s 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 .