EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language...
-
Upload
berniece-fletcher -
Category
Documents
-
view
214 -
download
0
Transcript of EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language...
![Page 1: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/1.jpg)
EPSII
59:006
Spring 2004
![Page 2: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/2.jpg)
Introduction to C
More Administrative Details The C Programming Language How a computer processes programs Your first C program Anatomy of a C program
![Page 3: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/3.jpg)
More Administrative Details
Evening Exams Will try to have T.A.’s in lab by Monday
(1245 SC) Accessing Unix systems
![Page 4: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/4.jpg)
Course web page
http://www.engineering.uiowa.edu/~eng006
Note: use webCT4.1
Exams: concurrent exams at nightUnified HW assignmentsGrading uniform across all sections.
![Page 5: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/5.jpg)
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 responsibilityThe language’s rules allow for many different
programming techniquesThis freedom can get you into trouble!
![Page 6: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/6.jpg)
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
![Page 7: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/7.jpg)
How does a computer process a user program?
![Page 8: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/8.jpg)
A simple C program
![Page 9: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/9.jpg)
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.
%
![Page 10: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/10.jpg)
Anatomy of a C Program
![Page 11: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/11.jpg)
Anatomy of a C Program
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
![Page 12: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/12.jpg)
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;}
![Page 13: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/13.jpg)
Anatomy of a C Program
#include<stdio.h>
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.
![Page 14: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/14.jpg)
Anatomy of a C Program
Variables are symbolic names referring to storage in computer memory
All variables must be declared in Cvariable declaration:
int num; All variables must be defined before use:
variable definition:num = 1;
![Page 15: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/15.jpg)
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
![Page 16: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/16.jpg)
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
![Page 17: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/17.jpg)
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);
![Page 18: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/18.jpg)
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 */
![Page 19: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/19.jpg)
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;}
![Page 20: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/20.jpg)
Writing to the screen
Use the printf() function:”
#include <stdio.h>
printf(”This goes to the screen without a new line”);
printf(”This gets a new line\n”);
![Page 21: EPSII 59:006 Spring 2004. Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649ee95503460f94bfb002/html5/thumbnails/21.jpg)
Writing to the screen
Escape Sequences
\n new line
\a alert (rings terminal bell)
\t tab
\\ Prints a backslash
\*
*/
Start of comment
End of comment