我在以下程序的第6行(初始化my_foo到foo_init)时收到错误,我不确定我理解为什么.
typedef struct foo_t {
int a, b, c;
} foo_t;
const foo_t foo_init = { 1, 2, 3 };
foo_t my_foo = foo_init;
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请记住,这是我正在研究的大型多文件项目的简化版本.目标是在目标文件中有一个常量,多个文件可用于初始化状态结构.由于它是一个资源有限的嵌入式目标,并且结构不是那么小,我不想要源的多个副本.我不想使用:
#define foo_init { 1, 2, 3 }
Run Code Online (Sandbox Code Playgroud)
我也在尝试编写可移植代码,所以我需要一个有效的C89或C99解决方案.
这是否与目标文件中的ORG有关?初始化变量进入一个ORG并通过复制第二个ORG的内容进行初始化?
也许我只需要改变我的策略,并在启动时使用初始化功能完成所有副本.除非有其他想法吗?
为什么在阅读时scanf()需要l" %lf" double,何时printf()可以使用" %f",无论其参数是a double还是a float?
示例代码:
double d;
scanf("%lf", &d);
printf("%f", d);
Run Code Online (Sandbox Code Playgroud) 我通过PuTTY和WinSCP连接到我大学的小型Linux集群,使用后者传输文件,并使用前者编译和运行它们.到目前为止,我的工作已在大学的实验室进行,但今天我在家里做了一些工作,产生了一个有趣的警告.
我上传了整个文件夹,在运行make命令后,我将其作为输出的最后一行:
make:warning:检测到时钟偏斜.您的构建可能不完整.
生成的二进制文件正常工作,并且在构建过程中似乎没有任何其他意外错误.
我似乎能够在上传一些新的/替换文件之后通过构建来触发错误(我在本地编辑所有内容然后上传新版本),所以我想知道它是否像文件修改时间不匹配一样简单?还是更关心的事情?
那么,我应该担心吗?我该如何修复/防止这种情况?
使用以下代码:
char *name = malloc(sizeof(char) + 256);
printf("What is your name? ");
scanf("%s", name);
printf("Hello %s. Nice to meet you.\n", name);
Run Code Online (Sandbox Code Playgroud)
用户可以输入他们的名字,但是当他们输入一个像空格一样的名字时Lucas Aardvark,scanf()只需切断后面的所有内容Lucas.如何设置scanf()允许空格
我正在使用Ubuntu,我也使用Geany和CodeBlock作为我的IDE.我正在尝试做的是读取一个字符串(如"Barack Obama")并将其放在一个变量中:
#include <stdio.h>
int main(void)
{
char name[100];
printf("Enter your name: ");
scanf("%s", name);
printf("Your Name is: %s", name);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Enter your name: Barack Obama
Your Name is: Barack
Run Code Online (Sandbox Code Playgroud)
如何让程序读取整个名称?
我有以下代码(根据这个问题的基础知识):
#include<stdio.h>
#include<math.h>
double f1(double x)
{
double res = sin(x);
return 0;
}
/* The main function */
int main(void)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当gcc test.c我编译它时,我得到以下错误,我无法解决原因:
/tmp/ccOF5bis.o: In function `f1':
test2.c:(.text+0x13): undefined reference to `sin'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但是,我编写了各种sin从main函数内部调用的测试程序,并且这些程序完美地工作.我必须在这里做一些明显错误的事 - 但它是什么?
我正在尝试用C语言编写一个程序(在Linux上)循环,直到用户按下一个键,但不应该要求按键继续每个循环.
有一个简单的方法吗?我想我可以做到这一点,select()但这似乎很多工作.
或者,有没有办法在程序关闭之前捕获ctrl- ckeypress进行清理而不是非阻塞io?
我有这个简单的代码:
max = (int) sqrt (number);
Run Code Online (Sandbox Code Playgroud)
在标题中我有:
#include <math.h>
Run Code Online (Sandbox Code Playgroud)
但应用程序仍然表示未定义的引用sqrt.你觉得这里有什么问题吗?看起来一切都应该没问题.
只是一个简单的程序,但我一直得到这个编译错误.我正在使用MinGW作为编译器.
这是头文件,point.h:
//type for a Cartesian point
typedef struct {
double x;
double y;
} Point;
Point create(double x, double y);
Point midpoint(Point p, Point q);
Run Code Online (Sandbox Code Playgroud)
这是重点.:
//This is the implementation of the point type
#include "point.h"
int main() {
return 0;
}
Point create(double x, double y) {
Point p;
p.x = x;
p.y = y;
return p;
}
Point midpoint(Point p, Point q) {
Point mid;
mid.x = (p.x + q.x) / 2;
mid.y …Run Code Online (Sandbox Code Playgroud) c ×9
linux ×3
scanf ×3
input ×2
string ×2
whitespace ×2
asynchronous ×1
directory ×1
function ×1
makefile ×1
math ×1
nonblocking ×1
printf ×1