Top Banner
1 CSE 324 - Computer Programming “Using C-Language” Lectures 2
51
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: lec2_Cintro

1

CSE 324 - Computer Programming

“Using C-Language”

Lectures 2

Page 2: lec2_Cintro

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

Page 3: lec2_Cintro

High-Level Language

• Compilers and linkers translate a high level program into executable machine code.

#include <stdio.h>

int main(){printf(“Hello World”);

return 0;}

Source code Executable code

10100110 0111011000100110 0000000011111010 1111101001001110 1010011011100110 1001011011001110 0010111010100110 0100111011111010 0110011001001110 10000110

etc...

Page 4: lec2_Cintro

Why C?

• Flexible language:– Structured language– Low level activities possible

• Standard library exists, allowing portability• It can produce lean and efficient code• Wide availability on a variety of computers• Widely used

Page 5: lec2_Cintro

History of C

• Developed in 1972 by Dennis Ritchie on a DEC PDP11 at Bell Systems Lab as a system development language– Derived from the language B of Ken Thompson, which itself was based

on BCPL, developed by Martin Richards• For many years the de-facto C standard was the version provided

with Unix System V– The C ProgramminLanguage, Brian Kernigham and Dennis Ritchie,

Prentice-Hall 1978• In 1983 ANSI creates a group to begin the standardization of C

– ANSI C is finalized in 1989 (C89), and ISO adopts it in 1990– ANSI updated the standard and C99 was adopted in 2000

Page 6: lec2_Cintro

Basic Structure of a C Program

output “Hello World!”

Algorithm:#include <stdio.h>

int main(){printf(“Hello World!”);

return 0;}

C Program:

Example: Hello World

Page 7: lec2_Cintro

Basic Structure of a C Program (cont)

#include <stdio.h>

int main(){

printf(“Hello World!”);

return 0;}

C Program:

Example: Hello world

Includes declarations for the standard input/output library of procedures.Read: “Hash-include”

Page 8: lec2_Cintro

Basic Structure of a C Program

#include <stdio.h>

int main(){

printf(“Hello World”);

return 0;}

C Program:

Curly braces mark the beginning and end of a

block of instructions.

Example: Hello World

Page 9: lec2_Cintro

Basic Structure of a C Program

#include <stdio.h>

int main(){

printf(“Hello World”);

return 0;}

C Program:

Instruction (function call) to output “Hello World”

Example: Hello World

Page 10: lec2_Cintro

Basic Structure of a C Program

#include <stdio.h>

int main(){

printf(“Hello World”);

return 0;}

C Program:

“Statements” (lines of instructions) always end with a semi-colon (;)

Example: Hello World

Page 11: lec2_Cintro

int main(){

return 0;}

Example -- Count to 10

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 12: lec2_Cintro

#include <stdio.h>

