STDC--08 preprocessing in large programs

Source: Internet
Author: User

Highlight:

  1. Macro macro functions with parameters

  2. GCC can be compiled separately for each. c file

  3. Header files

  4. Global variable extern

  5. Makefile notation

1. Macro macro functions with parameters

Types can be passed

#include <stdio.h>#defineSWAP (t,x,y) {T t=x; x=y; y=t;} With parameter macros#defineMAX (x, y) (X&LT;Y?Y:X)//Macro functionintMain () {intA =Ten, b= -; Doublec =12.3, d=45.6; SWAP (int, A, b); SWAP (Double, C, D); printf ("a=%d,b=%d\n", A, b); printf ("c=%g,d=%g\n", c,d); printf ("%d\n", MAX (A, b));    GetChar (); return 0; }

2. GCC can be compiled separately for each. c file

Command Gcc-c xxx.c--compile to get a. O target file for the extension

When all the target files are available, you can use the GCC command to merge them.

3. Header files

Header files use conditional compilation to avoid being included multiple times with the same source file, in the header file Readint ()

  1 #ifndef __readint_h__   2 #define __readint_h__  3int  READINT ();   4 #endif  // __readint_h__

4. Global variable extern

If you write a multi-file program, you need to use global variables to share in multiple files, a file definition, and extern in other files.

The extern keyword indicates that the variable is declared in a different file, and the file is just going to use that variable.

Static variables are modified only for use in the current file.

5. Makefile notation

Readint.c

 1#include <stdio.h>2#include"readInt.h"  3   4 intJie_guo; 5 intreadInt () {6     intShu_zi=0; 7      while(!SCANF ("%d",&Shu_zi)) 8     {  9scanf"%*[^\n]"); Tenscanf"%*c");  Oneprintf"Please enter one number :");  A     }  -scanf"%*[^\n]");  -scanf"%*c");  thejie_guo=Shu_zi; -     //return Shu_zi;  -}

ReadInt.h

  1 #ifndef __readint_h__   2 #define __readint_h__  3int  Jie_guo;   4 int readInt ();   5 #endif  // __readint_h__

Mutilfiles.c

  1#include <stdio.h>2#include"readInt.h"  - intMain () - {  -     intShu_zi=0;  -readInt (); inprintf"The number is :%d\n", Jie_guo);  *     return 0;  $}

Makefile can be written as:

  1 mutilfiles:mutilfiles.c readint.c readInt.h   2         gcc-c readint.c  3         gcc-c mutilfiles.c  4         gcc MUTILFILES.O Readint.o-o Mutilfiles

or scattered into each task

  1 mutilfiles:readint.o mutilfiles.o readInt.h   2         GCC readint.o mutilfiles.o readInt.h-o mutilfiles  3   4  readint.o:readint.c ReadInt.h  5         gcc-c readint.c  6    7  MUTILFILES.O:MUTILFILES.C readInt.h  8         gcc-c mutilfiles.c  9  Ten clean :           RM *.o

Stdc--08 preprocessing in large programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.