Thursday 3 July 2014

GE6151 Computer Programming HEADER FILES



GE6151 Computer Programming HEADER FILES

HEADER FILES

Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables.

To use any of the standard functions, the appropriate header file should be included. This is done at the beginning of the C source file. For example, to use the function printf() in a program, the line

#include <stdio.h>

should be at the beginning of the source file, because the definition for printf() is found in the file stdio.h All header files have the extension .h and generally reside in the /include subdirectory.

#include <stdio.h>

#include "mydecls.h"

The use of angle brackets <> informs the compiler to search the compilers include directory for the specified file. The use of the double quotes "" around the filename inform the compiler to search in the current directory for the specified file.

OPERATORS AND EXPRESSIONS

An ex pr e s s i o n is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof.

ARITHMETIC OPERATORS:

The symbols of the arithmetic operators are:-

Multiply

*

sum = sum * 2;

4

8

Divide

/

sum = sum / 2;

4

2

Addition

+

sum = sum + 2;

4

6

Subtraction

-

sum = sum -2;

4

2

Increment

++

++sum;

4

5

Decrement

--

--sum;

4

3

Modulus

%

sum = sum % 3;

4

1

Operation Operator Comment Value of Sum before Value of sum after

Example:

#include <stdio.h>

main()

{

int sum = 50; float modulus;

modulus = sum % 10;

printf("The %% of %d by 10 is %f\n", sum, modulus);

}

PRE/POST INCREMENT/DECREMENT OPERATORS

PRE means do the operation first followed by any assignment operation. POST means do the operation after any assignment operation. Consider the following statements

++count; /* PRE Increment, means add one to count */

count++; /* POST Increment, means add one to count */

Example:

#include <stdio.h>

main()

{

int count = 0, loop;

loop = ++count; /* same as count = count + 1; loop = count; */

printf("loop = %d, count = %d\n", loop, count);

loop = count++; /* same as loop = count; count = count + 1; */

printf("loop = %d, count = %d\n", loop, count);

}

If the operator precedes (is on the left hand side) of the variable, the operation is performed first, so the statement loop = ++count;

really means increment count first, then assign the new value of count to loop.

THE RELATIONAL OPERATORS

These allow the comparison of two or more variables.

== equal to

!= not equal

< less than

<= less than or equal to

> greater than

>= greater than or equal to

Example:

#include <stdio.h>

main() /* Program introduces the for statement, counts to ten */

{

int count;

for( count = 1; count <= 10; count = count + 1 )

printf("%d ", count );

printf("\n");

}

RELATIONALS (AND, NOT, OR, EOR)

Combining more than one condition

These allow the testing of more than one condition as part of selection statements. The symbols are

LOGICAL AND &&

Logical and requires all conditions to evaluate as TRUE (non-zero).

LOGICAL OR ||

Logical or will be executed if any ONE of the conditions is TRUE (non-zero).

LOGICAL NOT !

logical not negates (changes from TRUE to FALSE, vsvs) a condition.

LOGICAL EOR ^

Logical eor will be excuted if either condition is TRUE, but NOT if they are all true.

Example:

The following program uses an if statement with logical AND to validate the users input to be in the range 1-10.

#include <stdio.h>

main()

{

int number;

int valid = 0;

while( valid == 0 )

{

printf("Enter a number between 1 and 10 -->"); scanf("%d", &number);

if( (number < 1 ) || (number > 10) )

{

printf("Number is outside range 1-10. Please re-enter\n"); valid = 0;

}

else

valid = 1;

}

printf("The number is %d\n", number );

}

Example: NEGATION

#include <stdio.h>

main()

{

int flag = 0;

if( ! flag )

{

printf("The flag is not set.\n");

flag = ! flag;

}

printf("The value of flag is %d\n", flag);

}

Example:

Consider where a value is to be inputted from the user, and checked for validity to be within a certain range, lets say between the integer values 1 and 100.

#include <stdio.h>

main()

{

int number;

int valid = 0;

while( valid == 0 ) {

printf("Enter a number between 1 and 100"); scanf("%d", &number );

if( (number < 1) || (number > 100) )

printf("Number is outside legal range\n");

else

valid = 1;

}

 

printf("Number is %d\n", number );

}

 

No comments:

Post a Comment