Скачать презентацию EPSII 59 006 Spring 2004 Introduction to Скачать презентацию EPSII 59 006 Spring 2004 Introduction to

b2bba005aaf772b7386fce33a94c514a.ppt

  • Количество слайдов: 21

EPSII 59: 006 Spring 2004 EPSII 59: 006 Spring 2004

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

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

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

Introduction to the C Programming Language C is a powerful high-level programming language that 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 n With the power of C comes both freedom and responsibility n ¨ The language’s rules allow for many different programming techniques ¨ This freedom can get you into trouble!

Writing C Programs In this course you will learn one consistent approach to writing 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) n If you try hard to conform to the guidelines presented, you will be up and running with a minimum of difficulty n

How does a computer process a user program? How does a computer process a user program?

A simple C program A simple C program

Compiling, linking, and running n Next, compile, link, and run your program: % gcc Compiling, linking, and running n 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. %

Anatomy of a C Program Anatomy of a C Program

Anatomy of a C Program n Fundamental Aspect of a C Program: ¨ ¨ Anatomy of a C Program n 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 n Well learn more about functions in a moment

Anatomy of a C Program n The main () function ¨ ¨ ¨ Tells Anatomy of a C Program n The main () function ¨ ¨ ¨ Tells the compiler to start with this function Calls other functions in your program Example: int f 1(void) { return 1; } int f 2(void) { return 1; } int main(void) { f 1(); f 2(); return 0; }

Anatomy of a C Program #include<stdio. h> n n This is actually a pre-processor Anatomy of a C Program #include n n 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.

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

Anatomy of a C Program n Variables ¨ Must refer to a specific amount Anatomy of a C Program n 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. n 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 ¨

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

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

Anatomy of a C Program n Functions also take 0 or more arguments, separated Anatomy of a C Program n Functions also take 0 or more arguments, separated by commas: ¨ Examples: int main (void); /* no arguments */ int f 1(int); /* a single integer argument */ int f 2(int, int); /* two integer arguments */

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

Writing to the screen n Use the printf() function: ” #include <stdio. h> printf(”This Writing to the screen n Use the printf() function: ” #include printf(”This goes to the screen without a new line”); printf(”This gets a new linen”);

Writing to the screen n Escape Sequences n new line a alert (rings terminal Writing to the screen n Escape Sequences n new line a alert (rings terminal bell) t tab \ Prints a backslash * */ Start of comment End of comment