Preprocessor Commands - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Preprocessor Commands

Description:

This directive tells the compiler to take code from the file with the name ... C provides a set of standard library functions, called the ANSI C libraries ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 19
Provided by: robynve
Category:

less

Transcript and Presenter's Notes

Title: Preprocessor Commands


1
Preprocessor Commands
  • Preprocessor commands are commands given directly
    to the compiler rather than instructions that
    form part of the program
  • All preprocessor commands begin with the symbol
  • We will look at the most common commands in the
    next few slides

2
include ltfilenamegt
  • This directive tells the compiler to take code
    from the file with the name filename and paste it
    into the current file in the place of the
    directive
  • It is typically used to include header files
  • A header file contains function declarations

3
include filename
  • This directive behaves in the same way as the
    previous slide except it is used with non
    standard file names
  • For example if your files become to big, you can
    split them up and use the include directive to
    include them in your main file

4
define myconst 32
  • The define directive is used to implement
    replacements
  • The compiler will search through the code and
    wherever it sees the name following define it
    will replace it with the appropriate value
  • The benefits of this is that you can alter the
    value in one place in the code and it makes code
    more legible

5
Example 1
  • This function uses the define derivative to
    replace the word integer with i
  • include ltstdio.hgt
  • define integer i
  • int main(void)
  • printf(integer,42)

Digital Electronics EEE3017W R. Verrinder (2008)
5
6
Strings
  • A string is a group of characters
  • C does not have a specific data type to hold
    strings, it uses arrays of characters to store
    the string
  • Most string functions are supported through
    library functions
  • In order to mark the end of a string, C uses the
    \0 character or 0000 0000

7
Example 2
  • Here is an example of a very basic function which
    prints out the word Cat
  • include ltstdio.hgt
  • int main(void)
  • char mystring4
  • mystring0C
  • mystring1a
  • mystring2t
  • mystring3\0
  • printf(mystring) //This prints the word Cat

Digital Electronics EEE3017W R. Verrinder (2008)
7
8
Null Terminator
  • \0 is called the null terminator
  • It is important that you leave one open character
    at the end of your string array for the null
    terminator
  • You therefore always need one plus the number of
    characters in the string
  • If you forget the null terminator, the program
    will print out all characters in memory until it
    encounters a null terminator

9
Example 3
  • Here is an example of one function that is
    implemented in the string library. This function
    calculates the length of a string
  • int len(const char instr)
  • int count 0
  • while (instrcount) count
  • return count

Digital Electronics EEE3017W R. Verrinder (2008)
9
10
Library Functions
  • C provides a set of standard library functions,
    called the ANSI C libraries
  • These are broken down into logical groupings
  • Library functions are used to implement much of
    the standard functionality used in high level
    programming languages

11
stdio.h
  • This library is used for input and output
    functions
  • It is a very vast library so we will only show a
    few functions here

12
int fprintf(stdout, char s, const char
format,)
  • This function is very similar to printf except
    that it is used to print data to a file

13
int sprintf(char s, const char format,)
  • This is very similar to fprintf, but the output
    is written into a string buffer s rather than a
    stream
  • s must be large enough to hold the output
  • The output is null terminated
  • The function returns the string length excluding
    the null character

14
int scanf(const char format,)
  • scanf is equivalent to fscanf(stdin, f, ) except
    data is taken in from a keyboard

15
int sscanf(char s, const char format,)
  • This is like fscanf except that input is read
    from a string buffer s rather than a standard
    input

16
int fgetc(FILE stream)
  • Returns next character from (input) stream
    stream, or EOF on end of file error

17
int putchar(int c)
  • This function puts one character c on the screen
  • It is equivalent to putc(c, stdout)

18
int puts(const char s)
  • Write s (excluding terminating NULL) and newline
    to stdout. Returns non-negative on success, EOF
    on error
Write a Comment
User Comments (0)
About PowerShow.com