Skip to content

Instantly share code, notes, and snippets.

@ngn999
Created September 25, 2012 11:27
Show Gist options
  • Save ngn999/3781227 to your computer and use it in GitHub Desktop.
Save ngn999/3781227 to your computer and use it in GitHub Desktop.
scanf,printf的那些变态用法
ret = sscanf(p,"%*s %1024s",request);
if(ret!=1){
    ...
}

p是"GET /index.html", 这行代码的意思是说直接取"/index.html", 跳过前面的"GET ".
这种用法, 避免定义一个哑变量.


int len = sprintf(buf, "%.*s", LEN, str);�

最多写LEN个字符到buf.

切记不要写下面这种 ad hoc 的代码:

int len = sprintf(buf, "%2047s", str);

vsnprintf的返回值:

If the output was trun- cated due to this limit then the return value is the number of charac- ters (not including the trailing ’\0’) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.) If an output error is encountered, a negative value is returned.

@henshao
Copy link

henshao commented Sep 25, 2012

很cool啊~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment