Date post: | 18-Jan-2016 |
Category: |
Documents |
Upload: | hilary-wilson |
View: | 220 times |
Download: | 0 times |
CSC 107 -Programming for Science
Lecture 4:
Beginning Programming
Problem of the Day
At what times do the minute and hour hands on an analog clock line up?
The Week’s Goal
At the end of the week, you should be able to write (small, useless) C programs on your ownBut, for this week, requires some “magic”Will examine material in greater depth later in
term
Comments
Key to any program Describe code in simple English
Sie konnen auch auf Deutsch screibeno U c%d wrte n txt msg
Should be used liberally I add comments anywhere where I cannot
immediately tell what code does Impossible to have too many comments
Comments in C Program
Double slash comments out rest of linea = a – 4; // Hi, Mom!// This entire line is a comment!
/* … */ comments can go across linesa = a - /* Hi, Mom! */ 4;/* This comment takes an entire line. *//* This is a really long comment that * goes on to multiple lines. The stars on * lines 2 and on are optional, but * makes things easier to read. */
Pre-processor Directives
Code “pre-processed” before compilationNo need to request it --- automatically occursUsed to make code simpler & easier to read
Notice a recurring theme?
Pre-processor directives start with #Each directive must be on own lineDirectives should not span multiple lines
Starting a File
C code files usually named something.cSomething could be any legal name you’d like
Nearly all “*.c” files start with 2 directives: /* For now, think of these as magic */ #include <stdio.h>#include <stdlib.h>
Symbolic Constants
Directive can be used to name a constantUse name on any/all lines BELOW directive
Pre-processor replaces name with valueCompiler only sees the constant valueProgrammer only sees the nameMakes code far easier to read, write, debug
Names traditionally in all CAPITAL letters
What You Write And Work With
#define PI 3.1415962#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEarea = PI * (r * r);puts(MY_NAME);puts(DUMB_EXAMPLE);
What The Compiler Sees
#define PI 3.1415962#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEarea = PI * (r * r);puts(MY_NAME);puts(DUMB_EXAMPLE);
#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEarea = 3.1415962 * (r * r);puts(MY_NAME);puts(DUMB_EXAMPLE);
#define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEarea = 3.1415962 * (r * r);puts(MY_NAME);puts(DUMB_EXAMPLE);
#define DUMB_EXAMPLE “Matthew Hertz”area = 3.1415962 * (r * r);puts(“Matthew Hertz”);puts(DUMB_EXAMPLE);
area = 3.1415962 * (r * r);puts(“Matthew Hertz”);puts(“Matthew Hertz”);
Beginning of Every C Program
Programs must include function named mainThis is where the program start executingFunctions discussed in more detail next weekFor this week, think of this as more “magic”
More About main()
int main(int argc, char* argv[]) { // Put code here
/* Add these 2 lines at the end */ return 0;}
Variables
Variables name a memory location where program can store dataValue at memory location is initially unknownAssignments to variable update the memory
locationWhen variable used in program, computer
uses value stored at that memory location
Variable Declarations
Variables must be declared before its useDeclarations must be at start of function
Each declaration includes two pieces:Type of data that the variable storesName of the variable
Variable Names
Begin with letter or underscore (_)Then use any letters, numbers, or underscore
Names are case-sensitiveMass, mass, & masS are different
Each variable must have unique nameComputer does not know which of your 1,000
“bob” variables to use Cannot use one of C’s reserved words
List on p. 38 should say “int”, not “ints”
Variable Name Conventions
Usually begin with lowercase letterHelps clarify variables & symbolic constants
Provide good idea of what variable storesSplit multiple uses into multiple variablestmp, b, and anything you would not say in
front of your parents/priest are not good
Data Types
Each variable also has data type Specifies how program treats variable’s value
C defines 6 numeric data types Integer types: short, int, longDecimal types: float, double, long doubleDoes NOT specify ranges for each type
char data type can hold a character
Writing Variable Declarations
Single variable declared as: type name;double goodNameExample;short bad;
Declare multiple variables at one time:int i, j;long double k, l, m, n, o, p;float thisIsAReallyLongName, thisIsAnotherLongName;
Writing Variable Declarations
Could also specify initial value for variableint i = 0.0;long j = -1;long double k = -0.000123928478812;long l = j, many, minusJ = -j;char c = ‘a’;char newLine = ‘\n’;char tab = ‘\t’;
Your Turn
Divide into groups of 3 and complete the daily activity
For Next Lecture
Read through Section 2.3 of bookDo not need to understand all the detailsBut important knowing what is not understood
Review homework assignment for week 2Covers material from this week’s lectures