Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.

Similar presentations


Presentation on theme: "CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition."— Presentation transcript:

1 CSCI 171 Presentation 2

2 Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition Comments

3 main() Required in every program –Under normal circumstances: program starts at first statement in main program ends at last statement in main –Ex: void main() { printf(“Hello World”); }

4 #include Instructs C compiler to add contents of another file during linkage #include statements at top of file Usually called header files (.h) –Some supplied with compiler Never modified –Some user defined Can be modified

5 Example of #include Library function –#include –greater than, less than indicate library function User defined function –#include “myfile.h” –quotation marks indicate user-defined function –if not in same directory as main file, need to specify the path

6 Program Statements These statements do the ‘real’ work One per line, end with semi-colon Statements perform various functions: –input/output write to screen/file/printer accept from screen/file –call functions –perform calculations –other

7 Functions Components of a function –Prototype (signature) –Definition (header and body) –Call Functions will be discussed later –Chapter 4

8 Comments No effect on program execution Any text can be stored in a comment May take up part of a line, a whole line, more than one line

9 Examples of Comments Comments may use one line, part of a line, or more than one line: /*A single line*/ for (int x; x<10; x++) /*start of a loop*/ /*Comments can span multiple lines as well*/

10 Philosophy You never have too many comments Too few comments may: –cost you points in this class –cost you friends on the job –cause gray hairs Use comments when the logic is fresh in your mind Crucial to program maintenance

11 Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type of the variable Variables MUST be declared before they are used

12 Sample Program 2.1 //Example 2.1 #include int main( void ) { int a = 1, b = 2, c = 0; c = a + b; printf("%d", c); }

13 Naming rules for variables Only certain characters are legal –letters, digits, underscore ( _ ) First character must be a letter –underscore allowed but not advised Case sensitive –count, Count are two different variables Cannot use keywords

14 Variable Types Nonnumeric –character Numeric –signed, unsigned (signed is the default) –char, integer, floating point

15 Ranges / Memory Allocation Each type has a range of numbers How you use the variables in the program will determine what type you use –memory usage (efficiency) –magnitude of range

16 Variable Types (p. 47) Var TypeKeywordBytesRange Characterchar1-128 to 127 Short Integershort2-32768 to 32767 Long Integer Or Integer long int 4-2,147,483,648 to 2,147,483,647 Singe precision floating point float415 digit prec. Max exp: 1308 Max value: 1.797693e1308 Double precision floating point or long double double long double 819 digit prec. Max exp: 14932 Max value: 1.189731e14392

17 Numeric Variables Variables need to be initialized before being used Ex 1: –int x;Sets aside storage space for x –x = 3;Stores the value 3 in x Ex 2: –int x = 3; –int x = 3, y, z = 4;

18 Sample Program 2.2 //Sample program 2.2 #include int main( void ) { char num1 = 0; int num2 = 1; double num3 = 7.2; printf("The variable num1 is in memory location: %d", &num1); printf("\nThe variable num1 utilizes %d byte(s) of memory", sizeof(num1)); printf("\nThe numeric value of num1 is: %d", num1); printf("\n\nThe variable num2 is in memory location: %d", &num2); printf("\nThe variable num2 utilizes %d byte(s) of memory", sizeof(num2)); printf("\nThe numeric value of num2 is: %d", num2); printf("\n\nThe variable num3 is in memory location: %d", &num3); printf("\nThe variable num3 utilizes %d byte(s) of memory", sizeof(num3)); printf("\nThe numeric value of num3 is: %lf", num3); }

19 Symbolic Constants Created using the #define preprocessor directive: –#define PI 3.14159 no equals sign no semi-colon no variable type PI is a symbolic constant –area = PI * radius * radius; Cannot change value –compiler error

20 Sample Program 2.3 //Example 2.3 #include #define PI 3.14159 int main( void ) { double radius = 0.0, area = 0.0; printf("Enter radius: "); scanf("%lf", &radius); area = PI * radius * radius; printf("\nThe area of your circle is: %.2lf", area); }

21 const const double PI = 3.14159; –data type, equals sign, and semi-colon PI is a constant Can be used in formulas –area = PI * radius * radius;

22 Sample Program 2.4 //Example 2.4 #include int main( void ) { const double PI = 3.14159; double radius = 0.0, area = 0.0; printf("Enter radius: "); scanf("%lf", &radius); area = PI * radius * radius; printf("\nThe area of your circle is: %.2lf", area); }

