c programming language - lecture 1

41
C Programming Language Lecture 1

Upload: cami-rosca

Post on 23-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming Language - Lecture 1

C Programming Language

Lecture 1

Page 2: C Programming Language - Lecture 1

C Programming Language - Lecture 1 2

C Programming Language

• Lecture content– The evolution of C– Conformance– Strengths and weaknesses– Overview of C programming– Lexical elements– The C preprocessor– Q&A

Page 3: C Programming Language - Lecture 1

C Programming Language - Lecture 1 3

The evolution of CWhen Who, where Comments

mid-1960 Ken Thompson, Bell Laboratories

Developed B language (based on BCPL which in turn is based on Algol-60) for UNIX Operating System developmentA higher-level programming language was needed for further development of the system (prior, assembly language was used)

1971 Denis Ritchie, Bell Laboratories

Developed an extended version of B called NB (New B) and then changed its name into C

1973 - UNIX was rewritten in C

1978 Brian Kernighan and Denis Ritchie

The C Programming Language – 1st edition, referred as K&R - it was the de facto standard

Page 4: C Programming Language - Lecture 1

C Programming Language - Lecture 1 4

The evolution of CWhen Comments

1983 ANSI starts the standardization process of the C programming language

1989 Standard adopted as ANSI Standard X3.159-1989 – ANSI-C

1990 Standard adopted by ISO as an international standard ISO/IEC 9899:1990 – C89 or C90

1995 Amendment 1 – extension to C89 – C89 with Amendament or C95

1999 C standard adopted as ISO/IEC 9899:1999 – C99Changes are “in the spirit of C” they do not change the fundamental nature of the languageC99 isn’t yet universal and it will take time before all compilers are C99 compliant

2011 New C standard adopted as ISO/IEC 9899:2011 – C11 (or C1X)http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

For more information about C standards, follow the link below http://www.open-std.org/JTC1/SC22/WG14/

Page 5: C Programming Language - Lecture 1

C Programming Language - Lecture 1 5

Conformance

• Both C programs and C implementations can conform to Standard C:– A C program is said to be strictly conforming to

Standard C if that program uses only the features of the language and library described in the Standard

• The program's operation must not depend on any aspect of the C language that the Standard characterizes as unspecified, undefined, or implementation-defined

Page 6: C Programming Language - Lecture 1

C Programming Language - Lecture 1 6

Conformance

• There are two kinds of conforming implementations:– Hosted implementation – it accepts any conforming

program– Freestanding implementation – accepts any conforming

program that uses no library facilities other than those provided in the header files float.h, iso646.h , limits.h, stdarg,h, stdbool.h, stddef .h and stdint.h

• Freestanding conformance is meant to accommodate C implementations for embedded systems or other target environments with minimal run-time support. For example, such systems may have no file system

Page 7: C Programming Language - Lecture 1

C Programming Language - Lecture 1 7

Strengths and Weaknesses

• C is a low level programming language:– Is suitable for system programming– Provides access to machine level concepts (bytes and addresses)– Provides operations that correspond closely to a computer’s built-

in instructions• C is a small language

– Provides a limited set of features than other languages– It relies on a library of standard functions

• C is permissive– Does not implement detailed error-checking mechanisms– It assumes that programmer knows what hi is doing so it allows a

wider degree of freedom than other languages

Page 8: C Programming Language - Lecture 1

C Programming Language - Lecture 1 8

Strengths and WeaknessesStrengths Weaknesses

Efficiency: programs execute fast and use limited amounts of memory

Programs can be error-prone: C allows some programming mistakes to go undetected (e.g. the use of & operator)

Portability: programs may run on different computing systems

Programs can be difficult to understand: C allows to write programs in a quite cryptic manner (e.g. a[(i>>1) + j] += i>>j;)

Power: C has a large collection of types and operators

Programs can be difficult to modify: Large programs can be hard to modify if they haven’t been designed with maintenance in mind

Flexibility: C may be used to write programs for embedded systems or for commercial data processing systemsStandard library: has plenty of functions for I/O, string processing, storage allocation, etc.

