小编San*_*raj的帖子

unsigned short int的格式说明符是什么?

我有以下程序

#include <stdio.h>

int main(void)
{
    unsigned short int length = 10; 

    printf("Enter length : ");
    scanf("%u", &length);

    printf("value is %u \n", length);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

其中编译时使用gcc filename.c发出以下警告(scanf()在行中).

warning: format ‘%u’ expects argument of type ‘unsigned int *’, but argument 2 has type ‘short unsigned int *’ [-Wformat]

然后我提到的C99 specification - 7.19.6 Formatted input/output functions,并使用长度调节剂(如当不明白正确的格式指定符short,long等)与unsigned用于int数据类型.

%u正确的说明符unsigned short int吗?如果是这样,为什么我得到上述警告?!

编辑:大多数时候,我在尝试%uh,它仍在发出警告.

c scanf

118
推荐指数
4
解决办法
26万
查看次数

GDB损坏了堆栈帧 - 如何调试?

我有以下堆栈跟踪.是否可以从中进行任何有用的调试?

Program received signal SIGSEGV, Segmentation fault.
0x00000002 in ?? ()
(gdb) bt
#0  0x00000002 in ?? ()
#1  0x00000001 in ?? ()
#2  0xbffff284 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) 
Run Code Online (Sandbox Code Playgroud)

当我们得到一个时,从哪里开始查看代码Segmentation fault,并且堆栈跟踪不是那么有用?

注意:如果我发布代码,那么SO专家会给我答案.我想从SO那里得到指导并自己找到答案,所以我不会在这里发布代码.道歉.

c recursion gdb

111
推荐指数
4
解决办法
8万
查看次数

在64位计算机上应该是sizeof(int)应该是什么?

可能重复:
int,long等
的大小int的大小是否取决于编译器和/或处理器?
什么决定整数的大小?

我正在使用一64-bit台机器.

$ uname -m
x86_64
$ file /usr/bin/file
/usr/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, stripped
$ 
Run Code Online (Sandbox Code Playgroud)

当我运行以下程序时,我得到了sizeof(int)as 4-bytes.

#include <stdio.h>

int main(void)
{
    printf("sizeof(int) = %d bytes\n", (int) sizeof(int));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我运行一个16-,32-64-位机,那么不就意味着一个大小integer16-,32-64-分别位?

在我的机器上,我找到了WORD_BITIS 32.它不应该6464-bit机器上吗?

$ getconf WORD_BIT
32 …
Run Code Online (Sandbox Code Playgroud)

c c++ sizeof

65
推荐指数
4
解决办法
15万
查看次数

char*envp []是main()可移植的第三个参数

为了在C程序中获取环境变量,可以使用以下内容:

  • getenv()
  • extern char **environ;

但除了上面提到的,使用char *envp[]第三个参数main()来获取环境变量被认为是标准的一部分?

#include <stdio.h>

int main(int argc, char *argv[], char *envp[])
{
    while(*envp)
        printf("%s\n",*envp++);
}
Run Code Online (Sandbox Code Playgroud)

char *envp[]便携式?

c environment-variables command-line-arguments

59
推荐指数
4
解决办法
4万
查看次数

vim退格离开^?

在Vim中,当我backspaceinsert模式中按下键时,它会保留^?字符并且不会删除它想要删除的字符.

我有以下内容 .vimrc

syntax on
set number
set expandtab
set incsearch
set nocompatible
set backspace=indent,eol,start
fixdel
Run Code Online (Sandbox Code Playgroud)

这也发生在命令模式中.当我错误地键入W而不是w保存时,我按下backspace键,它给了我以下内容:

:W^?
Run Code Online (Sandbox Code Playgroud)

关于什么是错的任何想法以及如何解决它?!

更新:在将此问题发布到SO之前,我已经完成了基本的谷歌搜索并尝试了搜索结果第一页的所有建议,但未成功.

@strcat我正在使用vim版本7.0.237,KDE控制台1.6.4,Linux 2.6.18 x86_64机器.

@graywh wrt cat -v,对于删除,我得到^[[3~退格,我得到^?.

输出stty -a如下

speed 38400 baud; rows 38; columns 194; line = 0;
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = …
Run Code Online (Sandbox Code Playgroud)

vi vim keymapping backspace

54
推荐指数
5
解决办法
6万
查看次数

vi - 如何生成数字序列?

有没有办法在vi(m)中生成数字序列?

例如,从文件中的随机行(在vim中打开),比如Row-i - 到随机行,比如Row-j,其中Row-i <Row-j,有没有办法从Row生成数字序列-i到Row-j从数字1开始到数字j-i + 1,步长增量为1?

假设我在文件中有以下行.

this is line #1
this is line #2
this is line #3
this is line #4
this is line #5
this is line #6
this is line #7
this is line #8
this is line #9
this is line #10
Run Code Online (Sandbox Code Playgroud)

我想要从第4行到第8行的数字序列作为前缀,从数字1到数字5开始.操作后,生成的文件应如下所示:

this is line #1
this is line #2
this is line #3
1 this is line #4
2 this is line #5
3 this is line #6
4 this is line …
Run Code Online (Sandbox Code Playgroud)

vi vim pattern-matching sequential

44
推荐指数
5
解决办法
3万
查看次数

如何使用灵活的数组成员初始化结构

我有以下结构

typedef struct _person {
    int age;
    char sex;
    char name[];
}person;
Run Code Online (Sandbox Code Playgroud)

关于如何创建实例并使用灵活的数组成员初始化结构而不使用,我做了一些基本的互联网搜索(但不成功)malloc().

例如:对于普通结构,如

struct a {
    int age; 
    int sex;
};
Run Code Online (Sandbox Code Playgroud)

我们可以创建一个实例struct a并初始化它

struct a p1 = {10, 'm'};
Run Code Online (Sandbox Code Playgroud)

但是对于具有灵活数组的结构(_person如上所述),我们如何创建一个实例并初始化,就像我们如何正常做一样structures

它甚至可能吗?如果是这样,我们如何在初始化期间传递数组大小以及要初始化的实际值?

(要么)

创建具有灵活数组的结构的唯一方法是使用malloc()C99规范中提到的 - 6.7.2.1 Structure and union specifiers - point #17?!

c struct flexible-array-member

22
推荐指数
3
解决办法
2万
查看次数

计算位数 - 哪种方法最有效?

有一种以上的解决方案可以找到给定数字中的数字位数.

例如:

方法1:

int findn(int num)
{
    char snum[100];
    sprintf(snum, "%d", num);
    return strlen(snum);
}
Run Code Online (Sandbox Code Playgroud)

方法2:

int findn(int num)
{
    if (num == 0) return 1;
    int n = 0;
    while(num) {
        num /= 10;
        n++;
    }
    return n;
}
Run Code Online (Sandbox Code Playgroud)

方法-3:

int findn(int num)
{
    /* math.h included */
    return (int) log10(num) + 1;
}
Run Code Online (Sandbox Code Playgroud)

问题是 - 什么是最有效的方法?我知道方法-2 O(n)但是方法1和方法3怎么样?如何找到库函数的运行时复杂性?

c time-complexity

21
推荐指数
4
解决办法
4万
查看次数

typedef是存储类说明符吗?

我尝试了以下代码

#include <stdio.h>

int main(void)
{
    typedef static int sint;
    sint i = 10;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

并遇到以下错误:

error: multiple storage classes in declaration specifiers
Run Code Online (Sandbox Code Playgroud)

当我提到C99规范时,我才知道这typedef是一个storage class.

6.7.1 Storage-class specifiers

Syntax

storage-class-specifier:
    typedef
    extern
    static
    auto
    register

Constraints: At most, one storage-class specifier may be 
             given in the declaration specifiers in a declaration

Semantics: The typedef specifier is called a ‘‘storage-class specifier’’ 
           for syntactic convenience only; 
Run Code Online (Sandbox Code Playgroud)

我能找到的唯一解释(基于一些互联网搜索和交叉引用C99规范中的各个部分)是syntactic convenience only to make the grammar simpler. …

c typedef

17
推荐指数
2
解决办法
1万
查看次数

当一个程序终止使用不是免费的malloc分配的内存时会发生什么?

说我有以下程序

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    int * i;

    if ((i = malloc(sizeof(int) * 100)) == NULL) {
        printf("EROOR: unable to allocate memory \n");
        return -1;
    }

    /* memory is allocated successfully */

    /* memory is not free'ed but program terminates */
    // free(i);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的程序调用malloc分配一些内存而不调用free取消分配它.程序终止而不取消分配内存.

Valgrind清楚地发现内存泄漏.

<snap>
==14209== HEAP SUMMARY:
==14209==     in use at exit: 400 bytes in 1 blocks
==14209==   total heap usage: 1 allocs, 0 frees, 400 bytes allocated …
Run Code Online (Sandbox Code Playgroud)

c malloc free valgrind memory-leaks

16
推荐指数
2
解决办法
6151
查看次数