我们知道这sizeof
是一个用于计算任何数据类型和表达式大小的运算符,当操作数是表达式时,括号可以省略.
int main()
{
int a;
sizeof int;
sizeof( int );
sizeof a;
sizeof( a );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第一次使用sizeof
是错误的,而其他人则是对的.
当使用gcc编译时,将给出以下错误消息:
main.c:5:9: error: expected expression before ‘int’
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么C标准不允许这种操作.会不会sizeof int
引起任何歧义?
为什么我不能在类中初始化非const static
成员或static
数组?
class A
{
static const int a = 3;
static int b = 3;
static const int c[2] = { 1, 2 };
static int d[2] = { 1, 2 };
};
int main()
{
A a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器发出以下错误:
g++ main.cpp
main.cpp:4:17: error: ISO C++ forbids in-class initialization of non-const static member ‘b’
main.cpp:5:26: error: a brace-enclosed initializer is not allowed here before ‘{’ token
main.cpp:5:33: error: invalid in-class initialization of static data …
Run Code Online (Sandbox Code Playgroud) 当我在Linux中编写C程序,然后使用gcc编译它们时,我总是很好奇这些头文件的位置.例如,在哪里stdio.h
.更一般地说,在哪里stdbool.h
?
我想知道的不仅仅是它的位置,还有如何获取这些地方,例如,使用shell命令或使用C编程语言.
我有一个混合了小写字母和大写字母的文件,我可以awk
用来将该文件中的所有字母转换成大写字母吗?
假设我有一个包含一些文件的目录:
$ ls
a.c b.c e.c k.cpp s.java
Run Code Online (Sandbox Code Playgroud)
如何在没有文件扩展名的情况下显示结果(点后面的部分,包括该点)?像这样:
$ <some command>
a
b
e
k
s
Run Code Online (Sandbox Code Playgroud) 我有一个文件,比如all
2000行,我希望它可以分成4个小文件,行号为1~500,501~1000,1001~1500,1501~2000.
也许,我可以这样做:
cat all | head -500 >small1
cat all | tail -1500 | head -500 >small2
cat all | tail -1000 | head -500 >small3
cat all | tail -500 >small4
Run Code Online (Sandbox Code Playgroud)
但是这种方式涉及行号的计算,当行数不是一个好的数字时,或者当我们想要将文件拆分成太多的小文件时(例如:all
带有3241行的文件,我们想要将它分成7个文件,每个文件有463行).
有一个更好的方法吗?
为什么在条件运算符(?:
),第二和第三个操作数必须具有相同的类型?
我的代码是这样的:
#include <iostream>
using std::cout;
int main()
{
int a=2, b=3;
cout << ( a>b ? "a is greater\n" : b ); /* expression ONE */
a>b? "a is greater\n" : b; /* expression TWO */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用g ++编译时,会发出错误:
main.cpp:7:36: error: operands to ?: have different types ‘const char*’ and ‘int’
main.cpp:8:28: error: operands to ?: have different types ‘const char*’ and ‘int’
Run Code Online (Sandbox Code Playgroud)
我想知道为什么他们必须有相同的类型?
(1)在我看来,如果(a>b)
是,那么表达式( a>b ? "a is …
在C++中使用标准C函数时,我们应该为每个函数添加前缀std::
吗?
例如(文件名:) std.C
:
#include <cstdio>
int main() {
std::printf("hello\n");
printf("hello\n");
}
Run Code Online (Sandbox Code Playgroud)
该文件可以编译为:
g++ -Wall -Werror -std=c++11 std.C
Run Code Online (Sandbox Code Playgroud)
没有任何错误.
我的问题是:
std::
在C++中使用它们时,我们是否应该始终放在所有标准C库函数之前?<stdio.h>
和<cstdio>
?我有两个代码块new[]
和delete[]
:
1)
#include <string>
int main()
{
std::string *p = new std::string[0];
delete[] p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
2)在这种情况下,我只是std::string
改为int
int main()
{
int *p = new int[0];
delete[] p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
为什么第一个程序崩溃时出现以下消息(在linux环境中):
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
但第二个程序运行良好没有任何错误?
编辑
编译: g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
我只是使用g++
没有任何参数来编译它.
如果是编译器错误,它们是否会根据标准崩溃?
我早期跟踪了很多文件,但是我不希望git从现在起再跟踪它们.
我可以根据.gitignore
文件解开这些文件吗?
编辑
文件太多而且它们在许多不同的目录中分开,因此逐个删除它们是不切实际的,相反,我希望它们可以根据.gitignore
文件中的模式进行不跟踪.