Download presentation
Presentation is loading. Please wait.
Published byHilary Wilson Modified over 9 years ago
1
CSC 107 - Programming for Science Lecture 4: Beginning Programming
2
Problem of the Day At what times do the minute and hour hands on an analog clock line up?
3
The Week’s Goal At the end of the week, you should be able to write (small, useless) C programs on your own But, for this week, requires some “magic” Will examine material in greater depth later in term
4
Comments Key to any program Describe code in simple English Sie konnen auch auf Deutsch screiben o 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
5
Comments in C Program Double slash comments out rest of line a = a – 4; // Hi, Mom! // This entire line is a comment! /* … */ comments can go across lines a = 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. */
6
Pre-processor Directives Code “pre-processed” before compilation No need to request it --- automatically occurs Used to make code simpler & easier to read Notice a recurring theme? Pre-processor directives start with # Each directive must be on own line Directives should not span multiple lines
7
Starting a File C code files usually named something.c Something could be any legal name you’d like Nearly all “ *.c ” files start with 2 directives: /* For now, think of these as magic */ #include #include
8
Symbolic Constants Directive can be used to name a constant Use name on any/all lines BELOW directive Pre-processor replaces name with value Compiler only sees the constant value Programmer only sees the name Makes code far easier to read, write, debug Names traditionally in all CAPITAL letters
9
What You Write And Work With #define PI 3.1415962 #define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME area = PI * (r * r); puts(MY_NAME); puts(DUMB_EXAMPLE);
10
What The Compiler Sees #define PI 3.1415962 #define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME area = PI * (r * r); puts(MY_NAME); puts(DUMB_EXAMPLE); #define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME area = 3.1415962 * (r * r); puts(MY_NAME); puts(DUMB_EXAMPLE); #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME area = 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”);
11
Beginning of Every C Program Programs must include function named main This is where the program start executing Functions discussed in more detail next week For this week, think of this as more “magic”
12
More About main() int main(int argc, char* argv[]) { // Put code here /* Add these 2 lines at the end */ return 0; }
13
Variables Variables name a memory location where program can store data Value at memory location is initially unknown Assignments to variable update the memory location When variable used in program, computer uses value stored at that memory location
14
Variable Declarations Variables must be declared before its use Declarations must be at start of function Each declaration includes two pieces: Type of data that the variable stores Name of the variable
15
Variable Names Begin with letter or underscore (_) Then use any letters, numbers, or underscore Names are case-sensitive Mass, mass, & masS are different Each variable must have unique name Computer 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”
16
Variable Name Conventions Usually begin with lowercase letter Helps clarify variables & symbolic constants Provide good idea of what variable stores Split multiple uses into multiple variables tmp, b, and anything you would not say in front of your parents/priest are not good
17
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, long Decimal types: float, double, long double Does NOT specify ranges for each type char data type can hold a character
18
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;
19
Writing Variable Declarations Could also specify initial value for variable int 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’;
20
Your Turn Divide into groups of 3 and complete the daily activity
21
For Next Lecture Read through Section 2.3 of book Do not need to understand all the details But important knowing what is not understood Review homework assignment for week 2 Covers material from this week’s lectures
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.