SFTW120-LectureNotes-02

SFTW120-LectureNotes-02 - SFTW120 Programming Science...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 SFTW120 Programming Science Lecture Notes 2: C: An Introduction (Textbook Chapter 2) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 2 The C language s The C language was developed in 1972 at AT&T Bell Laboratories by Dennis Ritchie for writing the Unix operating system (OS) s The language was called “C” because it was based on the older programming language B s Today C is very popular and widespread: people use C to write all kinds of programs s Different versions of C exist, most common standard version: ANSI C s C is a compiled language – to run and test a program it first has to be compiled, then executed
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 2 C program development 3 steps: 1. Edit (write the program) 2. Compile (get the program translated to machine code) 3. Run (load the program into memory and execute it) Editor Compiler source code 10101010 10101010 10101010 10101010 executable code Loader SFTW120 - Programming Science 4 Lecture Notes 2 Example of C program development 1. Edit (using the gedit editor): gedit hello.c 2. Compile (using the gcc compiler): gcc –o hello hello.c 3. Run (the operating system loads and executes the program): ./hello Editor Compiler hello.c 10101010 10101010 10101010 10101010 hello Loader gedit gcc (part of the OS)
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 2 Example: Hello, World! s The program: int main( void ) { printf( "Hello, World!\n" ); return ( 0 ); } s Compiling the program: gcc –o hello hello.c s Running the program: ./hello Hello, World! SFTW120 - Programming Science 6 Lecture Notes 2 A larger example of a C program /* Converts distance in miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main ( void ) { double miles , /* input - distance in miles. */ kms ; /* output - distance in kilometers */ /* Get the distance in miles. */ printf(" Enter the distance in miles: "); scanf(" %lf ", &miles); /* Convert the distance to kilometers. */ kms = KMS_PER_MILE * miles; /* Display the distance in kilometers. */ printf(" %f miles equals %f kilometers.\n ", miles, kms); return (0); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 SFTW120 - Programming Science 7 Lecture Notes 2 Going through the example: comments s Comments in C are started with “ /* ” and ended with */ ”: /* Converts distance in miles to kilometers. */ s Comments are not part of the program instructions, they are for helping explain the program to the programmer s Every program should comment: b Functions (what they do) b Variables (what they are for) b Complex instructions that may be difficult to understand SFTW120 - Programming Science 8 Lecture Notes 2 Preprocessor directives s Before a C program is compiled, it is preprocessed s Most C compilers automatically combine three steps: b Preprocessing the source program b Compiling the preprocessed source program b Linking the compiled program together with any other parts it uses s Preprocessors can include other program parts,
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

SFTW120-LectureNotes-02 - SFTW120 Programming Science...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online