C Structures - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

C Structures

Description:

PowerPoint Presentation ... c structures – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Slides: 22
Provided by: dama152
Category:

less

Transcript and Presenter's Notes

Title: C Structures


1
C Structures
2
A first C Program
  • include ltstdio.hgt
  • 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
  • ltgt- 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
    parameters value to be changed. tells the
    compiler to create a pointer to the given
    Variable, pass it to scanf

5
  • area2 heightwidth - 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
  • second100.1
  • char l

8
More Variables
  • typedef- Allows users to change the name of a
    standard data type.
  • enum- Enumerated data types enum
    colorred,blue,white
  • struct-
  • Structures are heterogeneous user-defined
    datatypes
  • used to create nodes and lists
  • e.g. Struct bodyparts
  • int feet
  • double weight
  • char5 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
  • lt less than
  • gt greater than
  • lt less than or equal to
  • gt 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 ( i0 i ! 11 i i1 )
  • 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 lt min) (temp gt 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 arraynamenumber of ellements
  • e.g int x10
  • creates and empty array that can hold 10 integers
  • The number 10 is the subscript of the array.
  • The elements of the array are
  • X0, x1x9

20
  • Initializing an Array
  • int main()
  • short age4 23, 34, 65, 74
  • return 0
  • Or
  • int main()
  • short age4
  • age023
  • age134
  • age265
  • age374
  • return 0

21
  • Print an Array
  • for(j0 jlt4 j)
  • printf("d\n", same_agej)
Write a Comment
User Comments (0)
About PowerShow.com