1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver
2 Topics Structure of a C program Values and variables Expressions Function calls Comments
3 From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language Program: –talking to computer (compiler) –can be regarded as a “formal expression” of an algorithm
4 Basic Structure of a C Program output “Hello World!” Algorithm: #include int main() { printf(“Hello World!”); return 0; } C Program: Example: Hello World
5 Basic Structure of a C Program (cont) #include int main() { printf(“Hello World!”); return 0; } C Program : Example: Hello world Includes standard input/output library of procedures. Read: “Hash-include”
6 Basic Structure of a C Program (cont) #include int main() { printf(“Hello World!”); return 0; } C Program: Example: Hello world Instruction (function call) to output “Hello World!”
7 int main() { return 0; } Example -- Count to 10 Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }
8 #include int main() { return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }
9 #include /* Print out numbers 0 to 9 */ int main() { return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Comment
10 #include /* Print out numbers 0 to 9 */ int main() { int count; return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Variable declaration
11 #include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }
12 #include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Assignment of a value (right expression) to a variable (left).
13 #include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } No semi- colon here!
14 #include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }
15 #include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Format string
16 #include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }
17 Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign?
18 Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign? (cont)
19 Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( number < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign? (cont)
20 Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign? (cont)
21 Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign? (cont)
22 Topics Structure of a C program Values and variables Expressions Function calls Comments
23 Values and Variables Basic Types: –Integers –Floating point numbers –Characters –Character Strings
24 Basic Types: int and float Integers ( int ) Floating point numbers ( float ) e-1 1e1
25 Basic Types: char Characters ( char ) ’a’ ’z’ ’A’ ’Z’ ’?’ ’0’ ’9’ - Special Characters: preceded by \ ’\n’ ’\t’ ’\0’ ’\’’ ’\\’ etc.
26 Basic Types: character string Character Strings (a string of char -s) Examples: –”Hi there!” –”Line 1\nLine 2\nLine 3” –”” –”\”\””
27 Topics Structure of a C program Values and variables Expressions Function calls Comments
28 Expressions Combine values using operators and function calls Return a value of a known type
29 Arithmetic Expressions take arithmetic (numerical) values and return an arithmetic (numerical) value Are composed using the following operators: - (unary minus; a.k.a. “negation”) * (multiplication) / (division or quotient) % (modulus or remainder) + (addition) - (subtraction)
30 Precedence in Expressions Defines the order in which an expression is evaluated
31 Precedence in Expressions -- Example * / 5 = B stands for brackets, O for Order (exponents), D for division, M for multiplication, A for addition, and S for subtraction. B.O.D.M.A.S. 1 + (2 * 3) - (4 / 5)
32 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)
33 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)
34 Precedence in Expressions – Example (cont) Integer division results in integer quotient * / 5 = 1 + (2 * 3) - (4 / 5)
35 Precedence in Expressions – Example (cont) = * / 5 = 1 + (2 * 3) - (4 / 5)
36 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)
37 int -s and float -s float is a “communicable” type Example: * / 5 = 1 + (2 * 3) - (4.0 / 5) = = 6.2
38 int -s and float -s – Example 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0
39 int -s and float -s – Example 2 (cont) ( ) * (3 - 4) / 5 = (( ) * (3 - 4)) / 5 = (3.0 * -1) / 5 = -3.0 / 5 = -0.6
40 int -s and float -s – Example 3 ( ) * ((3 - 4) / 5) = ( ) * (-1 / 5) = 3.0 * 0 = 0.0
41 Evaluate an expression set result to * / 5 output result #include Example -- Simple Expressions
42 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ Example -- Simple Expressions (cont)
43 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { return 0; } Example -- Simple Expressions (cont)
44 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; return 0; } Example -- Simple Expressions (cont)
45 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; result = * / 5; return 0; } Example -- Simple Expressions (cont)
46 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; result = * / 5; printf(“%f\n”, result); return 0; } Example -- Simple Expressions (cont)
47 Topics Structure of a C program Values and variables Expressions Function calls Comments
48 Function Calls Tell the computer to execute a series of C commands and (maybe) return a value –In algorithm-speak: An invocation of a named sequence of instructions Example: printf, scanf, sqrt
49 Find the square root of x output ”Enter a number: " input x set myResult to result of squareRoot(x) output myResult Example --Find the square root
50 Example -- Find the square root (cont) #include /* Find square root */ int main() { /* declare variables */ float x,myResult; /* output ”Enter a number: " */ printf(“Enter a number\n”); /* input x */ scanf(“%f”,&x); /* set myResult to result of squareRoot(x) */ myResult=sqrt(x); /* output myResult */ printf(“Result is %f\n”,myResult); return 0; }
51 Topics Structure of a C program Values and variables Expressions Function calls Comments
52 Comments Essential for documenting programs Run from a /* to the next */ Examples: /* THIS IS A COMMENT */ /* So is this*/ /* **...and this. ** */
53 Comments (cont) Comments do not “nest” /*Comments start with a “/*” and end with a “*/” but they don’t nest! */
54 Topics Structure of a C program Values and variables Expressions Function calls Comments
55 Reading D&D: Chapter 2 –Sections 2.1 to 2.5