在c中u'123'或U'\ n \ t'或L'abc'整数常数怎么样?(How are u'123' or U'\n\t' or L'abc' integer constants in c? [duplicate])
这个问题在这里已有答案:
- C ++多字符文字 5个答案
在为C语言编写代码时,我一直在寻找它的语法。 我偶然发现了ANSI C Grammar(Lex) 。 在那里我发现了以下正则表达式(RE):
{CP}?"'"([^'\\\n]|{ES})+"'" { return I_CONSTANT; }
其中CP和ES如下
CP (u|U|L) ES (\\(['"\?\\abfnrtv]|[0-7]{1,3}|x[a-fA-F0-9]+))
据我所知,ES是逃脱序列的RE。
如果我正确理解了正则表达式,那么
u'123'
或U'\n\t'
或L'abc'
是有效的I_CONSTANT
s。我编写了以下小程序来查看它们代表的常量值。
#include <stdio.h> int main(void) { printf("%d %d %d\n", u'123', U'\n\t', L'abc'); return 0; }
这给出了以下输出。
51 9 99
我破译他们代表单引号内最右边字符的ASCII值。 但是,我无法理解的是这种整数常量的用法和重要性。
This question already has an answer here:
- C++ multicharacter literal 5 answers
While making a compiler for C language, I was looking for its grammar. I stumbled upon ANSI C Grammar (Lex). There I found the following regular expression (RE):
{CP}?"'"([^'\\\n]|{ES})+"'" { return I_CONSTANT; }
where CP and ES are as follows
CP (u|U|L) ES (\\(['"\?\\abfnrtv]|[0-7]{1,3}|x[a-fA-F0-9]+))
I understand that ES is RE for escape sequence.
If I understand the regular expression correctly then,
u'123'
orU'\n\t'
orL'abc'
are validI_CONSTANT
s.I wrote the following small program to see what constant values they represent.
#include <stdio.h> int main(void) { printf("%d %d %d\n", u'123', U'\n\t', L'abc'); return 0; }
This gave the following output.
51 9 99
I deciphered that they represent the ASCII value of the right-most character inside single quotes. However, what I fail to understand is the use and importance of this kind of integer constant.
原文:https://stackoverflow.com/questions/47960999
最满意答案
你可以用这个单一的条件替换你的所有条件:
if let index = names.indexOf(battlename) { print(battlename, "Vs.", monsters[index]) }
此外,如果您想要在前奏中打印怪物名称列表,而不是只打印数组,您可以将字符串作为字符串加入:
print(battlename, "Vs.", monsters.joinWithSeparator(", ")) // "Arthur Vs. Dragon, Boar, Giant, Griffin"
You can replace all your conditions with this single one:
if let index = names.indexOf(battlename) { print(battlename, "Vs.", monsters[index]) }
Also, a tip if you want to print the list of the monsters names in your intro, instead of just printing the array you can join the items as a string:
print(battlename, "Vs.", monsters.joinWithSeparator(", ")) // "Arthur Vs. Dragon, Boar, Giant, Griffin"
相关问答
更多-
如果数组为空,则必须在if let语句之外显式检查,因为 空数组永远不是可选的 if let response = data["response"] as? [String: AnyObject], groups = response["groups"] as? NSArray { if !groups.isEmpty { if let dic = groups[0] as? NSDictionary { items = dic["items"] as! NSArray ...
-
更简明的退货声明(A More Concise Return Statement)[2022-11-16]
您可以使用三元运算符: int valueCompare(int i, int j) { return (i == j) ? 1 : -1; } You can use the ternary operator: int valueCompare(int i, int j) { return (i == j) ? 1 : -1; } -
matches_by = matches.group_by {|m| m.class.to_s } %w{Graphic Category SubCategory Collection}.map do |class_name| matches_by[class_name] || [] end 如果你不在乎他们的订单,试试这个 matches.group_by {|m| m.class.to_s }.values 如果你只想要一个Hash,由类名索引(其中每个条目的值是该类型的对象数组),它只是 m ...
-
如果要保留该state变量,请替换 state = typeof states[i + 1] !== 'undefined' ? states[i + 1] : false 同 state = states[(i+1)%states.length] 剩余运营商 否则,通过使用工厂来保持功能会更干净: var next = (function(){ const states = [false, 'zoomIn', 'addBg', 'zoom', 'removeBg', 'zoomOut']; ...
-
你可以用这个单一的条件替换你的所有条件: if let index = names.indexOf(battlename) { print(battlename, "Vs.", monsters[index]) } 此外,如果您想要在前奏中打印怪物名称列表,而不是只打印数组,您可以将字符串作为字符串加入: print(battlename, "Vs.", monsters.joinWithSeparator(", ")) // "Arthur Vs. Dragon, Boar, Giant, Gr ...
-
你可以使用defaultdict : from collections import defaultdict d = defaultdict(list) d['something'].append(3) print d['something'] # > [3] You can use a defaultdict: from collections import defaultdict d = defaultdict(list) d['something'].append(3) print d['s ...
-
如何使这个Swift事件处理程序样板更简洁?(how can I make this Swift event handler boilerplate more concise?)[2023-09-06]
NSNotificationCenter 您的问题有多种解决方案。 一种可能是简单地使用NSNotificationCenter而不是实现自己的事件机制。 使用NSNotificationCenter您可以注册活动: NSNotificationCenter.defaultCenter().addObserver( self, selector: #selector(MyClass.myMethod), name: "com.mycompany.myEvent1", object: userData) 并 ... -
==仍然适用于期权,所以你可以做这样的事情: assert(vc1?.activityIndicator.isAnimating == true || v22?.activityIndicator.isAnimating == true) == still works for optionals, so you could do something like this: assert(vc1?.activityIndicator.isAnimating == true || v ...
-
是否有更简洁的方式将许多对象添加到数组列表中?(Is there a more concise way to add many objects to an arraylist?)[2023-08-16]
你可以使用像: ArrayListkwh = new ArrayList (Arrays.asList(data, moreData, evenMoreData)); 或者,您可以使用Guava的Lists类。 要添加新元素: List ints = Lists.newArrayList(1, 2, 3); 但是,这个不是固定的大小。 之后您可以通过调用ints.add()来添加新元素。 You could use like: ArrayList ... -
将值作为数组传递并检查indexOf 。 在你的情况下, if ([3, 7, 13, 19].indexOf(aInd) > -1) { letter = alphabet[aInd + 1].toUpperCase(); } Pass the values as an array and check the indexOf. In your case, if ([3, 7, 13, 19].indexOf(aInd) > -1) { letter = alphabet[aInd + 1]. ...