c-standard-library

Does the standard require EOF to be negative?

二次信任 提交于 2019-12-12 05:14:09
问题 Is it written in the standard that EOF must be negative? On the contrary, WEOF is not required to be negative. Why? The situation for wchar_t must not be in any way different from ordinary char (except where automatic promotion from char to int plays the role), because defining wchar_t as char is perfectly fine with the standard. Therefore, similar rules must apply. Some quotations from glibc reference: if wchar_t is defined as char the type wint_t must be defined as int due to the parameter

snprintf: Are there any C Standard Proposals/plans to change the description of this func?

允我心安 提交于 2019-12-11 08:53:49
问题 Are there any Proposals (or plans) to the C language Standard to change the (last sentence of the) description of the snprintf function such that the ambiguity described in this my answer to the question - "Is snprintf() ALWAYS null terminating?"- is resolved? (Or how (using which links) can I determine by myself if there are any such Proposals? Is there any search engine that can show all the currently active Proposals about the snprintf function? The only link I currently know is this one -

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

不羁的心 提交于 2019-12-04 10:59:33
#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 duplicate of: What exactly is One Definition Rule in C++? Typical scenario. If extern "C" double log(double)

Advantages of strncmp over strcmp?

荒凉一梦 提交于 2019-12-03 06:03:14
Seems strncmp is usually recommended than strcmp , what are the advantages? I think it could be related to security. If this is the case, is it still applicable if one of the input string is known to be literal constant, like "LiteralString" ? UPDATE: I mean under the same user scenario where whole strings need to be compared, and strncmp can be used as below. I am wondering it makes sense or not. strncmp(inputString, "LiternalString", strlen("LiternalString")); Surajeet Bharati The problem with strcmp is that sometimes, if by mistake, arguments that are passed are not valid C-strings (meaning

Advantages of strncmp over strcmp?

你。 提交于 2019-11-29 12:34:01
问题 Seems strncmp is usually recommended than strcmp , what are the advantages? I think it could be related to security. If this is the case, is it still applicable if one of the input string is known to be literal constant, like "LiteralString" ? UPDATE: I mean under the same user scenario where whole strings need to be compared, and strncmp can be used as below. I am wondering it makes sense or not. strncmp(inputString, "LiternalString", strlen("LiternalString")); 回答1: The problem with strcmp

Why argument type of `putchar()`, `fputc()` and `putc()` is not `char`?

只愿长相守 提交于 2019-11-29 11:55:54
Does anybody know Why argument type of putchar() , fputc() and putc() is not char , but argument type of putwchar() , fputwc() and putwc() is wchar_t ? See also this and this . The answer is 'legacy' (or 'history'). Before the C90 standard, there were no function prototypes and all arguments to all functions were subject to default promotion rules, so a char was automatically passed as an int ( short was promoted to int too, and float to double , and similarly for unsigned types). The standard couldn't afford to break existing code, so it kept that type for these functions. It makes very

Why argument type of `putchar()`, `fputc()` and `putc()` is not `char`?

做~自己de王妃 提交于 2019-11-28 05:04:16
问题 Does anybody know Why argument type of putchar() , fputc() and putc() is not char , but argument type of putwchar() , fputwc() and putwc() is wchar_t ? See also this and this. 回答1: The answer is 'legacy' (or 'history'). Before the C90 standard, there were no function prototypes and all arguments to all functions were subject to default promotion rules, so a char was automatically passed as an int ( short was promoted to int too, and float to double , and similarly for unsigned types). The

what is difference between fgetpos/fsetpos and ftell/fseek

六眼飞鱼酱① 提交于 2019-11-27 18:09:57
What's the difference between using the functions fgetpos() and fsetpos() and using the functions ftell() and fseek() to get and set a position in a file? What are fgetpos() and fsetpos() good for? Why would they be used instead of ftell() and fseek() ? None of the above answers are correct - in fact if you use fsetpos interchangeably with fseek you could introduce a security flaw ( https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=20087255 ). The reason is that the fpos_t *pos argument to fsetpos isn't actually an integer so it can't be used to seek to arbitrary

Why isn't there int128_t?

橙三吉。 提交于 2019-11-27 09:00:26
A number of compilers provide 128-bit integer types, but none of the ones I've used provide the typedefs int128_t . Why? As far as I recall, the standard Reserves int128_t for this purpose Encourages implementations that provide such a type to provide the typedef Mandates that such implementations provide an intmax_t of at least 128 bits (and, I do not believe I've used an implementation that actually conforms to that last point) I'll refer to the C standard; I think the C++ standard inherits the rules for <stdint.h> / <cstdint> from C. I know that gcc implements 128-bit signed and unsigned

What is the difference between getch() and getchar()?

China☆狼群 提交于 2019-11-27 04:15:30
What is the exact difference between the getch and getchar functions? getchar() is a standard function that gets a character from the stdin. getch() is non-standard. It gets a character from the keyboard (which may be different from stdin) and does not echo it. The Standard C function is is getchar() , declared in <stdio.h> . It has existed basically since the dawn of time. It reads one character from standard input ( stdin ), which is typically the user's keyboard, unless it has been redirected (for example via the shell input redirection character < , or a pipe). getch() and getche() are old