Stringification

本文深入探讨了C语言中字符串表示的概念及其预处理过程中的关键作用,详细解释了如何通过预处理将字符串常量转换为有效的C语言字符串表达式,并通过实例演示了这一过程。

Stringification in C involves more than putting double-quote characters around the fragment.

The preprocessor backslash-escapes the quotes surrounding embedded string constants, and all backslashes within string and character constants, in order to get a valid C string constant with the proper contents.

Thus, stringifying p = "foo\n"; results in "p = \"foo\\n\";". However,

backslashes that are not inside string or character constants are not duplicated: ‘\n’ by itself stringifies to "\n".

实验如下:


stringification.c

#include <unistd.h>
#include <stdio.h>
#define WARN(EXP) fprintf (stderr, "Warning: " #EXP "\n");

int main(void)
{
    WARN(p= "foo\n");
    return 0;
}

gcc constptr.c  -E stringification.c  -o   stringification.i


stringification.i

int main(void)
{
    fprintf (stderr, "Warning: " "p= \"foo\\n\"" "\n");;
    return 0;
}

-------------------------------------------------------------------------------------------------

后续实验的结果:

实参 :“p= foo\n"  预处理后      "\"p= foo\\n\""

实参:”\n“             预处理后        "\"\\n\""

实参 : \n             预处理后       "\n"

实参 :' \n'           预处理后       "'\\n'"



class ConstrainedList (list): """Constrains the list class so it offers only the following primitive array API: - `lst[i]` for getting and setting a value at an *existing, positive* index `i` - `len(lst)` to obtain the number of slots - `lst.append(None)` to grow the list by *one slot at a time* - `del lst[len(lst)-1]` to delete the last slot in a list All other operations will result in an exception being raised. """ def __init__(self, *args): super().__init__(*args) def append(self, value): if value is not None: raise ValueError('Can only append None to constrained list!') super().append(value) def __getitem__(self, idx): if idx < 0 or idx >= len(self): raise ValueError('Can only use positive, valid indexes on constrained lists!') return super().__getitem__(idx) def __setitem__(self, idx, value): if idx < 0 or idx >= len(self): raise ValueError('Can only use positive, valid indexes on constrained lists!') super().__setitem__(idx, value) def __delitem__(self, idx): if idx != len(self)-1: raise ValueError('Can only delete last item in constrained list!') super().__delitem__(idx) def __getattribute__(self, name): if name in ('insert', 'pop', 'remove', 'min', 'max', 'index', 'count', 'clear', 'copy', 'extend'): raise AttributeError('Method "' + name + '" not supported on constrained list!') else: return super().__getattribute__(name) # __getattribute__ isn't called for special methods, so the following are needed def __add__(self, value): raise AttributeError('Constrained lists do not support `+`!') def __contains__(self, value): raise AttributeError('Constrained lists do not support `in`!') def __eq__(self, value): raise AttributeError('Constrained lists do not support `==`!') def __iter__(self): raise AttributeError('Constrained lists do not support iteration!') def __str__(self): raise AttributeError('Constrained lists do not support stringification!') def __repr__(self): raise AttributeError('Constrained lists do not support stringification!') # for testing only! (don't use this in your ArrayList implementation) def _as_list(self): return list(super().__iter__())
02-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值