Presentation is loading. Please wait.

Presentation is loading. Please wait.

EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.

Similar presentations


Presentation on theme: "EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program."— Presentation transcript:

1 EPSII 59:006 Spring 2004

2 Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program Anatomy of a C program

3 More Administrative Details Evening Exams Will try to have T.A.’s in lab by Monday (1245 SC) Accessing Unix systems

4 Course web page http://www.engineering.uiowa.edu/~eng006 Note: use webCT4.1 Exams: concurrent exams at night Unified HW assignments Grading uniform across all sections.

5 Introduction to the C Programming Language C is a powerful high-level programming language that also maintains the ability to do some low-level stuff With the power of C comes both freedom and responsibility  The language’s rules allow for many different programming techniques  This freedom can get you into trouble!

6 Writing C Programs In this course you will learn one consistent approach to writing C programs (and pick up much information common to any way of writing these programs) If you try hard to conform to the guidelines presented, you will be up and running with a minimum of difficulty

7 How does a computer process a user program?

8 A simple C program

9 Compiling, linking, and running Next, compile, link, and run your program: % gcc simple.c –o simple % simple I am a simple computer. My favorite number is 1 because it is first. %

10 Anatomy of a C Program

11 Fundamental Aspect of a C Program:  Everything is a function!  Functions are blocks, and curly braces { and } delimit blocks  So, int main(void) { ……..…… return 0; } Is a function Well learn more about functions in a moment

12 Anatomy of a C Program The main () function  Tells the compiler to start with this function  Calls other functions in your program  Example: int f1(void) { return 1; } int f2(void) { return1; } int main(void) { f1(); f2(); return 0; }

13 Anatomy of a C Program #include This is actually a pre-processor statement that includes a header file (note the.h extension) which describes the standard I/O library functions (needed for printf() in this program). Anytime you use functions from external libraries you should include a header file to tell the compiler about the function you are using.

14 Anatomy of a C Program Variables are symbolic names referring to storage in computer memory All variables must be declared in C  variable declaration: int num; All variables must be defined before use:  variable definition: num = 1;

15 Anatomy of a C Program Variables  Must refer to a specific amount of computer storage (in bytes), but you don’t have to worry about the details – for now you can just use a pre-defined type. Some Predefined Variable Types  int – integer (positive or negative whole number)  char – character variable (holds a code describing a character)  double – double-precision floating point, a real number  float – single-precision floating point number

16 Anatomy of a C Program Where to Declare Variables  In C, you must declare all variables first in a function (this includes main()) Where to Define Variables  Although you can define (initialize) variables anywhere in a function, you usually do it at the top for readability

17 Anatomy of a C Program Each of these lines of code are called statements Statements always are terminated with a semicolon ‘;’ Functions can return a value: int f1(void); Or, functions can return nothing: void f1(void);

18 Anatomy of a C Program Functions also take 0 or more arguments, separated by commas:  Examples: int main (void); /* no arguments */ int f1(int); /* a single integer argument */ int f2(int, int); /* two integer arguments */

19 Example program: Adding two numbers Let’s say you want to add two numbers: int main(void) { int num1,num2,sum; num1 = 1; num2 = 2; sum = num1 + num2; printf("The sum of %d and %d is %d\n", num1, num2, sum); return 0; }

20 Writing to the screen Use the printf() function:” #include printf( ” This goes to the screen without a new line ” ); printf( ” This gets a new line\n ” );

21 Writing to the screen Escape Sequences \nnew line \aalert (rings terminal bell) \ttab \\Prints a backslash \* */ Start of comment End of comment


Download ppt "EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program."

Similar presentations


Ads by Google