小编chq*_*lie的帖子

为什么unsigned int 1低于char y -1?

main() {
    unsigned x = 1;
    char y = -1;

    if (x > y)
        printf("x>y");
    else
        printf("x<=y");
}
Run Code Online (Sandbox Code Playgroud)

我期待x> y.但当我将unsigned int更改为signed int时,我得到了预期的结果.

c

8
推荐指数
1
解决办法
527
查看次数

scanf跳过

我正在尝试为一个类创建一个简单的C程序,其中一个要求是我需要使用scanf/ printf用于所有输入和输出.我的问题是为什么我scanf跳过主循环中的for循环并且程序刚终止.

这是我的代码

#include <stdio.h>

void main() {
    int userValue;
    int x;
    char c;

    printf("Enter a number : ");
    scanf("%d", &userValue);
    printf("The odd prime values are:\n");
    for (x = 3; x <= userValue; x = x + 2) {
        int a;
        a = isPrime(x);
        if (a = 1) { 
            printf("%d is an odd prime\n", x);
        }
    }   
    printf("hit anything to terminate...");
    scanf("%c", &c);    
}

int isPrime(int number) {
    int i;
    for (i = 2; i …
Run Code Online (Sandbox Code Playgroud)

c scanf

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

如何使用fgets()来避免转换第二个int类型的参数?

fgets函数的声明如下所示:

char *fgets(char *str, int n, FILE *stream);
Run Code Online (Sandbox Code Playgroud)

这意味着第二个参数应该是一个int.

在以下程序中哪种方法可以避免这种转换?

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

int main(void) {
    const char *buffer = "Michi";
    size_t len = strlen(buffer) + 1;
    char arr[len];

    printf("Type your Input:> ");
    if (fgets(arr, (int)len, stdin) == NULL) {
        printf("Error, fgets\n");
        exit(1);
    } else {
        printf("Arr = %s\n", arr);
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我使用(int)len哪个看起来很好,但如果buffer存储一个很长的字符串会发生什么?

让我们说:

const char *buffer = "Very long long ..."; /* where length is beyond the range of …
Run Code Online (Sandbox Code Playgroud)

c casting

8
推荐指数
1
解决办法
635
查看次数

使用\来扩展单行注释

我只是注意到我可以使用\将单行注释扩展到下一行,类似于在预处理器指令中这样做.

为什么没有人说这种语言功能?我甚至没有在书中看到它..什么语言版本支持这个?

c comments c-preprocessor

8
推荐指数
3
解决办法
162
查看次数

2 ways of accessing value of an array of pointers in C

The first block

#include <stdio.h>

const int MAX = 3;

int main() {

    int  var[] = { 10, 100, 200 };
    int i, *ptr[MAX];

    for (i = 0; i < MAX; i++) {
        ptr[i] = &var[i]; /* assign the address of integer. */
    }

    for (i = 0; i < MAX; i++) {
        printf("Value of var[%d] = %d\n", i, *ptr[i]);
    }

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

Easy to understand, since ptr is an array of int pointers. So when you need to …

c arrays printf pointers format-specifiers

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

用fgets()替换gets()

我一直在测试这个结构,我收到有关使用的警告gets.有人提到要使用fgets而是替换结尾'\0'.任何建议我如何更改我的代码来做到这一点?

void regCars(Car reg[], int *pNrOfCars) {
    char again[WORDLENGTH] = "yes", model[WORDLENGTH], tmp[WORDLENGTH];
    int year, milage;

    while (strcmp(again, "yes") == 0) {
        printf("Enter model:");
        gets(model);
        printf("Enter Year:");
        gets(tmp);
        year = atoi(tmp);
        printf("Enter milage:");
        gets(tmp);
        milage = atoi(tmp);
        reg[*pNrOfCars] = createCar(model, year, milage); 
        (*pNrOfCars)++;
        printf("Continue? (yes/no)");
        gets(again);
    }
}
Run Code Online (Sandbox Code Playgroud)

c gets fgets

7
推荐指数
1
解决办法
663
查看次数

为什么有些 C/C++ 头文件前面有“sys/”?

一些 C/C++ 标头前面<sys/ioctl.h>有一个前缀。sys/

(也有一些带有net/前缀。)

为什么是这样?

c c++ header

7
推荐指数
1
解决办法
2471
查看次数

C中的严格别名规则

我正在尝试理解严格的别名规则,如下所述6.5(p6):

如果通过具有非字符类型的左值的值将值存储到没有声明类型的对象中,则左值的类型将成为该访问的对象的有效类型以及不修改该值的后续访问的有效类型储值.

并且6.5(p7):

对象的存储值只能由具有以下类型之一的左值表达式访问:88)

- 兼容对象的有效类型的类型

请考虑以下示例:

struct test_internal_struct_t{
    int a;
    int b;
};

struct test_struct_t{
    struct test_internal_struct_t tis;
};

int main(){
    //alocated object, no declared type
    struct test_struct_t *test_struct_ptr = malloc(sizeof(*test_struct_ptr)); 

    //object designated by the lvalue has type int
    test_struct_ptr->tis.a = 1; 

    //object designated by the lvalue has type int
    test_struct_ptr->tis.b = 2; 

    //VIOLATION OF STRICT ALIASING RULE???
    struct test_internal_struct_t tis = test_struct_ptr->tis; 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在malloc(sizeof(*test_struct_ptr))没有声明的类型,因为它被分配作为脚注87:

87)分配的对象没有声明的类型

对象通过访问test_struct_ptr->tis.a和test_struct_ptr->tis.b具有 …

c strict-aliasing language-lawyer

7
推荐指数
1
解决办法
137
查看次数

找到一种时间复杂度为 O(n + k*log(k)) 的整数排序算法

设计一个算法,对存在重复的n 个整数进行排序。不同数字的总数是k。你的算法应该有时间复杂度O(n + k*log(k))。预计时间足够了。对于k 的哪些值,算法变为线性?

我无法提出满足条件的整数排序算法,它必须是O(n + k*log(k))。我不是一个非常高级的程序员,但在这个人应该为xi列表中的所有数字提出一种算法之前,我遇到了问题0 ? xi ? m,该算法是 O(n+m),其中 n 是列表中的元素数列表和 m 是列表中最大整数的值。我通过使用计数排序轻松解决了这个问题,但我在这个问题上很挣扎。对我来说最困难的k*log(k)条件是 ordo 表示法下的术语,如果n*log(n)相反,我将能够使用合并排序,对吗?但现在这是不可能的,所以任何想法都会非常有帮助。

提前致谢!

sorting algorithm mergesort time-complexity counting-sort

7
推荐指数
1
解决办法
437
查看次数

为什么 C99 中的这个语句在 C11 中发生了变化?

C99标准:

在上一个和下一个序列点之间,一个对象的存储值最多应通过表达式的评估修改一次

C11标准:

如果一个标量对象的副作用相对于同一个标量对象的另一个副作用是无序的,则行为是未定义的。

那么 C99 标准的这个定义是否不完整,因此它在 C11 中进行了更新,因为它只包含术语object而不包含术语scalar object?

c object c99 language-lawyer c11

7
推荐指数
1
解决办法
241
查看次数