1 / 18

2. C FUNDAMENTALS

2. C FUNDAMENTALS. Example: Printing a Message. /* Illustrates comments, strings, and the printf function */ #include &lt;stdio.h&gt; int main(void) { printf(&quot;To C, or not to C: &quot;); printf(&quot;that is the question.<br>&quot;); return 0; }. Comments. • Begin with /* and end with */.

braith
Download Presentation

2. C FUNDAMENTALS

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. 2. C FUNDAMENTALS

  2. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include <stdio.h> int main(void) { printf("To C, or not to C: "); printf("that is the question.\n"); return 0; }

  3. Comments • • Begin with /* and end with */. • /* This is a comment */ • • May extend over more than one line. • /* This comment starts on one line, • but ends on another. */ • • Warning: Failing to close a comment will cause the compiler to ignore part of your program. • printf("Hello"); /* forgot to close this comment... • … • printf("Goodbye"); /* so it ends here */ • • In C99, comments can also be written this way: • // This is a comment. • This type of comment is terminated by the end of the line.

  4. Example: Computing the Volume /* Illustrates the use of variables, expressions, and assignment statements */ #include <stdio.h> int main(void) { int height, length, width, volume; height = 5; length = 20; width = 10; volume = height * length * width; printf("The height is %d\n", height); printf("The length is %d\n", length); printf("The width is %d\n", width); printf("The volume is %d\n", volume); return 0; }

  5. Variables • Variables must be declared before they are used. • Variables can be declared one at a time: int height; int length; int width; int volume; or several can be declared at the same time: int height, length, width, volume;

  6. Variables • Always initialize variables before using them. Wrong: int height; printf("The height is %d\n", height); Right: int height; height = 5; printf("The height is %d\n", height); • An variable can be initialized in its declaration: int height = 5; • Variable names must be legal identifiers.

  7. Identifiers • Identifiers may contain letters, digits, and underscores. An identifier must begin with a letter or underscore. • Examples of legal identifiers: times10 get_next_char _done It’s usually best to avoid identifiers that begin with an underscore.

  8. Identifiers • Examples of illegal identifiers: 10times get-next-char • C is case-sensitive: it distinguishes between upper- and lower-case letters in identifiers. For example, the following identifiers are all different: eof eoF eOf eOF Eof EoF EOf EOF

  9. return short signed sizeof static struct switch typedef union unsigned void volatile while _Bool* _Complex* _Imaginary* *C99 only Keywords • The following keywords may not be used as identifiers: • auto • break • case • char • const • continue • default • do • double • else • enum • extern • float • for • goto • if • inline* • int • long • register • restrict*

  10. Keywords • Keywords (with the exception of _Bool, _Complex, and _Imaginary) and names of library functions (e.g., printf) must be written using only lowercase letters.

  11. The General Form of a Simple Program • The simplest C programs have the following form: int main(void) { declarations statements } The word int may be omitted (although it’s good practice to include it). The use of void is also optional. Each declaration and each statement (except for compound statements) must end with a semicolon.

  12. The General Form of a Simple Program • C99 does not require that declarations precede statements. However, each variable must be declared before it is used for the first time

  13. Program Layout • C programs are—for the most part—“free-format.” • Spaces and newline characters can be deleted, except where this would cause two symbols to merge into one: #include <stdio.h> int main(void){int height,length,width,volume;height=5; length=20;width=10;volume=height*length*width; printf("The height is %d\n",height); printf("The length is %d\n",length); printf("The width is %d\n",width); printf("The volume is %d\n",volume);return 0;}

  14. Program Layout Extra spaces and newline characters can be inserted, except where this would divide a symbol: #include <stdio.h> int main ( void ) { int height, length, width, volume ; height = 5 ; length = 20 ; width = 10 ; volume = height * length * width ; printf ( "The height is %d\n" , height ) ; printf ( "The length is %d\n" , length ) ; printf ( "The width is %d\n" , width ) ; printf ( "The volume is %d\n" , volume ) ; return 0; } Note: Each command that begins with # must be on a separate line.

  15. Example: Converting Fahrenheit to Celsius /* Illustrates macro definition, floating-point numbers, and scanf */ #include <stdio.h> #define FREEZING_PT 32.0 #define SCALE_FACTOR (5.0 / 9.0) int main(void) { float fahrenheit, celsius; printf("Enter Fahrenheit temperature: "); scanf("%f", &fahrenheit); celsius = SCALE_FACTOR * (fahrenheit - FREEZING_PT); printf("\nCelsius equivalent is: %.1f\n", celsius); return 0; }

  16. Defining Constants • A macro definition has the form #define identifier replacement-list • Macros often represent constants: values that won’t change during program execution: #define N 100 • Macro definitions are handled by the C preprocessor. Wherever the identifier appears later in the source file, the preprocessor replaces it by the replacement list.

  17. Defining Constants If the replacement list contains operators, it is best to enclose it in parentheses. #define EOF (-1) #define TWO_PI (2*3.14159) By convention, names of C constants contain only upper-case letters. Advantages of defining constants: Programs are easier to read. Programs are easier to modify. Helps reduce errors of inconsistent use, as well as typographical errors.

  18. In Class Examples • Print to the screen • Print n, n2 and n3 for a value n entered by the user. • Calculate and print the surface area and volume for a box with dimensions entered by the user • Calculate and print net and gross pay

More Related