c structures. a first c program #include void main ( void ) { float height, width, area,...

21
C STRUCTURES

Upload: morgan-susanna-patrick

Post on 03-Jan-2016

239 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

C STRUCTURES

Page 2: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 3: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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.

Page 4: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 5: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 6: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 7: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

DECLARE A VARIABLE AND ASSIGN VALUES

int first; float second; char comma;

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

Page 8: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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;

}

Page 9: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 10: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 11: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 12: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 13: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 14: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

LOOPS do …while - ran exactly one or more times

do Statement or block

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

Page 15: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

}

Page 16: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

SWITCH STATEMENT

Takes a value and decides which option to perform

Chooses the option that matches the case presented.

Page 17: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 18: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 19: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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]

Page 20: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

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

Page 21: C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",

Print an Array

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