C STRUCTURES. A FIRST C PROGRAM #include void main ( void ) { float height, width, area,...

Post on 03-Jan-2016

224 views 1 download

Tags:

transcript

C STRUCTURES

A FIRST C PROGRAM

#include <stdio.h> void main ( void ) { float height, width, area, wood_length ; scanf ( "%f", &height ) ; scanf ( "%f", &width ) ; area = 2 * height * width ; wood_length = 2 * ( height + width ) * 3.25 ; printf ( "The area of glass is : %f metres.\n", area ) ; printf ( "The length of wood is : %f feet.\n",

wood_length ) ; }

STRUCTURES OF PROGRAM EXPLAINED #include- Not a part of the program. Tells the compiler to

get “something” – contents of a file needed for the program

stdio.h – standard input/output library <>- tells the compiler to look in the system area as

opposed to the “ “ –current directory

Void a keyword denoting nothing, tells the compiler that main returns nothing

Main- a special function. This function is ran at compilation time

(void)- tells the compiler that main works on nothing i.e. it has no parameters

{ - brace divides the program into blocks.

Float- a variable type used to store values. Three types, floats, integers and characters.

; - needed at end of all declarations in C, to keep compiler on track. Missing one causes tremendous errors.

Scanf- (scan formatted) function used for file input. The parameters are placed in brackets.

“%f “- “” tells the compiler that everything inside is used to denote th string. % tells the compiler we are giving the format of a number and f tells the compiler it is of the float type.

&height – C does not allow the value of a parameter’s value to be changed. & tells the compiler to create a pointer to the given Variable, pass it to scanf

area=2 *height*width; - this is an assignment, the result of the expression is placed in area.

Printf –does the opposite if scanf (prints to console) "The area of glass needed is : %f metres.\n", area)-

The text is printed as is without dilimiters %f tells the compiler a float will be printed, and

the variable area tells the compiler what float to print

Other types %d –signed decimal ineteger %c – print a character % 5d- decimal to five character positions % %6.2 f floating point to 6 characters, right

justified \n- move to new line

VARIABLES AND DATA

Int variables- ranges from -32768 to +32767 with no fractional parts

Float varibles – real numbers of varying precision; -3.4E-38 to +32767

Char variabes- can hold a single character

C does not provide Strings, Booleans

DECLARE A VARIABLE AND ASSIGN VALUES

int first; float second; char comma;

Type variablename ; first = 100; second=100.1; char= l;

MORE VARIABLES

typedef- Allows users to change the name of a standard data type. enum- Enumerated data types enum color{red,blue,white} struct-

Structures are heterogeneous user-defined datatypes used to create nodes and lists

` e.g. Struct bodyparts{

int feet;

double weight;

char[5] eye_color;

}

MAGIC NUMBERS

These are constants that can not be changed in a program e.g. array sizes, character positions, conversion factors, pi…

They should have their own names

We use the directive #define to manage magic numbers e.g.

#define pi 3.14159; Now whenever the compiler sees pi it will send 3.14259

EXPRESSIONS, OPERANDS AND OPERATORS

Expressions are made from Opereands and operators Operands things to work on i.e. variables Operators the workers.

E.g. 2+ 4

OPERATORS AND PRECEDENCE In Highest Priority order - unary minus Makes number negative * multiplication / division + addition - subtraction Use parenthesis to change precedence

A note ½ is not the same as ½.0 ..to overcome we use int i = 3, j = 2 ; float fraction ; fraction = (float) i / (float) j ; printf ( "fraction : %f\n", fraction ) ;

Conditional If

if (condition) statement or block we do if the condition is true else statement or block we do if the condition is false

Relational Operators

== is equal != not equal < less than > greater than <= less than or equal to >+ greater than or equal to ! Not - used for inverting && combine two statements that is both true || or one of the statements is true

LOOPS do …while - ran exactly one or more times

do Statement or block

while (condition) E.g Do printf ( "hello mum\n" ) ; while (1) ;

While Loop (runs 0 or many times)

while (condition) Statement or block

e.g. while(1)

printf (“hello mum”);

For loop – repeats for a set number of times

for ( i=0 ; i != 11 ; i = i+1 ) {

printf ( "hello\n" ) ;

}

SWITCH STATEMENT

Takes a value and decides which option to perform

Chooses the option that matches the case presented.

SWITCH STATEMENT(CONT)

void main (void) char selection ; printf ("\nEnter the type of window : ") ; scanf ("%c", &selection) ; switch (selection){ case '1' : handle_casement ();

break ;

case '2' : handle_standard () ; break ; case '3' : handle_patio () ;

break ; }

FUNCTIONS float get_ranged_value ( float min, float max ) // header { float temp ; do { printf ( "Give the value : " ) ; scanf ( "%f", &temp ) ; } while ( (temp < min) || (temp > max) ) ; return temp ; // return variable }

Header – tells the compiler that we are creating a function called get_ranged_value, that returns a float, and accepts two float values

Return statement- tells the compiler what to send back to main

A NOTE ON ARRAYS Basically a box od data with subscripts to each element Starts subscripts at 0

Type arrayname[number of ellements] e.g int x[10]; creates and empty array that can hold 10 integers

The number 10 is the subscript of the array.

The elements of the array are

X[0], x[1]…x[9]

Initializing an Array

int main() { short age[4] = {23, 34, 65, 74}; return 0; } Or int main() { short age[4]; age[0]=23; age[1]=34; age[2]=65; age[3]=74; return 0; }

Print an Array

for(j=0; j<4; j++) printf("%d\n", same_age[j])