23 printf Will print information out to the screen –printf(“Hello world”); –printf(“Hello world\n”); prints Hello World and a new line –printf(“The area is %lf”, area); area must be a double prints: The area is x –x is the value held in the variable area

24 scanf Will accept information from the screen –scanf(“%d”, &area) –scanf(“%lf%lf”, &area, &diameter) Allows the user to enter a value from the keyboard –knows to wait for the enter key Stores value in corresponding variable

25 Conversion Specifiers Conversion char:Displays type: ------------------------------------------------------- %ccharacter %dsigned integer %ffloat %lfdouble %sstring of text

26 Escape sequences Sequence:Represents: ------------------------------------------------------- \abeep \nnew line \ttab \\backslash \”double quotes

27 Sample Program 2.5 //Example 2.5 #include int main( void ) { char a = 0, b = 0, name[50]; printf("Enter 2 integers separated by a comma: "); scanf("%d, %d", &a, &b); printf("Enter your name first: "); scanf(“ %s", name); printf("\nThe sum of the two numbers you entered is: %d", a + b); printf("\nThe character corresponding to ascii %d is: %c", a, a); printf("\nThe character corresponding to ascii %d is: %c", b, b); printf("\nYour name is: \"%s\"", name); printf("\n\n\aSuccessful Completion"); }

28 Sample Program 2.6 //Example 2.6 #include int main( void ) { char a = 0, b = 0; printf("Enter a letter: "); scanf(“ %c", &a); printf("Enter another letter: "); scanf(“ %c", &b); printf("The two characters you entered are: %c and %c", a, b); }

29 Statements Complete direction to carry out single task Usually one per line Whitespace ignored except in strings Ex: –x = a + b; –printf(“Hello World”);

30 Code Block 2 or more C statements enclosed in braces Allowed anywhere a single statement is allowed Usually only used where necessary Use indentation for readablility Ex: for (int x = 0; x < 5; x++) { printf(“The value of x is “); printf(“%d”, x); }

31 Expressions Anything whose evaluation yields a numeric value –PI(symbolic expression defined in program) –20literal constant –ratea variable –700 / 63 - 42 –x = a + 10 –x = 6 + (y = 4 + 5)

32 Operators Instructs C to perform some operation Assignment = Mathematical

33 Assignment Operator Evaluates the left hand side and assigns that to the variable on the right hand side Variables should be initialized at declaration time Examples of assignment operator: int x = 3; (Declaration and initialization) x = 7; y = x + 17;

34 Unary Mathematical Operators Increment++increases value by 1 Decrement--decreases value by 1 Ex: x = 10; y = x++;(post increment) Ex 2: x = 10; y = ++x;(pre-increment)

35 Sample Program 2.7 //Example 2.7 #include int main( void ) { int x = 0, y = 0; printf("Enter a number: "); scanf("%d", &x); y = x++; y = ++x; printf("\nThe value of x is: %d", x); printf("\nThe value of y is: %d", y); }

36 Binary Mathematical Operators Addition+ Subtraction- Multiplication* Division/ Modulus% Ex: –int x = 100; –int y = 9; –z = x % y;(z holds the value 1)

37 Precedence 1.Parenthesis 2.Multiplication, division, modulus 3.Addition and subtraction Parenthesis can be used to give priority Ex: 3 * 7 - 4 + (17 +1) *.5 - 17

38 Compound Assignment Operators Shorthand method to combine assignment and binary math operations General form: –exp1 op= exp2 Examples: x *= yis equivalent tox = x * (y) x – = 6 + zis equivalent to x = x – (6 + z) y % =3is equivalent to y = y % (3)

39 Sample Program 2.8 //Example 2.8 #include int main( void ) { int x = 2, y = 5, z = 13, d = 0; d = x + y * x; printf("\n%d", d); d = z % y; printf("\n%d", d); d += z; printf("\n%d", d); d *= y++; printf("\n%d", d); printf("\n%d", y); }

40 Conditional Operator The only C ternary operator (3 operands) General form: exp1 ? exp2 : exp3; exp1 is true - entire expression evaluates as exp2 exp1 is false - entire expression evaluates as exp3

41 Conditional Example x = (z < 36) ? 0: 1; –If z < 36, x is set to 0, otherwise 1 z = (x < y) ? x : y; –sets z equal to the smaller of x and y

42 Sample Program 2.9 //Example 2.9 #include int main( void ) { int x = 0; printf("Enter an integer: "); scanf("%d", &x); (x%2 == 0) ? printf("Even") : printf("Odd"); }


Download ppt "CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition."

Similar presentations


Ads by Google