SlideShare a Scribd company logo
1 of 17
Download to read offline
Preprocessor



  Preprocessor,
     math.h
     stdlib.h
Preprocessor
 Preprocessor
  – Permits to define simple macros that are evaluated and
    expanded prior to compilation.
  – Commands begin with a ‘#’.
  –   #define : defines a macro             [Ex]    #include <stdio.h>
  –   #undef : removes a macro definition           #define PI 3.14159
  –   #include : insert text from file
  –   #if : conditional based on value of expression
  –   #ifdef : conditional based on whether macro defined
  –   #ifndef : conditional based on whether macro is not defined
  –   #else : alternative
  –   #elif : conditional alternative
  –   defined() : preprocessor function: 1 if name defined, else 0
       #if defined(__NetBSD__)

                                                                         2
Preprocessor
 #include <filename> or #include “filename”
   – Include contents of filename in this position (not modify your
     source file, but make a new temporary file just before compile)
   – <> is used if filename is in the system default directory
     (Most of the standard header files are in the default directory)
   – “” is used if filename is not in the default directory
     (Write the path to filename if it is not in the current directory)



[Ex]
                                     Include stdio.h which is in the default
       #include <stdio.h>
                                     directory
       #include “./test/file.h”

                                     Include file.h in test directory.




                                                                               3
Preprocessor
 Header file
   – Header files with the extension “.h” are included by #include


 Contents of Header file
   – Prototype of Function
   – ‘extern’ global variables     Refer to Modulization
   – type definition, etc.


 Typical header files
   – stdio.h, stdlib.h, math.h, etc.




                                                                     4
Preprocessor
 #define [identifier] [replacement]
       – replaces any occurrence of identifier in the rest of the code by
         replacement.
       – replacement can be an expression, a statement, a block or simply
         anything.


[Ex]

       #define LIMIT 100            Preprocessor regards LIMIT as 100,
       #define PI 3.14159           PI as 3.141592.




                                                                            5
Preprocessor
 #define
   #include <stdio.h>
   #define LIMIT 100
   #define PI 3.14159
   void main(void)
   {
      printf( “%d, %fn”, LIMIT, PI ) ;
   }
                                          Preprocessor makes a temporary file.

   …
                                       After that, compile the temporary file.
   void main(void)
   {
      printf( “%d, %fn”, 100, 3.14159 ) ;
   }


                                                                                 6
Preprocessor
      Example

#include <stdio.h>   void main(void)
                     {
#define YELLOW 0         int color ;
#define RED    1
#define BLUE   2         for( color = YELLOW ; color <= BLUE ; color++ ) {
                             switch( color ) {
                             case YELLOW : printf( “Yellown” ) ; break ;
                             case RED : printf( “Redn” ) ; break ;
                             case BLUE : printf( “Bluen” ) ; break ;
                             }
                         }
                     }

                                                                             7
Preprocessor
 Macro function: Declare a function by using #define

              #define multiply(a,b) ((a)*(b))

              void main() {
                  int c = multiply(3,2) ;
              }

   – Compiled after the code modified by preprocessor as follows.

              void main() {
                  int c = ((3)*(2)) ;
              }




                                                                    8
Preprocessor
 Macro function: Be careful! Simple replacement!!
   – what will happen?

               #define multiply(a,b) a*b

               void main() {
                   int c = multiply(3+1,2+2) ;
               }

   – Compiled after the code modified by preprocessor as follows.

               void main() {
                   int c = 3+1*2+2 ;
               }

   – Use ‘()’ for safety.

                                                                    9
Math functions

  (math.h)
Mathematical Functions

 Mathematical Functions
  – sqrt(), pow(), exp(), log(), sin(), cos(), tan(), etc.
  – Include the Header file <math.h>
  – All the math functions have argument with double type and
    return value with double type.

        [Ex] double sin(double); /* argument is the angle in radians */
            double pow(double, double);


  – Give “-lm” flag when you compile this with Unix(gcc).


        [Ex] gcc filename.c -lm


                                                                          11
Mathematical Functions

[Ex]   #include <stdio.h>
       #include <math.h>

       #define PI 3.1415926

       int main() {
           double r = PI / (2*90);        /* 180 radian = 1 degree */
           int i;

           for(i=0; i<=90; i+=5) {
                 printf("cos(%d) = %ft", i, cos(r*i));
                 printf("sin(%d) = %ft", i, sin(r*i));
                 printf("tan(%d) = %fn", i, tan(r*i));
           } cos(0) = 1.000000    sin(0) = 0.000000 tan(0) = 0.000000
       }     …
             cos(45) = 0.707107   sin(45) = 0.707107   tan(45) = 1.000000
             …
             cos(90) = 0.000000   sin(90) = 1.000000   tan(90) = 37320539.634355

                                                                                   12
