c-standard-library

Contradiction about Order of Evaluation of operands

蹲街弑〆低调 提交于 2021-01-27 07:24:57
问题 When I study recursive functions in C from deitel c, I read this sentence: Standard C does not specify the order in which the operands of most operators (including +) are to be evaluated. But also the book says that: associativity of '+' from left to right Order of evaluation of operands: Could anyone explain why this is? 回答1: Order of evaluation and associativity are two different things, take the example: int x = func1() - func2() - func3(); //having int return types In this expression you

“'getenv': This function or variable may be unsafe.” - really?

被刻印的时光 ゝ 提交于 2020-04-12 19:46:13
问题 I'm using MSVC to compile some C code which uses standard-library functions, such as getenv() , sprintf and others, with /W3 set for warnings. I'm told by MSVC that: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS Questions: Why would this be unsafe, theoretically - as opposed to its use on other platforms? Is it unsafe on Windows in practice? Assuming I'm not writing security-oriented code - should I

Is reading into uninitialized memory space ALWAYS ill advised?

隐身守侯 提交于 2020-01-17 14:03:13
问题 I am recreating the entire standard C library and I'm working on an implementation for strle n that I would like to be the basis of all my other str functions. My current implementation is as follows: int ft_strlen(char const *str) { int length; length = 0; while(str[length] != '\0' || str[length + 1] == '\0') length++; return length; } My question is that when I pass a str like: char str[6] = "hi!"; As expected, the memory reads: ['h']['i']['!']['\0']['\0']['\0']['\0'] If you look at my

Is reading into uninitialized memory space ALWAYS ill advised?

馋奶兔 提交于 2020-01-17 14:03:10
问题 I am recreating the entire standard C library and I'm working on an implementation for strle n that I would like to be the basis of all my other str functions. My current implementation is as follows: int ft_strlen(char const *str) { int length; length = 0; while(str[length] != '\0' || str[length + 1] == '\0') length++; return length; } My question is that when I pass a str like: char str[6] = "hi!"; As expected, the memory reads: ['h']['i']['!']['\0']['\0']['\0']['\0'] If you look at my

May wchar_t be promoted to wint_t?

孤者浪人 提交于 2020-01-04 02:18:25
问题 I see one contradiction of glibc reference and Amendment 1 to C90. The quote from glibc reference says that wchar_t may be promoted to wint_t: if wchar_t is defined as char the type wint_t must be defined as int due to the parameter promotion But AMD1 says this: Currently, an existing implementation could have wchar_t be int and wint_t be long, and default promotions would not change int to long. Basically, this is due to wchar_t and wint_t being typedefs. Hence, we will not now have wchar_t

Does redefining a function from the standard library violate the one-definition rule?

99封情书 提交于 2020-01-01 12:08:21
问题 #include <cmath> double log(double) {return 1.0;} int main() { log(1.0); } Suppose the function log() in <cmath> is declared in global namespace (this is unspecified in fact, and we just make this assumption), then it refers to the same function as the log() function we defined. So does this code violate the one-definition rule (see here, since no diagnostic required, this code may compile in some compiler and we cannot assert if it is correct)? Note : After recent edits, this is not a

Why there are no “unsigned wchar_t” and “signed wchar_t” types?

拥有回忆 提交于 2019-12-23 12:44:08
问题 The signedness of char is not standardized. Hence there are signed char and unsigned char types. Therefore functions which work with single character must use the argument type which can hold both signed char and unsigned char (this type was chosen to be int ), because if the argument type was char , we would get type conversion warnings from the compiler (if -Wconversion is used) in code like this: char c = 'ÿ'; if (islower((unsigned char) c)) ... warning: conversion to ‘char’ from ‘unsigned

Why there are no “unsigned wchar_t” and “signed wchar_t” types?

。_饼干妹妹 提交于 2019-12-23 12:39:32
问题 The signedness of char is not standardized. Hence there are signed char and unsigned char types. Therefore functions which work with single character must use the argument type which can hold both signed char and unsigned char (this type was chosen to be int ), because if the argument type was char , we would get type conversion warnings from the compiler (if -Wconversion is used) in code like this: char c = 'ÿ'; if (islower((unsigned char) c)) ... warning: conversion to ‘char’ from ‘unsigned

Why are islower() and friends required to handle EOF?

半腔热情 提交于 2019-12-13 14:12:31
问题 Why are islower() and friends required to handle EOF , whereas putchar() and friends don't have to? Why isn't islower() treating int as unsigned char , as it is the case in putchar() ? This would make total sense, because we have to check for EOF first anyway. See also Why the argument type of putchar(), fputc(), and putc() is not char? 回答1: because we have to check for EOF first anyway. We absolutely don't. int c; while(isspace(c=fgetc(fp))); if (c==EOF) ... This is totally legitimate code

What does the size argument of setvbuf mean for an unbuffered stream?

寵の児 提交于 2019-12-13 11:05:33
问题 The function setvbuf() can be used to make a stream unbuffered: #include <stdio.h> int setvbuf(FILE *stream, char *buf, int mode, size_t size); What does the value of the size argument mean when the mode is passed as _IONBF ? Will the buffer be allocated? Is it OK to pass 0 ? 回答1: The current version (C11) of the C Standard says: 7.21.5.6 The setvbuf function Synopsis #include <stdio.h> int setvbuf(FILE * restrict stream, char * restrict buf, int mode, size_t size); Description The setvbuf