CS210 - the University of Auckland

CS210 - the University of Auckland

CS210 - The University of Auckland COMPSCI 210 C Programming Language Introduction to Programming in C C: a bit of history (1) y First generation computer languages: Machine code. ◦ Computer hardware is designed to understand and execute what is called “machine code” (instructions to tell the computer what to do). ◦ A computer program: the bit pattern of the machine code to be loaded into the computer memory. It could be specified manually, using switches on the front panel for the computer y Second generation computer languages: Assembly language. ◦ Specifying a computer program as the bit pattern of the machine code was very time consuming, and error prone. A human readable/writable form of machine code was developed, namely “assembly language”. ◦ Another program, called an assembler, was developed to take a textual version of the machine code, and translate it into machine code. assembler Translation from Assembly language to Machine code 1 CS210 - The University of Auckland C: a bit of history (2) y Third generation computer languages: High level languages. ◦ Assembly language is very low level, and depends on the computer architecture. ◦ High level languages were developed, that were relatively machine independent, and more like the notation of mathematics. Fortran (~1955) and Basic (~1964): still had to build control statements out of one line if statements and goto statements, and there were no recursive functions. ◦ Pascal (~1970) and C (~1972) were developed, with support for data structures. ◦ 1971, Ken Thompson developed a FORTRAN compiler, instead ended up developed a compiler for a new high-level language he called B. ◦ In 1972, Dennis Ritchie at Bell Lab, built on B to create a new language called C (next to B) which inherited Thompson's syntax. Most of the components of Unix were eventually rewritten in C. Because of its convenience and power, C went on to become the most popular programming language in the world over the next quarter century. ◦ Modern languages (third generation languages), such as C++ (~1983) and Java (~1995), are object orie nte d languages (both are based on C). Third gene rat io n languages represe nt the main programming languages in use today. From: http://phpimpact.files.wordpress.com/2008/06/program ming_languages.png C: A HighHigh--LevelLevel Language y Gives symbolic names to values ◦ don’t need to know which register or memory location ◦ e.g. ADD R1, R2, R3 vs X = Y + Z y Provides abstraction of underlying hardware ◦ operations do not depend on instruction set ◦ example: can write “a = b * c”, even though LC-3 doesn’t have a multiply instruction y Provides expressiveness ◦ use meaningful symbols that convey meaning ◦ simple expressions for common control patterns (if-then- else, while, break,…) y Enhances code readability ◦ Up to the user to write clear code (e.g. comments, code structure) 2 CS210 - The University of Auckland Compiling a C Program y Entire mechanism is usually called the “compiler” y Preprocessor ◦ macro substitution x Substitute symbolic names of constants by their values ◦ conditional compilation x Use pre-processor directives (starting with #) to define several compilation configuration by suppressing certain portion of codes ◦ “source-level” transformations x output is still C y Compiler ◦ generates object file x machine instructions y Linker ◦ combine object files (including libraries) into executable image Compiler y Source Code Analysis ◦ “front end” ◦ parses programs to ididtifentify its pieces x variables, expressions, statements, functions, etc. ◦ depends on language (not on target machine) y Code Generation ◦ “back end” ◦ generates machine code from analyzed source ◦ may optimize machine code to make it run more efficiently ◦ very dependent on target machine y Symbol Table ◦ map between symbolic names and items ◦ like assembler, but more kinds of information 3 CS210 - The University of Auckland Example: HelloWorld.c Language Structure y Structure: ◦ C is a block structured programming language, consistingggppj of one or more files making up the project. ◦ Header File Section LEA R0,MESG ; x It contains Preprocessor Directives PUTS LC3 code for printf .... ◦ Data Section (optional) MESG .STRINGZ "Hello World.\n" x It contains the #define statement to set up a constant ◦ Code Section Header files to /* The file HelloWorld program */ x It represents be included #include <stdio.h> Data Section the actual high #define CONSTANT_NAME 4 -level instructions that you want the processor int main() { to execute: printf, scanf… printf("Hello World.\n"); Main C Program Section return 0; } No semi-colon #include <stdio.h> Language Structure Before compiling, copy contents of stdio.h (standard input/output) y Header File Section (#include) into source code ◦ is a preprocessor statement (compiler directive) ◦ tells the compiler to "go get" the specified file (header file ). x is primarily a list of function, constants, and other definitions. x has a corresponding implementation file, contains function bodies. x correspond to parts of the C library. x A library is simply a package of code that someone else has written to make your life easier! Similar to API in Java x #include <stdio.h> x includes the "standard I/O library" into your program. The standard I/O library lets you read input from the keyboard and write output to the screen. ◦ can also include your own multiple source code files x with angle brackets <> to tell the compiler that it only needs to look for the file in the standard include directory. x with double quotes “” to tell the compiler that it starts to search the file from the working directory #include “library.h” 4 CS210 - The University of Auckland Language Structure y Data Section ◦ It contains the #define statement to set up a constant. ◦ Before compiling, replace all instances of the constant in the code with the value. ◦ It is highly recommended that if you have to use a constant value in your program that you define it in this section ◦ The syntax is No equal sign #define CONSTANT_NAME constant_value No semi-colon ◦ It is good programming practice that constant’s names should only contain CAPITAL LETTERS and the _ (underscore) character ◦ You can define integer, real or character constants #define INTEGER_CONSTANT 101 INTEGER_CONSTANT .FILL 101 #define REAL_CONSTANT 3.1427 #define CHARACTER_CONSTANT “hello” CHARACTER_CONSTANT .STRINGZ “hello" ◦ Later on in code body, REAL_CONSTANT will be replaced by 3.1427 LC3 code No float in LC3 int main() { printf("Hello World.\n"); Language Structure return 0; } y Code Section ◦ It represents the actual high-level instructions that you want the processor to execute. ◦ The main metho d is the starting point of your program. Every C program must have the main method. ◦ The beginning and end of the main section are delimited by chain brackets - { and } ◦ The return statement causes the function to return an error code of 0 (no error) to the operating system. x Error code of 1 – with error ◦ It is good programming practice to indent your code in this section ◦ It is good practice to include comments in you C file x Comments can be placed either x Within the bounds of /* and */ (can span multiple lines) x On a single line starting with // Main function can also take int main(int argc, char **argv) arguments like in Java {...} 5 CS210 - The University of Auckland Language Structure y Comments ◦ Begins with /* and ends with */ ◦ Can span multiple lines ◦ Cannot have a comment within a comment /* Comment out this routine Compile_error_with_comment.c for testing /* Open file */ Invalid comment if(c == 't') break; */ ◦ Comments are not recognized within a string x example: "my/*don't print this*/string" would be printed as: my/*don't print this*/string ◦ As before, use comments to help reader, not to confuse or to restate the obvious Other C Compilers y Free C Compilers ◦ The URL http://www.thefreecountry.com/compilers/cpp.shtml provides information on manyyp free C compilers. y Cygwin ◦ Install Cygwin on a Windows system. x gives you a UNIX like system running on top of Windows, and includes the GNU C compiler, “gcc”. x Information on Cygwin -> tutorial y Bloodshed, ◦ provides a free C compiler, and is available from http://www.bloodshed.net/devcpp.html. y Visual Studio ◦ Download MS Visual C++ Express edition x http://www.microsoft.com/express/ ◦ A commercial version of Visual Studio can be obtained from the University web site, https://www.se.auckland.ac.nz/msdn/ 6 CS210 - The University of Auckland Some C compilers From http://en.wikipedia.org/wiki/List_of_compilers Compiling & Running y Using Microsoft Visual ◦ Graphical interface + button controls for compiling ◦ The assignment will be run using the C compiler on Cygwin so you’d better recompile your code in UUinix bbfefore submitt ittded y Steps: ◦ Having c_code.c file handly somewhere ◦ Start Visual Studio ◦ Choose File >> New >> Project ◦ Then choose Visual C++ >> Win32 Consol Application and enter a project name ◦ Choose Application Settings >> Empty project then click Finish ◦ You then can drag and drop c_code.c file on to the project on left hand side ◦ Double click on file name to open and edit ◦ In order to compile it, click on Debug button ◦ Or press F5 ◦ Or Ctrl + F5 to compile, run and leave command window open ◦ Alternatively, you can use Visual Studio Command to compile using cl filename.c ◦ From Start >> Program files >> Visual Studio >> Visual Studio Tools >> VS command 7 CS210 - The University of Auckland ES Represents LEA R0,String ; Input and Output \a Bell (alert) PUTS \b Backspace y Variety of I/O functions in C. \f Formfeed y Must include <stdio.h> \n New line \r Carriage return printf("%s\n", String); \t Horizontal tab ◦ String contains characters to print and formatting directions \v Vertical tab for variables. \' Single quotation mark ◦ This call says to print the variable \" Double quotation mark counter as a decimal integer, \\ Backslash ollowed by a linefeed or new line (\n).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us