我试图在 ANSI C 中执行此操作:
include <stdio.h>
int main()
{
printf("%d", 22);
int j = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在 Microsoft Visual C++ 2010(在 ANSI C 项目中)中不起作用。你会得到一个错误:
error C2143: syntax error : missing ';' before 'type'
Run Code Online (Sandbox Code Playgroud)
这确实有效:
include <stdio.h>
int main()
{
int j = 0;
printf("%d", 22);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我在很多地方读到,必须在变量所在的代码块的开头声明变量。这对于 ANSI C89 通常是这样吗?
我发现很多论坛上的人都给出了这个建议,但我没有看到它写在任何“官方”来源中,比如GNU C手册。
在.h 中:
typedef struct token_t TOKEN;
Run Code Online (Sandbox Code Playgroud)
在.c 中:
#include "token.h"
struct token_t
{
char* start;
int length;
int type;
};
Run Code Online (Sandbox Code Playgroud)
在main.c 中:
#include "token.h"
int main ()
{
TOKEN* tokens; // here: ok
TOKEN token; // here: Error: incomplete type is not allowed
// ...
}
Run Code Online (Sandbox Code Playgroud)
我在最后一行中得到的错误:
错误:不允许不完整的类型
怎么了?
我用 C读过这个问题,我在 C# 中也有同样的问题。
通过代码说是否正确
int a;
Run Code Online (Sandbox Code Playgroud)
a在初始化之前保留四个未分配的字节?或者初始化是第一次分配字节的地方?
是否可以在类声明之外定义函数或方法?如:
class A
{
int foo;
A (): foo (10) {}
}
int A::bar ()
{
return foo;
}
Run Code Online (Sandbox Code Playgroud) 我有一个模板类
实验.h:
template <class T>
class ExpOf{
...
}
Run Code Online (Sandbox Code Playgroud)
我在我的代码中反复使用它,例如T = double[以及其他ExpOf不应该知道的类]。所以我认为一次性编译它是个好主意[或两次......]
expofdouble.cpp:
#include "expof.h"
template class ExpOf<double>;
Run Code Online (Sandbox Code Playgroud)
并在不同的标头中声明它,这样当包含 expof.h 时它就不会被编译。
expofdouble.h:
extern template ExpOf<double>;
Run Code Online (Sandbox Code Playgroud)
当我编译这个(clang-800.0.42.1)时,我收到(许多)警告
expofdouble.h: warning: declaration does not declare anything [-Wmissing-declarations]
extern template ExpOf<double>;
^~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我得到了想要的行为吗?那为什么会出现警告呢?我应该采取不同的做法吗?
我C下面有 2 个文件。从我读到的我知道全局变量的默认存储类是 extern。如果我明确输入它,我会收到未定义的变量错误。我在这里缺少什么?这是否意味着当我省略 extern 关键字时它成为一个定义,但当我输入它时它只是一个声明?
文件1.c
#include <stdio.h>
#include <stdlib.h>
extern void file2function();
int variable; // if i put extern i will get error, isnt it implicitly extern?
int main()
{
variable = 1;
printf("file1 %d\n",variable);
file2function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
文件2.c
#include <stdio.h>
#include <stdlib.h>
extern int variable;
void file2function(){
variable = 2;
printf("file2 %d\n",variable);
return;
}
Run Code Online (Sandbox Code Playgroud) 所以在解释语言中,比如 javascript,我们有:
var x = doThis(); // function call, assign statement
console.log(x); // print statement
function doThis(){ //function declaration
return "Hello World"; //return statement
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
何时(运行时)打印语句实际执行?在解析函数声明之前还是之后?如果它之前被执行,如何,因为没有编译器,代码会立即执行。
PS 我已经阅读了一些关于函数提升的内容,但仍然不明白。
我有 2 个页面,有 2 个模块,并尝试使用相同的管道,所以我在每个页面中声明了相同的管道。
listview.module.ts
import { DateTimePipe } from '../pipes/dateTime.pipe';
declarations: [ListviewPage, DateTimePipe,]
Run Code Online (Sandbox Code Playgroud)
profile.module.ts
import { DateTimePipe } from '../pipes/dateTime.pipe';
declarations: [ProfilePage, DateTimePipe]
Run Code Online (Sandbox Code Playgroud)
为此,我遇到了这个问题:
错误错误:未捕获(承诺):错误:类型 DateTimePipe 是 2 个模块声明的一部分:ListviewPageModule 和 ProfilePageModule!请考虑将 DateTimePipe 移至导入 ListviewPageModule 和 ProfilePageModule 的更高模块。您还可以创建一个新的 NgModule 来导出并包含 DateTimePipe,然后在 ListviewPageModule 和 ProfilePageModule 中导入该 NgModule。错误:类型 DateTimePipe 是 2 个模块声明的一部分:ListviewPageModule 和 ProfilePageModule!请考虑将 DateTimePipe 移至导入 ListviewPageModule 和 ProfilePageModule 的更高模块。您还可以创建一个新的 NgModule 来导出并包含 DateTimePipe,然后在 ListviewPageModule 和 ProfilePageModule 中导入该 NgModule。
我尝试在 app.module.ts 中声明它但不起作用。
谢谢
Is such heavy use of backreferencing a declaration from the initialization code covered by at least one of the standards (C99-current) or is it a gcc extension? All the member initialization terms have in common that they either reference the type or a member/variable of that type from within the definition of the type.
#include <stddef.h>
#include <stdlib.h>
struct node{
int size;
int offset;
int *ptr;
struct node *this;
} s = {sizeof(s), offsetof(struct node, offset), &s.offset, &s};
int main(void){ …Run Code Online (Sandbox Code Playgroud) typedef struct {
//
} list;
Run Code Online (Sandbox Code Playgroud)
对比
typedef struct list{
//
} list;
Run Code Online (Sandbox Code Playgroud)
我在另一篇文章中读到过(例如使用匿名结构与带有 typedef 的命名结构),其中说这两个几乎是等效的,并且需要后者的唯一时间是在引用结构本身时。
但是,以下内容可以用 clang 和 gcc 编译得很好:
#include <stdio.h>
typedef struct {
struct list *next;
} list;
int main(){
list l;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面我有一个匿名结构引用它自己。这是如何编译的?
declaration ×10
c ×5
c++ ×2
definition ×2
extern ×2
function ×2
struct ×2
typedef ×2
variables ×2
c# ×1
ionic4 ×1
javascript ×1
templates ×1