int

Is `int` always signed?

时间秒杀一切 提交于 2021-01-27 04:17:04
问题 I always thought that in C, int stands for signed int ; but I have heard that this behavior is platform specific and in some platforms, int is unsigned by default. Is it true? What says the standard, and has it evolved over time? 回答1: You are quite right. As per C11 (the latest c standard), chapter §6.7.2 int , signed , or signed int is categorized as same type (type specifiers, to be exact). So, int is the same as signed int . Also, re-iterating the same, from chapter §6.2.5/P4 There are

itoa() c implementation int min underflow

﹥>﹥吖頭↗ 提交于 2021-01-26 04:27:01
问题 I'm running some test cases against my itoa() function but keep getting did not allocate memory for the int min value I'm doing the check but it's something I'm missing here, what is it? char *ft_itoa(int x) { char *s; size_t len; long int n; n = x; if (x == -2147483648) return (ft_strdup("-2147483648")); len = ft_intlen(n) + 1; if (!(s = (char*)malloc(sizeof(char) * len))) return (NULL); if (n == 0) s[0] = '0'; if (n < 0) { s[0] = '-'; n = -n; } s[len - 1] = '\0'; while (n) { len--; s[len -

itoa() c implementation int min underflow

℡╲_俬逩灬. 提交于 2021-01-26 04:26:42
问题 I'm running some test cases against my itoa() function but keep getting did not allocate memory for the int min value I'm doing the check but it's something I'm missing here, what is it? char *ft_itoa(int x) { char *s; size_t len; long int n; n = x; if (x == -2147483648) return (ft_strdup("-2147483648")); len = ft_intlen(n) + 1; if (!(s = (char*)malloc(sizeof(char) * len))) return (NULL); if (n == 0) s[0] = '0'; if (n < 0) { s[0] = '-'; n = -n; } s[len - 1] = '\0'; while (n) { len--; s[len -

How is the __format__ method supposed to be used for int?

你说的曾经没有我的故事 提交于 2021-01-22 06:54:22
问题 I saw there was a __format__ method but help( int.__format__ ) doesn't provide any help. I also know you're not suppose to call a __method__ directly. When is this method called? Which is its argument? 回答1: It's used for Py3k's new string formatting scheme. You can find more info here: http://docs.python.org/whatsnew/2.6.html#pep-3101-advanced-string-formatting You are right that it isn't called directly. It's called by str.format or the new format builtin. 回答2: It's used when you pass an

How is the __format__ method supposed to be used for int?

风格不统一 提交于 2021-01-22 06:52:48
问题 I saw there was a __format__ method but help( int.__format__ ) doesn't provide any help. I also know you're not suppose to call a __method__ directly. When is this method called? Which is its argument? 回答1: It's used for Py3k's new string formatting scheme. You can find more info here: http://docs.python.org/whatsnew/2.6.html#pep-3101-advanced-string-formatting You are right that it isn't called directly. It's called by str.format or the new format builtin. 回答2: It's used when you pass an

Why would uint32_t be preferred rather than uint_fast32_t?

穿精又带淫゛_ 提交于 2021-01-20 14:35:17
问题 It seems that uint32_t is much more prevalent than uint_fast32_t (I realise this is anecdotal evidence). That seems counter-intuitive to me, though. Almost always when I see an implementation use uint32_t , all it really wants is an integer that can hold values up to 4,294,967,295 (usually a much lower bound somewhere between 65,535 and 4,294,967,295). It seems weird to then use uint32_t , as the 'exactly 32 bits' guarantee is not needed, and the 'fastest available >= 32 bits' guarantee of

字符串(C语言)

青春壹個敷衍的年華 提交于 2021-01-07 05:45:45
1.如果要声明一个字符串“NoMasp”,下面这行代码错在哪里? char name[] = { 'N' , 'o' , 'M' , 'a' , 's' , 'p' }; 如果想声明的是字符串,就需要在初始化时在结尾加上一个’\0’。或者可以直接用下面这张方式: char name[] = "NoMasp" ; 2.以下这段代码会打印出什么? #include <stdio.h> int main() { char nomasp[]= "I like C language." ; char *ptr; ptr=nomasp; ++ptr; nomasp[ 6 ]= '\0' ; puts (++ptr); return 0 ; } 在ptr执行自增操作之后就是指向字母’I’后的空格,而后在索引为6的地方改为’\0’因此字符串在这里就被截断了。所以最后打印出来的是”like”。 3.’A’一定比”A”更加节省空间吗? 不一定。字符常量是存储在int中的,也就是说’A’会占用2个或4个字节,虽然它’A’实际上只使用了一个字节来存储它的编码。而”A”则使用了2个字节,一个字节用来保存’A’,另一个字节用来保存’\0’。 char c = 'A' ; c作为字符变量则占用1个字节。 为使本文得到斧正和提问,转载请注明出处: http://blog.csdn.net/nomasp 版权声明

golang: read text file line by line of int strings

两盒软妹~` 提交于 2021-01-01 04:42:28
问题 I am dealing with an input file containing a list of integers as a string 10 .. I have choosen to read it line by line with ReadString('\n') method The following code line, error := inputReader.ReadString('\n') lineStr := string(line) console output (length and value) lineStr %v 4 lineStr %v 10 lineStr as a length of "4", maybe because of rune encoding. Then I have tried several way to convert it to simple integer but with no success. Ex1 num, _ := strconv.ParseUint(lineStr, 0, 64) ouputs a

golang: read text file line by line of int strings

时光怂恿深爱的人放手 提交于 2021-01-01 04:41:11
问题 I am dealing with an input file containing a list of integers as a string 10 .. I have choosen to read it line by line with ReadString('\n') method The following code line, error := inputReader.ReadString('\n') lineStr := string(line) console output (length and value) lineStr %v 4 lineStr %v 10 lineStr as a length of "4", maybe because of rune encoding. Then I have tried several way to convert it to simple integer but with no success. Ex1 num, _ := strconv.ParseUint(lineStr, 0, 64) ouputs a

Quickest way to convert a base 10 number to any base in .NET?

扶醉桌前 提交于 2020-12-27 07:23:20
问题 I have and old(ish) C# method I wrote that takes a number and converts it to any base: string ConvertToBase(int number, char[] baseChars); It's not all that super speedy and neat. Is there a good, known way of achieving this in .NET? I'm looking for something that allows me to use any base with an arbitrary string of characters to use. This only allows bases 16, 10, 8 and 2: Convert.ToString(1, x); I want to use this to achieve a massively high base taking advantage of numbers, all lower case