1 / 21

C Structures

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 ;

cterry
Download Presentation

C Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Structures

  2. 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 ) ; • }

  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. Structbodyparts{ intfeet; 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 < 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

  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.gint 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])

More Related