Misc. functions

  (stdlib.h)
Miscellaneous functions
 Standard Headers you should know about:
  – stdio.h: file and console (also a file) IO
      • perror, printf, open, close, read, write, scanf, etc.
  – stdlib.h: common utility functions
      • malloc, calloc, strtol, atoi, etc
  – string.h: string and byte manipulation
      • strlen, strcpy, strcat, memcpy, memset, etc.
  – math.h: math functions
      • ceil, exp, floor, sqrt, etc.




                                                                14
Miscellaneous functions
 Standard Headers you should know about:
  – ctype.h: character types
      • isalnum, isprint, isupport, tolower, etc.
  – errno.h: defines errno used for reporting system errors
  – signal.h: signal handling facility
      • raise, signal, etc
  – stdint.h: standard integer
      • intN_t, uintN_t, etc
  – time.h: time related facility
      • asctime, clock, time_t, etc.




                                                              15
Miscellaneous functions

 Miscellaneous Functions
  – atoi(), rand(), srand()
  – <stdlib.h>: Common Utility Functions
  – atoi(“String”): Convert string to integer
     int a = atoi( “1234” ) ;

  – rand() : Generate random number
     int a = rand() ;

  – srand( Initial Value ) : Initialize random number generator
     srand( 3 ) ;



                                                                  16
Miscellaneous functions

 Ex.: Print 10 random numbers
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
                                       Run this Program twice
         for(i=0; i<10; i++)
           printf(“%dn”, rand() ) ;
      }

 Print different random number in every execution
      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>

      int main() {
         srand( (int)time(NULL) ) ;
         for(i=0; i<10; i++)
           printf(“%dn”, rand() ) ;
      }
                                                                17

More Related Content

What's hot

6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 
Boost.Python - domesticating the snake
Boost.Python - domesticating the snakeBoost.Python - domesticating the snake
Boost.Python - domesticating the snakeSławomir Zborowski
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Rick Copeland
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting StartedKent Ohashi
 
Functions in python
Functions in pythonFunctions in python
Functions in pythonIlian Iliev
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซีkramsri
 
C++totural file
C++totural fileC++totural file
C++totural filehalaisumit
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซีkramsri
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and GoEleanor McHugh
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehmanNauman Rehman
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python Chetan Giridhar
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and CEleanor McHugh
 

What's hot (20)

6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
Boost.Python - domesticating the snake
Boost.Python - domesticating the snakeBoost.Python - domesticating the snake
Boost.Python - domesticating the snake
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
C - ISRO
C - ISROC - ISRO
C - ISRO
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
C++ tutorial
C++ tutorialC++ tutorial
C++ tutorial
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
C++totural file
C++totural fileC++totural file
C++totural file
 
c programming
c programmingc programming
c programming
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
 
c programming
c programmingc programming
c programming
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
 

Similar to 3 1. preprocessor, math, stdlib

Similar to 3 1. preprocessor, math, stdlib (20)

Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
7 functions
7  functions7  functions
7 functions
 
Usp
UspUsp
Usp
 
C ISRO Debugging
C ISRO DebuggingC ISRO Debugging
C ISRO Debugging
 
L4 functions
L4 functionsL4 functions
L4 functions
 
Python basic
Python basicPython basic
Python basic
 
Function
FunctionFunction
Function
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started Cpp
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
 
Functions
FunctionsFunctions
Functions
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 

More from 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 

More from 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 

