Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f","— Presentation transcript:

1 C STRUCTURES

2 A FIRST C PROGRAM  #include  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 ) ;  }

3 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.

4  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

5  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

6 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

7 DECLARE A VARIABLE AND ASSIGN VALUES  int first;  float second;  char comma;  Type variablename ;  first = 100;  second=100.1;  char= l;

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

9 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

10 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

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

12  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

13  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

14 LOOPS  do …while - ran exactly one or more times  do  Statement or block  while (condition)  E.g  Do  printf ( "hello mum\n" ) ;  while (1) ;

15  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" ) ; }

16 SWITCH STATEMENT  Takes a value and decides which option to perform  Chooses the option that matches the case presented.

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

18 FUNCTIONS  float get_ranged_value ( float min, float max ) // header  {  float temp ;  do { printf ( "Give the value : " ) ;  scanf ( "%f", &temp ) ;  } while ( (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

19 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]

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

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


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

Similar presentations


Ads by Google