linux中#和## 用法

在linux内核中能看到很多的##    一般来讲# 很容易理解

一般来讲有两个用途

1. 在一些连接脚本文件里作为注释

也就是说#后面的都是注释内容

2.c代码中做类似宏定义的字符串替换

举个例子:

#define TEST(a) #a

printf("%s\n",TEST(tim))

运行结果就是

tim

这里就可以看出来这个#的功能就是将后面的变量变成一个字符串

但是##是什么意思呢?

这里就给出一个介绍

## 的左右有两个

一,

做连接符

顾名思义就是连接两个符号

a1和 a##1 是一样的  暂时没想到这种用处的必要性  毕竟我并不是想吧简单东西复杂化   让代码难读

#define TEST(x, y) x##y

printf("%d\n",TEST(1,2))

运行结果显示

2

具体的是实现是 TEST(X,Y)=X##Y=XY

TEST(1,2)=1*2=2

二,这里还有一种用法  不太常见

这里有一个网上的例子  感觉挺典型的

#include <stdio.h>

#include <stdlib.h>

#define YYG_PRINT(fmt, arg...) printf(fmt, ##arg)

int

main()

{

int m = 20, n = 90, l = 80;

char str[16] = "amstr";

char ch = ‘a‘;

YYG_PRINT("m is %d n is %d l is %d str is %s ch is %c\n", m, n, l, str, ch);

exit(0);

}

run result:

[[email protected]]# ./a.out

m is 20 n is 90 l is 80 str is amstr ch is a

这就表明这个作用就是一个省略的意思   以为不可能列出所有的变量  就用这个符号和第一个变量代表了

最后给出Linux标准文件中的说明

来自ISO-IEC1999文件:

=====================================================================

6.10.3.1 Argument substitution

1 After the arguments for the invocation of a function-like macro have been identified,argument substitution takes place. A parameter in the replacement list, unless
preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument’s preprocessing tokens are
completely macro replaced as if they formed the rest of the preprocessing file; no other preprocessing tokens are available.

2 An identifier _ _VA_ARGS_ _ that occurs in the replacement list shall be treated as if it were a parameter, and the variable arguments shall form the preprocessing
tokens used to replace it.

6.10.3.2 The # operator

Constraints

1 Each # preprocessing token in the replacement list for a function-like macro shall be followed by a parameter as the next preprocessing token in the replacement list.

Semantics

2 If, in the replacement list, a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing
token that contains the spelling of the preprocessing token sequence for the corresponding argument. Each occurrence of white space between the argument’s preprocessing tokens becomes a single space character in the character string literal. White space before
the first preprocessing token and after the last preprocessing token composing the argument is deleted. Otherwise, the original spelling of each preprocessing token in the argument is retained in the character string literal, except for special handling for
producing the spelling of string literals and character constants: a \ character is inserted before each " and \ character of a character constant or string literal (including the delimiting "

characters), except that it is implementation-defined whether a \ character is inserted before the \ character beginning a universal character name. If the replacement
that results is not a valid character string literal, the behavior is undefined. The character string literal corresponding to an empty argument is "". The order of evaluation of # and ## operators is unspecified.

6.10.3.3 The ## operator

Constraints

1 A## preprocessing token shall not occur at the beginning or at the end of a replacement list for either form of macro definition.

Semantics

2 If, in the replacement list of a function-like macro, a parameter is immediately preceded or followed by a ## preprocessing token, the parameter is replaced by the
corresponding argument’s preprocessing token sequence; however, if an argument consists of no preprocessing tokens, the parameter is replaced by a placemarker preprocessing token instead.145)

3 For both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing
token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token. Placemarker preprocessing tokens are handled specially: concatenation of two placemarkers results
in a single placemarker preprocessing token, and concatenation of a placemarker with a non-placemarker preprocessing token results in the non-placemarker preprocessing token. If the result is not a valid preprocessing token, the behavior is undefined. The
resulting token is available for further macro replacement. The order of evaluation of ## operators is unspecified.

4 EXAMPLE

In the following fragment:

#define hash_hash # ## #

#define mkstr(a) # a

#define in_between(a) mkstr(a)

#define join(c, d) in_between(c hash_hash d)

char p[] = join(x, y); // equivalent to

// char p[] = "x ## y";

The expansion produces, at various stages:

join(x, y)

in_between(x hash_hash y)