Page 9: C Programming Language - Lecture 1

C Programming Language - Lecture 1 9

Overview of C programmingEXAMPLE

#include <stdio.h>

#define SIZE = 10 int size(int a[SIZE]){ int ret; ret=printf("size of array is:%d\n", sizeof(a)); return ret;}

int main(){ int a[SIZE]; (void)size(a); return 0;}

Q: Will the program compile?Q: What would be the output of the program?Q: What would be the value returned by the function size?

• A C program is composed of one or more source files or translation units, each of which contains some part of the entire C program: typically some number of external functions

• Common declarations are often collected into header files and are included into the source files with a special #include command

• One external function must be named main and this function is where the program starts

Page 10: C Programming Language - Lecture 1

C Programming Language - Lecture 1 10

Overview of C programming

C source file

Compile

Link

Object file

Executable module

Library

C source file

Compile

Object file

• A C compiler independently processes each source file and translates the C program text into instructions understood by the computer

• The output of the compiler is usually called object code or an object module

• When all source files are compiled, the object modules are given to a program called the linker

• The linker resolves references between the modules, adds functions from the standard run-time library

• The linker produces a single executable program which can then be invoked or run

Page 11: C Programming Language - Lecture 1

C Programming Language - Lecture 1 11

Lexical elements

Class Characters52 Latin capital and small letters A B C D E F G H I J K L M N O

P Q R S T U V W X Y Z a b c d e f g h i j k 1 m n o p q r s t u v w x y z

10 digits 0 1 2 3 4 5 6 7 8 9 SPACEhorizontal tab (HT), vertical tab (VT), and form feed (FF) control characters

• A C source file is a sequence of characters selected from a character set • C programs are written using the following characters (source character

set) defined in the Basic Latin block of ISO/IEC 10646

Page 12: C Programming Language - Lecture 1

C Programming Language - Lecture 1 12

Lexical elementsClass Characters29 graphic characters and their official names

! +“ #={%~}^[,&].

EXCLAMATION MARKPLUS SIGN QUOTATION MARKNUMBER SIGNEQUALS SIGN LEFT CURLY BRACKET PERCENT SIGN TILDERIGHT CURLY BRACKET CIRCUMFLEX ACCENT LEFT SQUARE BRACKET COMMA AMPERSAND RIGHT SQUARE BRACKET FULL STOP

*‘<(|>_\/);?-:

ASTERISK APOSTROPHELESS-THAN SIGN LEFT PARENTHESIS VERTICAL LINE GREATER-THAN SIGN LOWLINE (underscore)REVERSE SOLIDUS (backslash)SOLIDUS (slash, divide sign)RIGHT PARENTHESIS SEMICOLONQUESTION MARK HYPHEN-MINUSCOLON

EX: Name three uses of the & symbol in a C source file.EX: Name two uses of the % symbol in a C source file.

Page 13: C Programming Language - Lecture 1

C Programming Language - Lecture 1 13

Lexical elements

• Dividing the source program into lines can be done with a character or character sequence

• Additional characters are sometimes used in C source programs, including: – formatting characters such as the backspace (BS) and

carriage return (CR) characters treated as spaces – additional Basic Latin characters, including the

