16
Lecture 2: Introduction to C Programming

Lecture 2: Introduction to C Programming

  • Upload
    lecea

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 2: Introduction to C Programming. OBJECTIVES. In this lecture you will learn: To use simple input and output statements. The fundamental data types. Computer memory concepts. To use arithmetic operators. The precedence of arithmetic operators. - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 2:  Introduction to C Programming

Lecture 2: Introduction to C Programming

Page 2: Lecture 2:  Introduction to C Programming

OBJECTIVES

In this lecture you will learn: To use simple input and output statements. The fundamental data types. Computer memory concepts. To use arithmetic operators. The precedence of arithmetic operators. To write simple decision-making statements.

Page 3: Lecture 2:  Introduction to C Programming

Example

Problem: Determine if a user-entered number is odd.Questions:

How to enter a number?Where to store the number?Under what condition is a number is odd?

Page 4: Lecture 2:  Introduction to C Programming

Definition of variable

scanf obtains a value from the user and assigns it to integer1

/* Determine if a user-entered number is odd. */

#include <stdio.h>

/* function main begins program execution */int main( void ){ int integer1; /* the number to be input by user */

printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */

/* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 );

return 0; /* indicate that program ended successfully */

} /* end function main */

Checked if integer1 is odd

Page 5: Lecture 2:  Introduction to C Programming

Comments: Used to describe program

#include <stdio.h>: <stdio.h> allows standard input/output operations

int main()C programs contain one or more functions, exactly one of which must be mainint means that main "returns" an integer valueThe execution of any C program starts from main

/* Determine if a user-entered number is odd. */

#include <stdio.h>

/* function main begins program execution */int main( void ){ int integer1; /* the number to be input by user */

printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */

/* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 );

return 0; /* indicate that program ended successfully */

} /* end function main */

Page 6: Lecture 2:  Introduction to C Programming

int integer1; Definition of a variable: location in memory where a value can be stored.int means the variable can hold an integerVariable name (identifier)

integer1Identifier: consist of letters, digits (cannot begin with a digit) and underscores( _ )Case sensitive

Definitions appear before executable statements

/* Determine if a user-entered number is odd. */

#include <stdio.h>

/* function main begins program execution */int main( void ){ int integer1; /* the number to be input by user */

printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */

/* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 );

return 0; /* indicate that program ended successfully */

} /* end function main */

Page 7: Lecture 2:  Introduction to C Programming

scanf( "%d", &integer1 ); Obtains a value from the user -- uses standard input (usually keyboard)Two arguments

%d - conversion specifier: data should be a decimal integer (“%d” is the format control string)&integer1 - location in memory to store variable

When executing the program the user responds to the scanf statement by typing in a number, then pressing the enter (return) key

/* Determine if a user-entered number is odd. */

#include <stdio.h>

/* function main begins program execution */int main( void ){ int integer1; /* the number to be input by user */

printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */

/* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 );

return 0; /* indicate that program ended successfully */

} /* end function main */

Page 8: Lecture 2:  Introduction to C Programming

if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 );

Simple version of the if statement, more detail next lectureIf a condition is true, then the body of the if statement executed

0 is false, non-zero is true.Control always resumes after the if structure

/* Determine if a user-entered number is odd. */

#include <stdio.h>

/* function main begins program execution */int main( void ){ int integer1; /* the number to be input by user */

printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */

/* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 );

return 0; /* indicate that program ended successfully */

} /* end function main */

Page 9: Lecture 2:  Introduction to C Programming

printf( "The entered number %d is odd.\n", integer1 );

Similar to scanf"The entered number %d is odd.\n” - printf format control string.%d - conversion specifier: means decimal integer will be printed

/* Determine if a user-entered number is odd. */

#include <stdio.h>

/* function main begins program execution */int main( void ){ int integer1; /* the number to be input by user */

printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */

/* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 );

return 0; /* indicate that program ended successfully */

} /* end function main */

Page 10: Lecture 2:  Introduction to C Programming

Memory Concepts

VariableVariable names correspond to locations in the computer's memoryEvery variable has a name, a type, and a valueWhenever a new value is placed into a variable (through scanf, for example), it replaces (and destroys) the previous valueReading variables from memory does not change them

……

integer1

integer2

sum

2000

500

2500

RAM

Page 11: Lecture 2:  Introduction to C Programming

Arithmetic

Arithmetic calculationsUse * for multiplication and / for divisionInteger division truncates remaindere.g. 9 / 5 evaluates to 1.Modulus operator (%) returns the remaindere.g. 9 % 5 evaluates to 4.

Operator precedenceUsed to decide which of two operators should be processed first. Parentheses () Multiplication/Division/Remainder Addition/Subtraction

Use parenthesis when neededE.g.: Find the average of three variables a, b, and cusing (a + b + c)/3, not a + b + c/3

Page 12: Lecture 2:  Introduction to C Programming

Arithmetic

Operator associativityused to decide which of two operators should be processed when both operators have same precedence. Multiplication/Division: Left to Right Addition/Subtraction: Left to Right

Examples:a - b + c = ((a - b) + c)a * b % c = ((a * b) % c)a^b^c = (a^(b^c))

Page 13: Lecture 2:  Introduction to C Programming

Some Arithmetic Operators

C opetration Arithmetic operator

Algebraic expression

C expression

Addition + f + 7 f + 7

Subtraction – p – c p - c

Multiplication * bm b * m

Division /

or orx

xy x ÷ yy

x / y

Remainder % r mod s r % s

Page 14: Lecture 2:  Introduction to C Programming

Some Arithmetic Operators

Operator(s) Operation(s) Order of evaluation (precedence)

( ) Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” ( i.e., not nested), they are evaluated left to right.

* / %

Multiplication Division Remainder

Evaluated second. If there are seve ral, they are evaluated left to right.

+ -

Addition Subtraction

Evaluated last. If there are several, they are evaluated left to right.

Page 15: Lecture 2:  Introduction to C Programming

Equality and Relational Operators

Standard algebraic equality operator or relational operator

C equality or relational operator

Example of C condition

Meaning of C condition

Equality operators

= == x == y x is equal to y

≠ != x != y x is not equal to y

Relational operator s

> > x > y x is greater than y

< < x < y x is less than y

≥ >= x >= y x is greater than or equal to y

≤ <= x <= y x is less than or equal to y

Page 16: Lecture 2:  Introduction to C Programming

Keywords

Special words reserved for CCannot be used as identifiers or variable names

Keywords

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while