Preprocessor Directives - PowerPoint PPT Presentation

slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Preprocessor Directives PowerPoint Presentation
Download Presentation
Preprocessor Directives

play fullscreen
1 / 11
Preprocessor Directives
Download Presentation
Download Presentation

Preprocessor Directives

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Structure of a C program Preprocessor Directives Global Declarations int main(void) { } Local Declarations Executable Statements

  2. Preprocessor Directives Special instructions to the preprocessor that tell it how to prepare your program for compilation. Two common directives are #include and #define. #include <standardheader file> Tells the preprocessor that you want the header file in the pointed brackets (< >) included in your program. (e.g. stdio.h) All preprocessor directives start with a # standard library #define NAMEvalue This directive instructs the preprocessor to replace each occurrence of NAME (known as a constant macro)in the text of the C program by the value. An executing C program cannot change the value of a name defined as a constant macro.

  3. Function Main function header int main(void) { void means no parameters function body int means that the function returns an integer to the operating system } body of the function is enclosed in curly brackets The executable part of your program begins with function main. The executable part of your program begins with function main.

  4. comments /* this is a comment */ Comments begin with “/*” and end with “*/ “ The compiler ignores comments when it translates the program into executable code. Comments are used for internal program documentation and can appear anywhere in the progrma. They help explain the meaning and logic of the program to the reader. Nested comments (comments inside comments) are invalid. /* *** /*this is a comment */ *** */ Second opening token ignored This token terminates the comment This will now cause an error All of your programs should begin with documentation explaining the purpose of the program.

  5. identifiers Identifiers allow us to name data and other objects in the program. Syntax rules for naming identifiers: 1) First character must be alphabetic character or underscore. 2) Must consist only of alphabetic characters, digits, or underscores. 3) First 31 characters of an identifier are significant. 4) A C reserved word cannot be used as an identifier. Reserved words have special meaning in C and cannot be used for other purposes. auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while An identifier defined in a C Standard Library (e.g. printf, scanf) also know as standard identifiers, should not be redefined.

  6. identifiers The C compiler is case sensitive. The identifiers miles, Miles and MILES are all different identifiers. • Program Style • use lower case for all identifiers except constant macros • use underscores to separate words ( e.g. miles_per_gallon) • good identifier names are descriptive but short (don’t use • single letters for identifiers) • don’t use identifiers that can be confused in the same • program ( e.g. Miles, miles)

  7. Data types A type defines a set of values and a set of operations that can be applied on those values. Type also defines the size of the field ( number of bits) in which data can be stored. C contains four standard types: int - sort for integer. An integer is a number without a fractional part ( eg 1, 2, 3 … 456, … 6789,…) There are three different sizes of the integer data type: short int or just short int long int or just long Each type can be further subdivided into two different subtypes: signed unsigned

  8. Data types Typical integer sizes for the PC char - short for character. Holds the value of one of the characters in the ASCII (American Standard Code for Information Interchange) alphabet. (e.g. ‘a’, ‘A’, ‘b’,’?’, …) The size of char is machine dependent but normally is one byte or 8 bits.

  9. Data types float - a floating-point type is a number (real number) with a fractionalpart. (e.g. 56.24, .0058 …) which is separated by a decimal point. There are three different sizes of theFloating- point data type: float double long double Variable Declarations The memory cells used for storing a program’s input data and its computational results are called variables because the values stored in variables can change as the program executes.When we create variables, the declaration gives them a symbolic name and reserves memory for them. Each variable in your program must be delcared (defined). Variable name Syntax: intvariable_list; doublevariable_list; charvariable_list; Type of data to be stored in this variable All C statements end with a semicolon

  10. Variable Declarations Examples: double miles; float pay_rate; int class_size; long int national_debt; double gpa, accum; char class grade; Variable Initialization We can initialize a variable at the same time that we declare it by placing an equal sign and a constant after the variable name. This establishes the first value that the variable will contain. Syntax: type variable_name = constant; Examples: double miles = 0.0; float pay_rate = 4.5; int count = 0; char grade = ‘A’;

  11. Constants Constants are data values that cannot be changed during the execution of your program. A literal is an unnamed constant used to specify data. If we know the data cannot be changed, then we simply code the data value itself in a statement. C constants can be of any of the vasic data types. The way each constant is represented depends upon its type Examples: 123, -123, +123 /*examples of integers*/ 0., .005, 8769.098 /*examples of floating-point*/ ‘a’, ‘C’, ‘&’ /*examples of characters*/ Character constants are always enclosed in single quotes. Numeric constants in C are considered non-negative numbers and cannot contain commas.