int main(){

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 13: lec2_Cintro

#include <stdio.h>

/* Print out numbers 0 to 9 */

int main(){

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Comment

Page 14: lec2_Cintro

#include <stdio.h>

/* 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 0while ( count is less than 10 ){

output countadd 1 to count

}

Variable declaration

Page 15: lec2_Cintro

#include <stdio.h>

/* 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 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 16: lec2_Cintro

#include <stdio.h>

/* 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 0while ( count is less than 10 ){

output countadd 1 to count

}

Assignment of a value (right expression) to a variable (left).

Page 17: lec2_Cintro

#include <stdio.h>

/* 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 0while ( count is less than 10 ){

output countadd 1 to count

} No semi-colon here!

Page 18: lec2_Cintro

#include <stdio.h>

/* 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 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 19: lec2_Cintro

#include <stdio.h>

/* 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 0while ( count is less than 10 ){

output countadd 1 to count

}

Format string

Page 20: lec2_Cintro

#include <stdio.h>

/* 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 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 21: lec2_Cintro

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 <stdio.h>/* 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?

Page 22: lec2_Cintro

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 <stdio.h>/* 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)

Page 23: lec2_Cintro

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 <stdio.h>/* 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)

Page 24: lec2_Cintro

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 <stdio.h>/* 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)

Page 25: lec2_Cintro

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 <stdio.h>/* 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)

Page 26: lec2_Cintro

C Values and Variables

• Basic Types:– Integers– Floating point numbers– Characters– Character Strings

Page 27: lec2_Cintro

Basic Types: int and float

• Integers (int)0 1 1000 -1 -10 666

• Floating point numbers (float)1.0 .1 1.0e-1 1e1

Page 28: lec2_Cintro

Basic Types: char

• Characters (char)’a’ ’z’ ’A’ ’Z’ ’?’ ’@’ ’0’ ’9’- Special Characters: preceded by\’\n’ ’\t’ ’\0’ ’\’’ ’\\’ etc.

Page 29: lec2_Cintro

Basic Types: character string

• Character Strings (a string of char-s)• Examples:

– ”Hi there!”– ”Line 1\nLine 2\nLine 3”– ””– ”\”\””

Page 30: lec2_Cintro

Arithmetic Expressions• take arithmetic (numerical) values and• return an arithmetic (numerical) value• Are composed using the following operators:

+ (unary plus)- (unary minus)+ (addition)- (subtraction)* (multiplication)/ (division or quotient)% (modulus or remainder)

Page 31: lec2_Cintro

Precedence in Expressions

• Defines the order in which an expression is evaluated

Page 32: lec2_Cintro

Precedence in Expressions -- Example

1 + 2 * 3 - 4 / 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)

Page 33: lec2_Cintro

More on precedence

• *, /, % are at the same level of precedence • +, - are at the same level of precedence • For operators at the same “level”, left-to-right

ordering is applied.2 + 3 – 1 = (2 + 3) – 1 = 42 – 3 + 1 = (2 – 3) + 1 = 0

2 * 3 / 4 = (2 * 3) / 4 = 6 / 42 / 3 * 4 = (2 / 3) * 4 = 0 * 4

Page 34: lec2_Cintro

Precedence in Expressions –Example (cont)

6.2

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 35: lec2_Cintro

Precedence in Expressions –Example (cont)

6.2

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 36: lec2_Cintro

Precedence in Expressions –Example (cont)

Integer division results in integer quotient

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 37: lec2_Cintro

Precedence in Expressions –Example (cont)

= 0D’oh

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 38: lec2_Cintro

Precedence in Expressions –Example (cont)

7

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 39: lec2_Cintro

int-s and float-s

• float is a “communicable” type• Example:

1 + 2 * 3 - 4.0 / 5

= 1 + (2 * 3) - (4.0 / 5)

= 1 + 6 - 0.8

= 6.2

Page 40: lec2_Cintro

int-s and float-s – Example 2

(1 + 2) * (3 - 4) / 5

= ((1 + 2) * (3 - 4)) / 5

= (3 * -1) / 5

= -3 / 5

= 0

Page 41: lec2_Cintro

int-s and float-s – Example 2 (cont)

(1 + 2.0) * (3 - 4) / 5

= ((1 + 2.0) * (3 - 4)) / 5

= (3.0 * -1) / 5

= -3.0 / 5

= -0.6

Page 42: lec2_Cintro

int-s and float-s – Example 3

(1 + 2.0) * ((3 - 4) / 5)

= (1 + 2.0) * (-1 / 5)

= 3.0 * 0

= 0.0

Page 43: lec2_Cintro

Unary operators

• Called unary because they require one operand.• Example

i = +1; /* + used as a unary operator */j = -i; /* - used as a unary operator */

• The unary + operator does nothing – just emphasis that a numeric constant is positive.

• The unary – operator produces the negative of its operand.

Page 44: lec2_Cintro

Increment and decrement operators

• ++ is the increment operator i++;

is equivalent toi = i + 1;

• -- is the decrement operatorj--;

is equivalent toj = j - 1;

(King, pp53-54)

Page 45: lec2_Cintro

Evaluate an expression

set result to 1 + 2 * 3 - 4 / 5

output result

#include <stdio.h>

Example -- Simple Expressions

Page 46: lec2_Cintro

Evaluate an expression

set result to 1 + 2 * 3 - 4 / 5

output result

#include <stdio.h>

/* Evaluate an expression */

Example -- Simple Expressions (cont)

Page 47: lec2_Cintro

Evaluate an expression

set result to 1 + 2 * 3 - 4 / 5

output result

#include <stdio.h>

/* Evaluate an expression */

int main(){

return 0;}

Example -- Simple Expressions (cont)

Page 48: lec2_Cintro

Evaluate an expression

set result to 1 + 2 * 3 - 4 / 5

output result

#include <stdio.h>

/* Evaluate an expression */

int main(){float result;

return 0;}

Example -- Simple Expressions (cont)

Page 49: lec2_Cintro

Evaluate an expression

set result to 1 + 2 * 3 - 4 / 5

output result

#include <stdio.h>

/* Evaluate an expression */

int main(){float result;

result = 1 + 2 * 3 - 4 / 5;

return 0;}

Example -- Simple Expressions (cont)

Page 50: lec2_Cintro

Evaluate an expression

set result to 1 + 2 * 3 - 4 / 5

output result

#include <stdio.h>

/* Evaluate an expression */

int main(){float result;

result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);

return 0;}

Example -- Simple Expressions (cont)

Page 51: lec2_Cintro

Evaluate an expression

set result to 1 + 2 * 3 - 4 / 5

output result

Output:7.000000

#include <stdio.h>

/* Evaluate an expression */

int main(){float result;

result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);

return 0;}

Example -- Simple Expressions (cont)