我正在尝试编写一个 C 程序,它从用户那里获取两个浮点数,然后使用 execv() 命令调用另一个程序。但我不能这样做,因为将 float 转换为 char 或者我不知道为什么。问题是 execv() 命令不起作用;输出必须是这样的
输入第一个数字:5
输入第二个数字:7
5.000000 + 7.000000 = 12.000000
父PID:9745 子PID:9746 现在可以使用
但现在就是这样
输入第一个数字:5
输入第二个数字:7
父 PID:9753 子 PID:9754 现在可以使用
我的第一个 C 程序 sum.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if(argc!=3)
printf("error...\n");
double a=atof(argv[1]);
double b=atof(argv[2]);
printf("%lf + %lf = %lf \n",a,b,a+b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和第二个程序calculate.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
float x,y;
pid_t pid;
printf("Enter first num: ");
scanf("%f",&x);
printf("Enter …Run Code Online (Sandbox Code Playgroud) 我正在通过C指针算术.我发现不允许添加指针但pointer + integer允许.
pointer + pointer由于安全原因,我认为是不允许的.但是,如果一个指针p1持有66400并p2持有,该怎么办66444?现在p1+p2是不被允许但是p1+66444被允许.为什么这样?
struct A {
struct B b;
};
struct B {
struct A a;
};
Run Code Online (Sandbox Code Playgroud)
这是代码,我无法弄清楚为什么它不工作(我总是得到一个结果0.000000):
#include <stdio.h>
#include <math.h>
int main ()
{
double num,b;
printf("Write a number between 0 and 256"); scanf("%lf",&num);
num = pow(2.0,b);
printf("%lf",b);
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
在这里,pow的答案被称为num,因为它是由用户引入的,我试图找到函数内部的b的值!
实际上,我在这里看到了类似的问题,但是其中一些问题依赖于gcc的解决方案,(我正在使用Dev C++),其余的结构略有不同,由于这种差异可能会出现问题.
任何人都可以解释为什么运行以下代码只打印换行符?
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
int x = 12;
char *s = (char *) &x;
printf("%s\n", s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因为我们正在&x作为一个字符串进行转换,所以不应该打印的是地址的字符串表示x(可能是一些十六进制的内存地址)?
strncpy()当我们将较少数量的数据复制到较大的(编译时分配的)缓冲区时,是否会导致内存泄漏?换句话说,内存利用不足会被称为内存泄漏吗?
以下是我的代码
#define uk "ln"
int main()
{
char buffer[32];
strncpy(buffer,uk,sizeof(buffer));// IS it memory leak free?
// uk macro has 3 byte of size to
// hold the string but here the
// Attemp of 32 byte is made to copy
// from uk macro to buffer?
}
Run Code Online (Sandbox Code Playgroud)
上面的代码中是否有内存泄漏或错误?
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main(){
stringstream ss;
ss << 32;
string str = ss.str();
cout << str << endl
<< str[0] << endl
<< str[1] <<endl
<< str[0]%10;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
32
3
2
1
相反,最后一行应为3,因为3%10 = 3.