Computing Fundamentals
Computing Fundamentals
Computing Fundamentals
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Fig. 5.4: fig05_04.c Finding the maximum of three integers */ #include <stdio.h> int maximum( int x, int y, int z ); /* function prototype */ /* function main begins program execution */ int main() { int number1; /* first integer */ int number2; /* second integer fig05_04.c */ int number3; /* third integer */ printf( "Enter three integers: " ); scanf( "%d%d%d", &number1, &number2, &number3 ); /* number1, number2 and number3 are arguments to the maximum function call */ printf( "Maximum is: %d\n", maximum( number1, number2, number3 ) ); return 0; /* indicates successful termination */
(Part 1 of 2)
23 } /* end main */ 24
25 /* Function maximum definition */ 26 /* x, y and z are parameters */ 27 int maximum( int x, int y, int z ) 28 { 29 30 31 32 33 34 35 36 37 38 39 40 41 } /* end function maximum */ return max; /* max is largest value */ Program Output if ( z > max ) { /* if z is larger than max, assign z to max */ max = z; } /* end if */ if ( y > max ) { /* if y is larger than max, assign y to max */ max = y; } /* end if */ int max = x; /* assume x is largest */
fig05_04.c (Part 2 of 2)
Enter three Maximum is: Enter three Maximum is: Enter three Maximum is:
Today's Lecture
Header Files Library Files Scope and Storage Class
Functions
1. Pre-defined Library Functions
E.g. sqrt( )
2. User-defined functions
Header Files
#include <math.h> #include <stdio.h> Custom header files
#include
Create file and Save as filename.h Load in other files with #include "filename.h"
Header Files
Contain
Function Declaration (Prototypes) Constant definitions
Function Prototypes
Return value
Constant Definitions
double pi = 3.1415926;
Then simply by including the header file in the program this value is defined and it has a meaningful name
#define
#define pi 3.1415926 Name can be used inside a program exactly like a variable It cannot be used as a variable
areaCalc.cpp
#include <stdio.h> #include areaCalc.h"
areaCalc.h
#define PI 3.14 int area (int); // constant // function prototype
void main() { int radius = 10; printf(d, area(radius)); } int area (int r) { return PI * r * r; }
.cpp
Calls function sqrt, which returns the square root of its argument All math functions return data type double
Function
sqrt( x ) exp( x ) log( x ) log10( x ) fabs( x ) ceil( x ) floor( x )
Description
square root of x exponential function ex natural logarithm of x (base e) logarithm of x (base 10) absolute value of x rounds x to the smallest integer not less than x rounds x to the largest integer not greater than x x raised to power y (xy) remainder of x/y as a floating point number trigonometric sine of x (x in radians) trigonometric cosine of x (x in radians)
Example
sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 exp( exp( log( log( 1.0 ) is 2.0 ) is 2.718282 7.389056 2.718282 7.389056 ) is 1.0 ) is 2.0
log10( 1.0 ) is 0.0 log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0 fabs( 5.0 ) is 5.0 fabs( 0.0 ) is 0.0 fabs( -5.0 ) is 5.0 ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 pow( 2, 7 ) is 128.0 pow( 9, .5 ) is 3.0 fmod( 13.657, 2.333 ) is 1.992 sin( 0.0 ) is 0.0 cos( 0.0 ) is 1.0
Explanation
Contains macros and information for adding diagnostics that aid program debugging. Contains function prototypes for functions that test characters for certain properties, and function prototypes for functions that can be used to convert lowercase letters to uppercase letters and vice versa. Defines macros that are useful for reporting error conditions. Contains the floating point size limits of the system. Contains the integral size limits of the system. Contains function prototypes and other information that enables a program to be modified for the current locale on which it is running. The notion of locale enables the computer system to handle different conventions for expressing data like dates, times, dollar amounts and large numbers throughout the world. Contains function prototypes for math library functions. Contains function prototypes for functions that allow bypassing of the usual function call and return sequence. Contains function prototypes and macros to handle various conditions that may arise during program execution. Defines macros for dealing with a list of arguments to a function whose number and types are unknown. Contains common definitions of types used by C for performing certain calculations. Contains function prototypes for the standard input/output library functions, and information used by them. Contains function prototypes for conversions of numbers to text and text to numbers, memory allocation, random numbers, and other utility functions. Contains function prototypes for string processing functions. Contains function prototypes and types for manipulating the time and date.
<string.h> <time.h>
Fig. 5.6
Identifiers
Identifier is any name user creates in a program e.g. constants and variables Functions are also identifiers Labels are also identifiers
Scope of Identifiers
Scope means visibility
E.g. variable scope means which parts of the program can access that variable
1. Local Scope
2. File Scope
Local Scope
Visible only within a block Block is code between opening brace and closing brace Function is also a block A variable declared inside a block has visibility within that block only
Example
void functionName ( ) { { int i ; } .. }
File Scope
Visible throughout a file Global variables # include < iostream > int i ;
Global variable
main() {}
Global Variable
Can be used anywhere in program Can cause logical problems if same variable name is used in local variable declarations For good programming Try to minimize the use of global variables Try to use local variables as far as possible
Visibility of Identifiers
Global Scope Anything identified or declared outside of any function is visible to all functions in that file Block or Function level scope Declaring variables inside a block or function can be used in the function only
Storage Classes
How long does an identifier stays in existence 1. Automatic (Local)
Exists during lifetime of a function in which they are defined
2. Extern (Global)
Exists for the lifetime of the program
Storage Classes
Lifetime duration between creation and destruction of a variable A local variable is not created until the function in which it is defined is called. Adv: To save memory space
Lifetime is global Used when its necessary for a function to remember a value when it is not being executed
Between function calls
Same as local, but Variables used by retains value when several functions function terminates
Example of scope
1 2 3 4 5 6 7 8 9 10 11 /* function main begins program execution */ 12 int main() 13 { 14 15 16 17 18 19 20 21 22 23 24 25 printf( "local x in outer scope of main is %d\n", x ); printf( "local x in inner scope of main is %d\n", x ); } /* end new scope */ { /* start new scope */ int x = 7; /* local variable to new scope */ printf("local x in outer scope of main is %d\n", x ); int x = 5; /* local variable to main */ int x = 1; /* global variable */ void useLocal( void ); void useGlobal( void ); /* function prototype */ /* function prototype */ void useStaticLocal( void ); /* function prototype */ /* Fig. 5.12: fig05_12.c A scoping example */ #include <stdio.h>
26 27 28 29 30 31 32 33 34 35 36
/* useLocal has automatic local x */ /* useGlobal uses global x */ /* useLocal reinitializes automatic local x */ /* global x also retains its value */
37 } /* end main */ 38 39 /* useLocal reinitializes local variable x during each call */ 40 void useLocal( void ) 41 { 42 43 44 45 46 printf( "\nlocal x in a is %d after entering a\n", x ); x++; printf( "local x in a is %d before exiting a\n", x ); int x = 25; /* initialized each time useLocal is called */
49 /* useStaticLocal initializes static local variable x only the first time 50 51 53 { 54 55 56 57 58 59 printf( "\nlocal static x is %d on entering b\n", x ); x++; printf( "local static x is %d on exiting b\n", x ); /* initialized only first time useStaticLocal is called */ static int x = 50; the function is called; value of x is saved between calls to this function */
60 } /* end function useStaticLocal */ 61 62 /* function useGlobal modifies global variable x during each call */ 63 void useGlobal( void ) 64 { 65 66 67 printf( "\nglobal x is %d on entering c\n", x ); x *= 10; printf( "global x is %d on exiting c\n", x );
References
C How to Program Deitel and Deitel