snprintf事例

sizeof Operator

sizeof expression

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.

The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses).sizeof后面跟的表达式(expression)既可以是标志符,也可以是类型转换的表达式(类型的说明包含在))

When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.

 

strlen

Get the length of a string.

Routine Required Header

strlen <string.h>

size_t strlen( const char *string );

Parameter

stringNull-terminated string

Libraries

All versions of the C run-time libraries.

Return Value

Each of these functions returns the number of characters in string, excluding the terminal NULL. No return value is reserved to indicate an error.

 

Remarks

Each of these functions returns the number of characters in string, not including the terminating null character. wcslen is a wide-character version of strlen; the argument of

wcslen is a wide-character string. wcslen and strlen behave identically otherwise.


snprintf


  int snprintf(char *str, size_t size, const char *format, ...);
  将可变个参数(...)按照format格式化成字符串,然后将其复制到str中
  (1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('\0');
  (2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0')
  函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值。


例:

char str1[3] = "123";

char str2[3] = "";

snprintf(str2, sizeof(str1), "%s", str1);

printf("str2 = %s\n", str2);

例子有问题,看家自己分析。面试的时候遇到的。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值