characters $ (DOLLAR SIGN), @ (COMMERClAL AT), and ` (GRAVE ACCENT) – may appear only in comments, character constants, string constants, and file names

Page 14: C Programming Language - Lecture 1

C Programming Language - Lecture 1 14

Lexical elements• The character set interpreted during the execution of a C program is not

necessarily the same as the one in which the C program is written• Characters in the execution character set are represented by their equivalents in

the source character set or by special character escape sequences that begin with the backslash (\) character

• In addition to the standard characters mentioned before, the execution character set must also include: – a null character that must be encoded as the value 0 – used for marking the and of

strings – a newline character that is used as the end-of-line marker: used to divide character

streams into lines during I/O– the alert, backspace, and carriage return characters

• These source and execution character sets are the same when a C program is compiled and executed on the same computer

• For programs that are cross-compiled, when a compiler calculates the compile-time value of a constant expression involving characters, it must use the target computer's encoding, not the more natural source encoding

Page 15: C Programming Language - Lecture 1

C Programming Language - Lecture 1 15

Lexical elements• In C source programs the blank (space), end-

of-line, vertical tab, form feed, and horizontal tab (if present) are known collectively as whitespace characters. Comments are also whitespace

• The end-of-line character or character sequence marks the end of source program lines. In some implementations, the formatting characters carriage return, form feed, and (or) vertical tab additionally terminate source lines and are called line break characters

• A source line can be continued onto the next line by ending the first line with a reverse solidus or backslash (\) character. The backslash and end-of-line marker are removed to create a longer, logical source line

EXAMPLE

if (a==b) X=1; el\ se X=2; Is equivalent to the single line

if (a == b) X=1; else X=2;

EXAMPLE

#define nine (3*3) Is equivalent to

#define nine /* this is nine*/ (3*3)

Page 16: C Programming Language - Lecture 1

C Programming Language - Lecture 1 16

Lexical elements• Comments:

– Traditionally, a comment begins with an occurrence of the two characters /* and ends with the first subsequent occurrence of the two characters */

– Beginning with C99, a comment also begins with the characters // and extends up to (but does not include) the next line break

– Comments are not recognized inside string or character constants or within other comments

– Comments are removed by the compiler before preprocessing

– Standard C specifies that all comments are to be replaced by a single space

• MISRA rules on comments– Source code shall only use /* … */ style comments.– The character sequence /* shall not be used within

a comment.– Sections of code should not be “commented out”.

EXAMPLE// Program to compute the squares of // the first 10 integers#include <stdio.h>

void Squares ( /* no arguments */ ) { int i; /* Loop from 1 to 10, printing out the squares */ for (i=1; i<=10; i++) printf("%d //squared// is %d\n”,i,i*i); }

EXAMPLE

To cause the compiler to ignore large parts of a C program, it is best to enclose the parts to be removed with the preprocessor commands

#if 0#endif

rather than insert /* before and */ after the text.

Page 17: C Programming Language - Lecture 1

C Programming Language - Lecture 1 17

Lexical elements• The characters making up a C program are

collected into lexical tokens• There are five classes of tokens: operators,

separators, identifiers, keywords, and constants• The compiler always forms the longest tokens

possible as it collects characters in left-la-right order, even if the result does not make a valid C program

• Adjacent tokens may be separated by whitespace characters or comments

EXAMPLE

Characters C Tokens forwhile forwhile b >x b ,>,x b->x b,->,x b--x b,--,x b---x b,--,-,x

Q: Which of the previous tokenizations make a valid C program?

Token class Tokens

Simple operators ! % ^ * - + = ~ | . < > / ?

Compound assignment operators += -= *= /= %= <<= >>= &= ^= |=

Other compound operators -> ++ -- << >> <= >= == != && ||

Separator characters ( ) [ ] { } , ; : …

Page 18: C Programming Language - Lecture 1

C Programming Language - Lecture 1 18

Lexical elements• An identifier or name, is a sequence of Latin capital and small letters,

digits, and the underscore character • An identifier must not begin with a digit, and it must not have the same

spelling as a keyword. C is case sensitive • Standard C further reserves all identifiers beginning with an underscore

and followed by either an uppercase letter or another underscore• C89 requires implementations to permit a minimum of 31 significant

characters in identifiers, and C99 raises this minimum to 63 characters• External identifiers – those declared with storage class extern – may

have additional spelling restrictions: C89 requires a minimum capacity of only six characters, not counting letter case. C99 raises this to 31 characters

Page 19: C Programming Language - Lecture 1

C Programming Language - Lecture 1 19

Lexical elements• MISRA rules on identifiers

– Identifiers in an inner scope shall not use the same name as an identifier in an outer scope, and therefore hide that identifier.

– A typedef name shall be a unique identifier.– A tag name shall be a unique identifier.– No object or function identifier with static storage duration should be

reused.– No identifier in one name space should have the same spelling as an

identifier in another name space, with the exception of structure member and union member names.

– No identifier name should be reused.

Page 20: C Programming Language - Lecture 1

C Programming Language - Lecture 1 20

Lexical elements

C Keywords

auto _Bool* break case char _Complex* const continue default restrict* do double else enum extern float for goto if _Imaginary* inline int long register return short signed sizeof static struct switch typedef union unsigned void volatile while* New in C99

Q: What is the meaning of the sizeof keyword?Q: What is the meaning of the continue keyword?

Page 21: C Programming Language - Lecture 1

C Programming Language - Lecture 1 21

Lexical elements• The lexical class of constants includes four different kinds of constants: integers,

floating-point numbers, characters, and strings• These are the rules for determining the radix of an integer constant:

– If the integer constant begins with the letters 0x or 0x, then it is in hexadecimal notation, with the characters a through f (or A through F) representing 10 through 15

– Otherwise, if it begins with the digit 0, then it is in octal notation – Otherwise, it is in decimal notation

• An integer constant may be immediately followed by suffix letters to designate a minimum size for its type: – letters l or L indicate a constant of type long – letters ll or LL indicate a constant of type long long (C99) – letters u or U indicate an unsigned type (int, long, or long long)

• The unsigned suffix may be combined with the long or long long suffix in any order.

EXAMPLEDecimal Hexadecimal octal68 0x44 0104

Page 22: C Programming Language - Lecture 1

C Programming Language - Lecture 1 22

Lexical elements• The value of an integer constant is always non-negative in the absence of overflow. • The actual type of an integer constant depends on its size, radix, suffix letters, and type representation

decisions made by the C implementation• MISRA rules on constants: Octal constants (other than zero) and octal escape sequences shall not be used.

Constant C89 C99

dd…d intlongunsigned long

intlonglong long

0dd…d0xdd…d

intunsignedlongunsigned long

intunsignedlongunsigned longlong longunsigned long long

dd…dU0dd…dU0xdd…dU

unsignedunsigned long

unsignedunsigned longunsigned long long

dd…dL longunsigned long

longlong long

0dd…dL0xdd…dL

longunsigned long

longunsigned longlong longunsigned long long

Page 23: C Programming Language - Lecture 1

C Programming Language - Lecture 1 23

Lexical elementsConstant C89 C99

dd…dUL0dd…dUL0xdd…dUL

unsigned long unsigned longunsigned long long

dd…dLL Not applicable Long long

0dd…dLL0xdd…dLL

Not applicable long longunsigned long long

dd…dULL0dd…dULL0xdd…dULL

Not applicable unsigned long long

EXAMPLES

C constant True value Standard C type Actual representation0 0 int 032767 215-1 int 0x7FFF077777 215-1 int 0x7FFF32768 215 long 0x000080000100000 215 unsigned 0x800065535 216-1 long 0x0000FFFF0xFFFF 216-1 unsigned 0xFFFF65536 216 long 0x000100000x10000 216 long 0x00010000

Page 24: C Programming Language - Lecture 1

C Programming Language - Lecture 1 24

Lexical elements• Floating-point constants may be written with a

decimal point, a signed exponent, or both• Standard C allows a suffix letter (floating-suffix)

to designate constants of types float (F, f)and long double (L, l). Without a suffix, the type of the constant is double.

• The value of a floating-point constant is always non-negative in the absence of overflow

• If the floating-point constant cannot be represented exactly, the implementation may choose the nearest representable value V or the larger or smaller representative value around V.

EXAMPLES

0.3e13.14.01.0E-31e-3.002342e+9

Page 25: C Programming Language - Lecture 1

C Programming Language - Lecture 1 25

Lexical elements

• A character constant is written by enclosing one or more characters in apostrophes.

• A special escape mechanism is provided to write characters or numeric values that would be inconvenient or impossible to enter directly in the source program.

EXAMPLES

Character Value‘a’ 97‘\r’ 13‘ ‘ 32‘\0’ 0‘\377’ 255‘\23’ 19

Page 26: C Programming Language - Lecture 1

C Programming Language - Lecture 1 26

Lexical elements• A string constant is a (possibly empty)

sequence of characters enclosed in double quotes

• For each string constant of n characters, at run time there will be a statically allocated block of n+1 characters whose first n characters are the characters from the string and whose last character is the null character, “\0”

• This block is the value of the string constant and its type is char [n+1]

• Do not depend on all string constants being stored at different addresses

EXAMPLES

"" "\"""Input numbers:" "One text and \ its continuation"

char p1[ ]= "Always writable"; char *p2 = "Possibly not writable"; const char p3[ ] = "Never writable";char p4[ ] = "This long string is permissible"

"in Standard C";

Q: What are the values returned by the sizeof operator and strlen function applied to p1 string?

‘A’ ‘l’ ‘w’ ‘a’ ‘ ‘‘s’

p1[0] p1[7] p1[14]

‘y’ ’\0’‘w’ ‘r’ ‘i’ ‘a’‘t’ ‘b’ ‘l’ ‘e’

65 108 119 97 121 115 32 119 114 105 116 97 98 108 101 0

Page 27: C Programming Language - Lecture 1

C Programming Language - Lecture 1 27

Lexical elements• Escape characters can be used in character and string

constants to represent characters that would be awkward or impossible to enter in the source program directly:– "character escapes" which can be used to represent some

particular formatting and special characters• are used to represent some common special characters in a fashion

independent of the target computer character set

– "numeric escapes" which allow a character to be specified by its numeric encoding

• Numeric escape codes allow a character from the execution character set to be expressed by writing its coded value directly in octal or, in Standard C, hexadecimal notation

EXAMPLESCharacter octal escape hex escape‘a’ ‘\141’ ‘\x61’

The string "\0111" consists of two characters: \011 and 1 The string "\090" consists of three characters, \0, 9 and 0

Page 28: C Programming Language - Lecture 1

C Programming Language - Lecture 1 28

Lexical elementsCharacter escape code Character constant Translation

a ‘\a’ Alert (bell)

b ‘\b’ Backspace

f ‘\f’ Formfeed

n ‘\n’ New line

r ‘\r’ Carriage return

t ‘\t’ Horizontal tab

v ‘\v’ Vertical tab

\ ‘\\’ Backslash

‘ ‘\’’ Quote

“ ‘\”’ Double quote

? ‘\?’ Question mark

MISRA rules on escapes: Only those escape sequences that are defined in the ISO C standard shall be used.

Page 29: C Programming Language - Lecture 1

C Programming Language - Lecture 1 29

The C preprocessor

• The C preprocessor is a simple macro processor that conceptually processes the source text of a C program before the compiler proper reads the source program

• The preprocessor is controlled by special preprocessor command lines, which are lines of the source file beginning with the character #

• The preprocessor typically removes all preprocessor command lines from the source file and makes additional transformations on the source file as directed by the commands

• The syntax of preprocessor commands is completely independent of (although in some ways similar to) the syntax of the rest of the C language

Page 30: C Programming Language - Lecture 1

C Programming Language - Lecture 1 30

The C preprocessor

• The preprocessor does not parse the source text, but it does break it up into tokens for the purpose of locating macro calls

• Standard C permits whitespace to precede and follow the # character on the same source line

• Preprocessor lines are recognized before macro expansion

C source file

Preprocess

Compile

Modified C source file

Object code

Page 31: C Programming Language - Lecture 1

C Programming Language - Lecture 1 31

The C preprocessorCommand Meaning

#define Define a preprocessor macro

#undef Remove a preprocessor macro definition.

#include Insert text from another source file.

#if Conditionally include some text based on the value of a constant expression.

#ifdef Conditionally include some text based on whether a macro name is defined.

#ifndef Conditionally include some text with the sense of the test opposite to that of #ifdef.

#else Alliteratively include some text if the previous #if, #ifdef , #ifndef, or #elif test failed.

#endif Terminate conditional text.

#line Supply a line number for compiler messages.

#else Alternatively include some text based on the value of another constant expression if the previous #if , #ifdef, #ifndef, or #elif test failed.

defined Preprocessor function that yields 1 if a name is defined as a preprocessor macro and 0 otherwise; used in #if and #elif.

# operator Replace a macro parameter with a string constant containing the parameter's value.

## operator Create a single token out of two adjacent tokens.

#pragma Specify implementation-dependent information to the compiler.

#error Produce a compile-time error with a designated message.

Page 32: C Programming Language - Lecture 1

C Programming Language - Lecture 1 32

The C preprocessor• The #define preprocessor command

causes a name (identifier) to become defined as a macro to the preprocessor

• A sequence of tokens, called the body of the macro, is associated with the name

• The #define command has two forms:– An object like macro takes no

arguments. It is invoked by mentioning its name

– A function like macro declares the names of formal parameters within parentheses separated by commas

• The left parenthesis must immediately follow the name of the macro with no intervening whitespace

EXAMPLES

#define BLOCK _SIZE 0x100 #define TRACK _SIZE (16-BLOCK_ SIZE)

#define product (x,y) ((x)*(y))

#define incr(v,low,high) \ for ((v) = (low); (v) < = (high); (v) ++))

#ifndef MAXTABLESIZE #define MAXTABLESIZE 1000 #endif

Page 33: C Programming Language - Lecture 1

C Programming Language - Lecture 1 33

The C preprocessor• Once a macro call has been expanded, the scan for macro calls resumes at

the beginning of the expansion so that names of macros may be recognized within the expansion for the purpose of further macro replacement

• Macros appearing in their own expansion-either immediately or through some intermediate sequence of nested macro expansions-are not reexpanded in Standard C

EXAMPLE

#define plus(x,y) add(y,x) #define add(x,y) (x)+(y) the invocation plus(plus(a,b),c) is expanded as shown next

Step Result1 plus(plus(a,b),c ) 2 add(c,(plus(a,b))3 ((c)+(plus(a,b)))4 ((c)+(add(b,a)))5 ((c)+(((b)+(a))))

#define sqrt (x) ( (x) <0 ? sqrt (-(x)) : sqrt (x))

Page 34: C Programming Language - Lecture 1

C Programming Language - Lecture 1 34

The C preprocessor• Macros operate purely by

textual substitution of tokens:– This can lead to surprising

results if care is not taken:• As a rule, it is safest to always

parenthesize each parameter appearing in the macro body

• The entire body, if it is syntactically an expression, should also be parenthesized.

• Function like macros does not allow the debug process to trace or step into them. The errors inside them are hard to find by debugging.

EXAMPLES

#define SQUARE(x) x*x

The invocation SQUARE (z+1) will be expanded into: z+1*z+1 WHICH IS NOT WHAT WAS INTENDEDSOLUTION:#define SQUARE(x) ((x)*(x))

The invocation SQUARE (z++) will be expanded into: z++*z++ WHICH HAS THE SIDE EFECT OF DOUBLE INCREMENTING zSOLUTION: USE A TRUE FUNCTION NOT A FUNCTION LIKE MACROint square(int x) { return x*x;}

Q: What would be the disadvantage of using a function instead of a function like macro?

Page 35: C Programming Language - Lecture 1

C Programming Language - Lecture 1 35

The C preprocessor• The # token appearing within a macro

definition is recognized as a unary "stringization" operator that must be followed by the name of a macro formal parameter

• During macro expansion, the # and the formal parameter name are replaced by the corresponding actual argument enclosed in string quotes

• Merging of tokens to form new tokens in Standard C is controlled by the presence of a merging operator, ##, in macro definitions

• In a macro replacement list, before rescanning for more macros, the two tokens surrounding any ## operator are combined into a single token

EXAMPLE

#define TEST(a,b) printf( #a "<“ #b "=%d\n", (a)<(b)

The invocation TEST (0, 0xFFFF) will expand into

printf("0" "<" “0xFFFF" "=:%d\n", (0)<(0xFFFF) );

Which will become after string concatenation:

printf("0<0xFFFF=:%d\n", (0)<(0xFFFF) );

EXAMPLE

#define TEMP(i) temp ## i

The invocation TEMP(1) = TEMP(2 + k) + X will expand into

temp1 = temp2 + k + X

Page 36: C Programming Language - Lecture 1

C Programming Language - Lecture 1 36

The C preprocessor• MISRA rules on #define

– Macros shall not be #define’d or #undef’d within a block.– #undef shall not be used.– A function should be used in preference to a function-like macro.– A function-like macro shall not be invoked without all of its arguments.– Arguments to a function-like macro shall not contain tokens that look like

preprocessing directives.– In the definition of a function-like macro each instance of a parameter shall be

enclosed in parentheses unless it is used as the operand of # or ##.– C macros shall only expand to a braced initializer, a constant, a string literal, a

parenthesised expression, a type qualifier, a storage class specifier, or a do-while zero construct.

– There shall be at most one occurrence of the # or ## preprocessor operators in a single macro definition.

– The # and ## preprocessor operators should not be used.

Page 37: C Programming Language - Lecture 1

C Programming Language - Lecture 1 37

The C preprocessor

• The #include preprocessor command causes the entire contents of a specified source text file to be processed as if those contents had appeared in place of the #include command

• The #include command has the following forms in Standard C: – #include <char-sequence>

• searches for the file in certain standard places according to implementation-defined search rules

– #include “char-sequence” • will also search in the standard places, but usually after searching

some local places, such as the programmer's current directory

Page 38: C Programming Language - Lecture 1

C Programming Language - Lecture 1 38

The C preprocessor

• MISRA rules on #include:– #include statements in a file should only be

preceded by other preprocessor directives or comments.

– Non-standard characters should not occur in header file names in #include directives.

– The #include directive shall be followed by either a <filename> or "filename“ sequence.

– Precautions shall be taken in order to prevent the contents of a header file being included twice.

Page 39: C Programming Language - Lecture 1

C Programming Language - Lecture 1 39

The C preprocessor

• The preprocessor conditional commands allow lines of source text to be passed through or eliminated by the preprocessor on the basis of a computed condition

• The preprocessor replaces any name in the #if expression that is not defined as a macro with the constant 0

• The expressions that may be used in #if and #elif commands include integer constants and all the integer arithmetic, relational, bitwise and logical operators

EXAMPLE

#define X86 0 #define ARM 0 #define PPC 1

#if X86 X86-dependent code

#endif #if ARM

ARM-dependent code #endif #if PPC

PPC -dependent code #endif

EXAMPLE

#define X86 1 #undef ARM#undef PPC

#ifdef X86 X86-dependent code

#endif #ifdef ARM

ARM-dependent code #endif #ifdef PPC

PPC -dependent code #endif

Page 40: C Programming Language - Lecture 1

C Programming Language - Lecture 1 40

The C preprocessor

• The #pragma directive can be used by C implementations to add new preprocessor functionality or provide implementation defined information to the compiler

• The #error directive produces a compile-time error message that includes the argument tokens, which are subject to macro expansion

EXAMPLE

#if defined (X86) && defined(ARM)#error Inconsistent CPU definition! #endif

EXAMPLE

#include "sizes.h" /* defines SIZE */

#if (SIZE % 256) != 0 #error "SIZE must be a multiple of 256!" #endif

EXAMPLES

#pragma location = 0xfe00 const int x = 5;

#pragma vector = TIMER2_OVF_vect __interrupt void ISR_T2_OVF( void )

Page 41: C Programming Language - Lecture 1

C Programming Language - Lecture 1 41

Q&A1. Eliminate all the comments from the following C program fragment:

/ ** / */*"*/* / *" //* //**/*/

2. Which strings would be recognized as a sequence of C tokens? How many tokens would be found in each case?1. X++Y2. X+++Y3. -12uL4. X**25. A*=B6. retValue = (100*i+j*k)/i-1

3. Which of the following identifiers are not valid?1. Forloop2. 100Miles3. Miles1004. _100Miles5. 100_Miles6. Register

4. How is interpreted an escape sequence that does not obey the presented rules?5. A Standard C compiler must perform each of the following actions on an input program. In what order are the actions performed?

1. collecting characters into tokens 2. removing comments 3. processing line continuation

6. What would be the result of the following code?

#define GETIO #include <stdio.h>GETIO

7. What is wrong with the following code?#define DBL(a) a+a