3 1. preprocessor, math, stdlib

  • 1. Preprocessor Preprocessor, math.h stdlib.h
  • 2. Preprocessor  Preprocessor – Permits to define simple macros that are evaluated and expanded prior to compilation. – Commands begin with a ‘#’. – #define : defines a macro [Ex] #include <stdio.h> – #undef : removes a macro definition #define PI 3.14159 – #include : insert text from file – #if : conditional based on value of expression – #ifdef : conditional based on whether macro defined – #ifndef : conditional based on whether macro is not defined – #else : alternative – #elif : conditional alternative – defined() : preprocessor function: 1 if name defined, else 0 #if defined(__NetBSD__) 2
  • 3. Preprocessor  #include <filename> or #include “filename” – Include contents of filename in this position (not modify your source file, but make a new temporary file just before compile) – <> is used if filename is in the system default directory (Most of the standard header files are in the default directory) – “” is used if filename is not in the default directory (Write the path to filename if it is not in the current directory) [Ex] Include stdio.h which is in the default #include <stdio.h> directory #include “./test/file.h” Include file.h in test directory. 3
  • 4. Preprocessor  Header file – Header files with the extension “.h” are included by #include  Contents of Header file – Prototype of Function – ‘extern’ global variables Refer to Modulization – type definition, etc.  Typical header files – stdio.h, stdlib.h, math.h, etc. 4
  • 5. Preprocessor  #define [identifier] [replacement] – replaces any occurrence of identifier in the rest of the code by replacement. – replacement can be an expression, a statement, a block or simply anything. [Ex] #define LIMIT 100 Preprocessor regards LIMIT as 100, #define PI 3.14159 PI as 3.141592. 5
  • 6. Preprocessor  #define #include <stdio.h> #define LIMIT 100 #define PI 3.14159 void main(void) { printf( “%d, %fn”, LIMIT, PI ) ; } Preprocessor makes a temporary file. … After that, compile the temporary file. void main(void) { printf( “%d, %fn”, 100, 3.14159 ) ; } 6
  • 7. Preprocessor  Example #include <stdio.h> void main(void) { #define YELLOW 0 int color ; #define RED 1 #define BLUE 2 for( color = YELLOW ; color <= BLUE ; color++ ) { switch( color ) { case YELLOW : printf( “Yellown” ) ; break ; case RED : printf( “Redn” ) ; break ; case BLUE : printf( “Bluen” ) ; break ; } } } 7
  • 8. Preprocessor  Macro function: Declare a function by using #define #define multiply(a,b) ((a)*(b)) void main() { int c = multiply(3,2) ; } – Compiled after the code modified by preprocessor as follows. void main() { int c = ((3)*(2)) ; } 8
  • 9. Preprocessor  Macro function: Be careful! Simple replacement!! – what will happen? #define multiply(a,b) a*b void main() { int c = multiply(3+1,2+2) ; } – Compiled after the code modified by preprocessor as follows. void main() { int c = 3+1*2+2 ; } – Use ‘()’ for safety. 9
  • 10. Math functions (math.h)
  • 11. Mathematical Functions  Mathematical Functions – sqrt(), pow(), exp(), log(), sin(), cos(), tan(), etc. – Include the Header file <math.h> – All the math functions have argument with double type and return value with double type. [Ex] double sin(double); /* argument is the angle in radians */ double pow(double, double); – Give “-lm” flag when you compile this with Unix(gcc). [Ex] gcc filename.c -lm 11
  • 12. Mathematical Functions [Ex] #include <stdio.h> #include <math.h> #define PI 3.1415926 int main() { double r = PI / (2*90); /* 180 radian = 1 degree */ int i; for(i=0; i<=90; i+=5) { printf("cos(%d) = %ft", i, cos(r*i)); printf("sin(%d) = %ft", i, sin(r*i)); printf("tan(%d) = %fn", i, tan(r*i)); } cos(0) = 1.000000 sin(0) = 0.000000 tan(0) = 0.000000 } … cos(45) = 0.707107 sin(45) = 0.707107 tan(45) = 1.000000 … cos(90) = 0.000000 sin(90) = 1.000000 tan(90) = 37320539.634355 12
  • 13. Misc. functions (stdlib.h)
  • 14. Miscellaneous functions  Standard Headers you should know about: – stdio.h: file and console (also a file) IO • perror, printf, open, close, read, write, scanf, etc. – stdlib.h: common utility functions • malloc, calloc, strtol, atoi, etc – string.h: string and byte manipulation • strlen, strcpy, strcat, memcpy, memset, etc. – math.h: math functions • ceil, exp, floor, sqrt, etc. 14
  • 15. Miscellaneous functions  Standard Headers you should know about: – ctype.h: character types • isalnum, isprint, isupport, tolower, etc. – errno.h: defines errno used for reporting system errors – signal.h: signal handling facility • raise, signal, etc – stdint.h: standard integer • intN_t, uintN_t, etc – time.h: time related facility • asctime, clock, time_t, etc. 15
  • 16. Miscellaneous functions  Miscellaneous Functions – atoi(), rand(), srand() – <stdlib.h>: Common Utility Functions – atoi(“String”): Convert string to integer int a = atoi( “1234” ) ; – rand() : Generate random number int a = rand() ; – srand( Initial Value ) : Initialize random number generator srand( 3 ) ; 16
  • 17. Miscellaneous functions  Ex.: Print 10 random numbers #include <stdio.h> #include <stdlib.h> int main() { Run this Program twice for(i=0; i<10; i++) printf(“%dn”, rand() ) ; }  Print different random number in every execution #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand( (int)time(NULL) ) ; for(i=0; i<10; i++) printf(“%dn”, rand() ) ; } 17