in_between(x ## y)

mkstr(x ## y)

"x ## y"

In other words, expanding hash_hash produces a new token, consisting of two adjacent sharp signs, but

this new token is not the ## operator.

给一个下载的网站 http://www.iso.org/iso/home/store/catalogue_ics/catalogue_detail_ics.htm?csnumber=57853   不过是付费的哦 198瑞士法郎 折合人民币一千四  估计没人下了   后面我会传上来这个文档

linux中#和## 用法

时间: 2024-08-01 21:43:31

linux中#和## 用法的相关文章

Linux中find用法

Linux中find用法 linux常用命令 find -name april* 在当前目录下查找以april开始的文件 find -name april* fprint file 在当前目录下查找以april开始的文件,并把结果输出到file中 find -name ap* -o -name may* 查找以ap或may开头的文件 find /mnt -name tom.txt -ftype vfat 在/mnt下查找名称为tom.txt且文件系统类型为vfat的文件 find /mnt -n

linux中sed用法

一.sed命令介绍: 1.含义:Stream Editor文本流编辑,sed是一个"非交互式的"面向字符流的编辑器.能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上.还可以对原文件改动,但是不会再屏幕上返回结果. 2.功能:主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等 3.参考:http://www.gnu.org/software/sed/manual/sed.html 二.sed用法 1.语法格式:

Linux中wget用法

Wget简介:Linux系统中wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协议,可以使用HTTP代理.所谓的自动下载是指,wget可以在用户退出系统之后在后台执行.这意味这你可以登录系统,启动一个wget下载任务,然后退出系统,wget将在后台执行直到任务完成. 一.命令格式:wget [参数] [URL地址] 二.功能:(1)支持断点下传功能:(2)同时支持F

linux中convert用法

转: 强大的convert命令 convert命令可以用来转换图像的格式,支持JPG, BMP, PCX, GIF, PNG, TIFF, XPM和XWD等类型,下面举几个例子:   convert  xxx.jpg  xxx.png   将jpeg转成png文件   convert  xxx.gif   xxx.bmp  将gif转换成bmp图像   convert  xxx.tiff    xxx.pcx   将tiff转换成pcx图像 还可以改变图像的大小:   convert -resi

linux中fuser用法详解

fuser功能 fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息.  fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文件名后面跟一个字母表示访问类型.  访问类型如下:  c 代表当前目录  e 将此文件作为程序的可执行对象使用 f 打开的文件.默认不显示.  F 打开的文件,用于写操作.默认不显示.  r 根目录.  m 映射文件或者共享库. s 将此文件作为共享库(或其他可装载对象)使用当指定的文件没有被访问,或者出现

linux中screen用法

screen实现一个类似windows中最小化运行的效果,可以在后台运行,之后想用时再调到前台.其实一直想学学怎么用的,但一直没什么契机. 其实常用的命令就几个.(注意参数大小写有区别) screen -S yourname -> 新建一个叫yourname的session screen -ls -> 列出当前所有的session screen -r yourname -> 回到yourname这个session screen -d yourname -> 远程detach某个se

linux中grep用法详解

查找特定字符串并颜色显示 [[email protected] test]# grep -n 'the' regular_express.txt --color=auto 8:I can't finish the test. 12:the symbol '*' is represented as start. 15:You are the best is mean you are the no. 1. 16:The world <Happy> is the same with "gl

Linux中date用法

1.命令:date 2.命令功能:date 可以用来显示或设定系统的日期与时间. 3.命令参数 -d<字符串>:显示字符串所指的日期与时间.字符串前后必须加上双引号: -s<字符串>:根据字符串来设置日期与时间.字符串前后必须加上双引号: -u:显示GMT: --help:在线帮助: --version:显示版本信息. 4.日期格式字符串列表 %H 小时(以00-23来表示). %I 小时(以01-12来表示). %K 小时(以0-23来表示). %l 小时(以0-12来表示). 

linux中vim编辑器各种常用命令及用法

linux中vim编辑器的常用命令以及用法(注意严格区分大小写以及中英文): vim编辑器有三种模式,分别是:编辑模式,输入模式以及末行模式. 模式转换: 编辑模式>>>输入模式: i:在光标所在字符前面,转为输入模式(即转完后在光标所在字符前输入):                      I:在光标所在行的行首,转为输入模式(即转完后在行首输入,不包括行首空                         白) a:在光标所在字符后,转为输入模式(即转完后在光标所在字符后面输入):