100% found this document useful (1 vote)
69 views33 pages

Computing Fundamentals

The document discusses computing fundamentals including functions, header files, library files, scope and storage classes. Some key points: - Functions can be pre-defined library functions or user-defined. Header files contain function prototypes and constant definitions. - Variables can have local, global or static local scope. Local variables only exist within the block/function they are defined in. Global variables exist throughout the file. Static local variables retain their value between function calls. - Standard library headers provide function prototypes for common tasks like input/output, math operations, strings etc. - Identifiers like variables and functions have a scope and storage class determining their lifetime and visibility. This impacts how they can be accessed within

Uploaded by

Faisal Javid
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
69 views33 pages

Computing Fundamentals

The document discusses computing fundamentals including functions, header files, library files, scope and storage classes. Some key points: - Functions can be pre-defined library functions or user-defined. Header files contain function prototypes and constant definitions. - Variables can have local, global or static local scope. Local variables only exist within the block/function they are defined in. Global variables exist throughout the file. Static local variables retain their value between function calls. - Standard library headers provide function prototypes for common tasks like input/output, math operations, strings etc. - Identifiers like variables and functions have a scope and storage class determining their lifetime and visibility. This impacts how they can be accessed within

Uploaded by

Faisal Javid
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Lecture 13

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:

integers: 22 85 17 85 integers: 85 22 17 85 integers: 22 17 85 85

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

Parameter List with data types

int functionName ( int , int );

Constant Definitions
double pi = 3.1415926;

It is better to define this value in a header file

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

CircleArea = pi * radius * radius; Circumference = 2 * pi * radius;

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

gcc o executable file1.c file2.c

Math Library Functions


Math library functions
perform common mathematical calculations
#include <math.h>

Format for calling functions


FunctionName( argument );

If multiple arguments, use comma-separated list


printf( "%.2f", sqrt( 900.0 ) );

Calls function sqrt, which returns the square root of its argument All math functions return data type double

Arguments may be constants, variables, or expressions

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

pow( x, y ) fmod( x, y ) sin( x ) cos( x ) tan( x )

trigonometric tangent of x tan( 0.0 ) is 0.0 (x in radians)

Commonly used math library functions.

Standard library header


<assert.h> <ctype.h>

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.

<errno.h> <float.h> <limits.h> <locale.h>

<math.h> <setjmp.h> <signal.h> <stdarg.h> <stddef.h> <stdio.h> <stdlib.h>

<string.h> <time.h>

Fig. 5.6

Some of the standard library header.

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

Local variables or automatic variables

Example
void functionName ( ) { { int i ; } .. }

Example: Block Scope


for ( int i = 0 ; i < 10 ; i++ ) { } It is block level scope declared in for loop When for is finished i no longer exists

Identifiers Important Points


Do not create variables with same name inside blocks, inside functions or inside bigger blocks Try to use separate variable names to avoid confusion

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

Example: Global Scope


#include < iostream > int i ; //global variable void f ( void ) ; main ( ) { i = 10 ; cout<< within main i = << i ; f(); } void f ( void ) { cout<< Inside function f , i = << i ; i = 20 ; }

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

Static Local Variable


Scope of a local variable
Only visible within the function

Lifetime is global Used when its necessary for a function to remember a value when it is not being executed
Between function calls

Initialized once, not reinitialized on subsequent function calls

Scope and Storage Class


Local Visibility Lifetime Initialized value Purpose Function Function Not initialized Variables used by a single function Static Local Function Program 0 Global File Program 0

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(); useGlobal(); useLocal(); useGlobal();

/* useLocal has automatic local x */ /* useGlobal uses global x */ /* useLocal reinitializes automatic local x */ /* global x also retains its value */

useStaticLocal(); /* useStaticLocal has static local x */

useStaticLocal(); /* static local x retains its prior value */

printf( "local x in main is %d\n", x ); return 0; /* indicates successful termination */

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 */

47 } /* end function useLocal */ 48

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 */

52 void useStaticLocal( void )

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 );

68 } /* end function useGlobal */

Output of scope program


local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c global x is 10 on exiting c local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is 5

References
C How to Program Deitel and Deitel

You might also like