Download presentation
Presentation is loading. Please wait.
1
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.
2
Syntax Diagrams Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Vocabulary: This lesson discusses how to read syntax diagrams. argument C99 syntax diagram
3
3 C, like most programming languages, has a strict format for writing code. The rules for writing a correctly formatted instruction is call the syntax. Syntax rules include: Correct spelling of key words Correct order of arguments Correct punctuation The official syntax rules are defined in the ANSI (American National Standards Institute) C standard, specifically the C99 standard adopted as ISO/IEC 9899:1999
4
4 Textbooks and reference books attempt to make the syntax easier to understand by providing a syntax diagram to illustrate the rules for a given instruction. Although the syntax is always the same - as defined in the C99 standard, each publisher seems to use a different format for the diagrams. Most references will include a guide to let you know how to read the diagrams. Read the guide to understand how the reference indicates required items vs option items and other syntax details. In our textbook, Appendix B includes the syntax diagrams for the language items used in the textbook. Unfortunately, the book does not include a guide to the diagrams. Or if it does, I don't see it - let me know if you find it. I will provide a guide here.
5
5 Guide to reading the syntax diagrams in Appendix B. DataType ItemName (argument [, argument, …]); stdio.h data type returned by the function correct spelling, including capitalization, of the item name list of parameters inside parentheses ellipse indicates the previous item (argument in this case) can be repeated square braces [ ] enclose optional items semicolon at end header file that defines the statement
6
6 Example: syntax diagram from our textbook printf syntax diagram from Page794 of the text int printf (const char *format [, argument, …]); stdio.h printf returns an int note the spelling and capitalization There is only 1 required argument and that is a string (identifed as "const char *format) ellipse indicates the previous item (argument in this case) can be repeated The square braces [ ] indicates that additional optional arguments may be included #include must be included in the preprocessor commands before printf can be used in a program. semicolon at end
7
int printf (const char *format [, argument, …]); NumChars = printf("Hello World"); 7 Example: syntax diagram from our textbook Note: colors are not part of the syntax. They are used here to show where they match in the examples. NumChars = printf("Total = %f", GrandTotal); printf("%d + %d = %d", Num1, Num2, Num3); Notice that the last example does not assign the returned value to a variable. That is acceptable. The value is returned but not assigned.
8
8 printf syntax from Programmer’s Reference by Herbert Schildt #include int printf (const char *format, …); shows the required header file data type returned by the function name of the function required arguments are specified by datatype ellipse indicates that optional parameters may be included It is important to be able to read syntax diagrams from several sources.
9
9 printf syntax from C Pocket Reference by Ulla Kirch-Prinz, Peter Prinz available on Safari int printf ( const char *format,... /*arg1,..., argn */ ); data type returned by the function name of the function required arguments are specified by datatype this ellipse indicates that optional parameters may be included this ellipse indicates you can have any number of arguments between arg1 and argn where n is any integer /* */ indicates that the enclosed arguments are OPTIONAL It is important to be able to read syntax diagrams from several sources.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.