CSCI 171 Presentation 2
Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition Comments
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”); }
#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
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
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
Functions Components of a function –Prototype (signature) –Definition (header and body) –Call Functions will be discussed later –Chapter 4
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
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*/
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
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
Sample Program 2.1 //Example 2.1 #include int main( void ) { int a = 1, b = 2, c = 0; c = a + b; printf("%d", c); }
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
Variable Types Nonnumeric –character Numeric –signed, unsigned (signed is the default) –char, integer, floating point
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
Variable Types (p. 47) Var TypeKeywordBytesRange Characterchar1-128 to 127 Short Integershort to 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: e1308 Double precision floating point or long double double long double 819 digit prec. Max exp: Max value: e14392
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;
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); }
Symbolic Constants Created using the #define preprocessor directive: –#define PI no equals sign no semi-colon no variable type PI is a symbolic constant –area = PI * radius * radius; Cannot change value –compiler error
Sample Program 2.3 //Example 2.3 #include #define PI 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); }
const const double PI = ; –data type, equals sign, and semi-colon PI is a constant Can be used in formulas –area = PI * radius * radius;
Sample Program 2.4 //Example 2.4 #include int main( void ) { const double PI = ; 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); }
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
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
Conversion Specifiers Conversion char:Displays type: %ccharacter %dsigned integer %ffloat %lfdouble %sstring of text
Escape sequences Sequence:Represents: \abeep \nnew line \ttab \\backslash \”double quotes
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"); }
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); }
Statements Complete direction to carry out single task Usually one per line Whitespace ignored except in strings Ex: –x = a + b; –printf(“Hello World”);
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); }
Expressions Anything whose evaluation yields a numeric value –PI(symbolic expression defined in program) –20literal constant –ratea variable –700 / –x = a + 10 –x = 6 + (y = 4 + 5)
Operators Instructs C to perform some operation Assignment = Mathematical
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;
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)
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); }
Binary Mathematical Operators Addition+ Subtraction- Multiplication* Division/ Modulus% Ex: –int x = 100; –int y = 9; –z = x % y;(z holds the value 1)
Precedence 1.Parenthesis 2.Multiplication, division, modulus 3.Addition and subtraction Parenthesis can be used to give priority Ex: 3 * (17 +1) *
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)
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); }
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
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
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"); }