小编reu*_*mut的帖子

C中的长双

我一直在读C Primer Plus书并得到了这个例子

#include <stdio.h>
int main(void)
{
    float aboat = 32000.0;
    double abet = 2.14e9;
    long double dip = 5.32e-5;

    printf("%f can be written %e\n", aboat, aboat);
    printf("%f can be written %e\n", abet, abet);
    printf("%f can be written %e\n", dip, dip);

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

在我的macbook上运行后,我对输出感到非常震惊:

32000.000000 can be written 3.200000e+04
2140000000.000000 can be written 2.140000e+09
2140000000.000000 can be written 2.140000e+09
Run Code Online (Sandbox Code Playgroud)

所以我仔细看了一下,发现显示long double的正确格式是使用%Lf.不过,我仍然不明白为什么我得到了双重abet价值,而不是我得到了什么,当我跑它在Cygwin的,Ubuntu的iDeneb的话这大概是

-1950228512509697486020297654959439872418023994430148306244153100897726713609
013030397828640261329800797420159101801613476402327600937901161313172717568.0
00000 can be …
Run Code Online (Sandbox Code Playgroud)

c long-double

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

C中的逻辑运算符有什么意义?

我只是想知道C中是否有一个XOR逻辑运算符(类似于&&表示AND,但是对于XOR).我知道我可以将XOR分成AND,NOT和OR,但简单的XOR会好得多.然后我想到如果我在两个条件之间使用普通的XOR位运算符,它可能会起作用.对于我的测试,它做到了.

考虑:

int i = 3;
int j = 7;
int k = 8;
Run Code Online (Sandbox Code Playgroud)

仅仅为了这个相当愚蠢的例子,如果我需要k大于i或大于j而不是两者,XOR将是非常方便的.

if ((k > i) XOR (k > j))
   printf("Valid");
else
   printf("Invalid");
Run Code Online (Sandbox Code Playgroud)

要么

printf("%s",((k > i) XOR (k > j)) ? "Valid" : "Invalid");
Run Code Online (Sandbox Code Playgroud)

我把按位XOR ^,它产生"无效".将两个比较的结果放在两个整数中导致2个整数包含1,因此XOR产生错误.然后我用&和|尝试了 按位运算符和两者都给出了预期的结果.知道真实条件具有非零值,而虚假条件具有零值,所有这些都是有意义的.

我想知道,是否有理由使用逻辑&&和|| 当按位运算符&,| 和^工作一样吗?

c xor bitwise-operators logical-operators

5
推荐指数
2
解决办法
839
查看次数

fork() 和 pipe()

我需要有关此示例应用程序的帮助。当我运行它时,它在子进程打印“Child Sending!”后卡住了。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>

#define INPUT 0
#define OUTPUT 1
int main()
{
    int fd1[2];
    int fd2[2];
    int pid;
    if (pipe(fd1) < 0)
        exit(1);
    if (pipe(fd2) < 0)
        exit(1);
    if ((pid = fork()) < 0)
    {
        perror("fork");
        exit(1);
    }
    else if (pid == 0)
    {
        close(fd1[INPUT]);
        close(fd2[OUTPUT]);
        char *str = "Hello World!";
        printf("Child sending!\n");
        write(fd1[OUTPUT], str, strlen(str));
        char *bufferc = (char *)malloc(1000);
        char *readbufferc = (char *)malloc(80);
        int rdc;
        int gotdata = …
Run Code Online (Sandbox Code Playgroud)

c fork pipe

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

标签 统计

c ×3

bitwise-operators ×1

fork ×1

logical-operators ×1

long-double ×1

pipe ×1

xor ×1