为什么memset的采取一个int

为什么memset的采取一个int

本文介绍了为什么memset的采取一个int,而不是一个char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 memset的采取 INT 作为第二个参数,而不是一个字符,而 wmemset 需要 wchar_t的,而不是像长长

Why does memset take an int as the second argument instead of a char, whereas wmemset takes a wchar_t instead of something like long or long long?

推荐答案

memset的 $ P $(由颇有几分)增加函数原型为C.没有pdates原型,你的不能的传递字符来一个功能 - 当/如果你尝试,它会被提升到 INT 当你通过它,什么函数接收是 INT

memset predates (by quite a bit) the addition of function prototypes to C. Without a prototype, you can't pass a char to a function -- when/if you try, it'll be promoted to int when you pass it, and what the function receives is an int.

另外值得一提的是,在C,如'A'做的的有型字符 - 它具有类型 INT ,所以你通过什么会的一般的开始了作为一个 INT 反正。本质上它开始作为一个char和升职的唯一方法是,如果你传递一个字符变量。

It's also worth noting that in C, a character literal like 'a' does not have type char -- it has type int, so what you pass will usually start out as an int anyway. Essentially the only way for it to start as a char and get promoted is if you pass a char variable.

在理论上, memset的也许可以修改,以便它接收的字符代替 INT ,但有不太可能是任何好处,并突破一些旧code或其他的pretty像样的可能性。随着未知的,但潜在的费用相当高,几乎没有任何实际的好处的机会,我会说被更改了它的机会获得字符落在右边线之间的苗条和无。

In theory, memset could probably be modified so it receives a char instead of an int, but there's unlikely to be any benefit, and a pretty decent possibility of breaking some old code or other. With an unknown but potentially fairly high cost, and almost no chance of any real benefit, I'd say the chances of it being changed to receive a char fall right on the line between "slim" and "none".

编辑(回应评论)的 CHAR_BIT 至少显著位 INT 作为价值写入目标。

Edit (responding to the comments): The CHAR_BIT least significant bits of the int are used as the value to write to the target.

这篇关于为什么memset的采取一个int,而不是一个char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:01