Unformatted text preview:

2. C FUNDAMENTALSExample: Printing a MessageCommentsExample: Computing the VolumeVariablesSlide 6IdentifiersSlide 8KeywordsSlide 10The General Form of a Simple ProgramSlide 12Program LayoutSlide 14Example: Converting Fahrenheit to CelsiusDefining ConstantsSlide 172. C FUNDAMENTALSExample: 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;}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.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;}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;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 initialized in its declaration: int height = 5;• Variable names must be legal identifiers.Identifiers• Identifiers may contain letters, digits, and underscores. An identifier must begin with a letter or underscore.• Examples of legal identifiers:times10get_next_char_doneIt’s usually best to avoid identifiers that begin with an underscore.Identifiers• Examples of illegal identifiers:10timesget-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 EOFKeywordsautobreakcasecharconstcontinuedefaultdodoubleelseenumexternfloatforgotoifinline*intlongregisterrestrict*returnshortsignedsizeofstaticstructswitchtypedefunionunsignedvoidvolatilewhile_Bool*_Complex*_Imaginary**C99 only• The following keywords may not be used as identifiers: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.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.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 timeProgram 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;}Program LayoutExtra 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.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;}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.Defining ConstantsIf 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


View Full Document

UF CGS 3460 - C FUNDAMENTALS

Download C FUNDAMENTALS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C FUNDAMENTALS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C FUNDAMENTALS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?