我在Linux上用GCC(gcc版本4.8.4(Ubuntu 4.8.4-2ubuntu1~14.04.3))运行了这个程序.它成功编译使用gcc myprog.c和运行没有任何警告或错误.
那么,当C中只提供一个参数时,为什么编译器不会给出任何警告或错误main?
#include <stdio.h>
int main(int i)
{
printf("%d\n", i);
}
Run Code Online (Sandbox Code Playgroud) 我在C中有一个简单的代码,看看三个相同的char数组是否都以'\0':
int main(){
char a[4] = "1234";
char b[4] = "1234";
char c[4] = "1234";
if(a[4] == '\0')
printf("a end with '\\0'\n");
if(b[4] == '\0')
printf("b end with '\\0'\n");
if(c[4] == '\0')
printf("c end with '\\0'\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但输出显示只有数组b以终结符结束'\0'.这是为什么?我想所有的char数组都必须结束'\0'.
输出:
b以'\ 0'结尾
我有一小段代码:
#include <iostream>
using namespace std;
int main()
{
int *p = new int(10);
if(p != NULL)
{
cout<<"Deleted dynamic allocated memory"<<endl;
delete p;
}
if(p == NULL)
{
cout<<"NULL"<<endl;
}
else
{
cout<<"Not NULL"<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用deleteoperator 删除动态分配的内存后,为什么编译器不会自动为指针(如p = NULL)分配NULL?
如果我像这样初始化变量:
static int i = 2 * 2 / 0;
Run Code Online (Sandbox Code Playgroud)
然后,编译器给我一个错误.
prog.c: In function 'main':
prog.c:5:23: warning: division by zero [-Wdiv-by-zero]
static int i = 2 * 2 / 0;
^
prog.c:5:17: error: initializer element is not constant
static int i = 2 * 2 / 0;
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用||而不是*像这样:
static int i = 2 || 2 / 0;
Run Code Online (Sandbox Code Playgroud)
然后它成功编译.
根据运算符优先级,优先级*高于||.因此,2 / 0评估了第一次操作.我对吗?
那么,为什么不static int i = 2 || …
我constexpr在C++ 17中使用此参考链接阅读了该文章.
然后,我制作了C++程序进行测试constexpr:
#include <iostream>
int i = 10;
int func()
{
if constexpr (i == 0)
return 0;
else if (i > 0)
return i;
else
return -1;
}
int main()
{
int ret = func();
std::cout<<"Ret : "<<ret<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器会出错:
main.cpp: In function 'int func()':
main.cpp:8:25: error: the value of 'i' is not usable in a constant expression
if constexpr (i == 0)
^
main.cpp:4:5: note: 'int i' is not const
int …Run Code Online (Sandbox Code Playgroud) 我使用Cppreference阅读了有关宏的内容.
__LINE__:扩展为源文件行号,可以通过
#line指令更改整数常量
我制作了c ++程序来测试__LINE__宏.
#include <iostream>
using namespace std;
#line 10
#define L __LINE__
int main()
{
#line 20
int i = L;
cout<<i<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
20
Run Code Online (Sandbox Code Playgroud)
为什么输出上面的代码是20?为什么不是10?
我在这里看到了以下代码.
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the heck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st …Run Code Online (Sandbox Code Playgroud) 我不确切地知道从这种结构中返回真假的案例.你能描述一下吗?Sry,如果是谷歌搜索,我还没找到.
( (struct->param = param_init()) == NULL )
Run Code Online (Sandbox Code Playgroud) 我是C#的新手.请帮我.
data.xml中
<?xml version="1.0" standalone="yes"?>
<DataSet1 xmlns="http://tempuri.org/DataSet1.xsd">
<Language>
<Id>001</Id>
<English>"Welcome to India"</English>
<German>"Willkommen in Indien"</German>
</Language>
</DataSet1>
Run Code Online (Sandbox Code Playgroud)
如何阅读Id使用C#Xml的价值?我是C#的新手.请帮我.
我的努力:
XmlDocument doc = new XmlDocument();
XmlReader reader = XmlReader.Create(@"D:\data.xml");
while(reader.Read())
{
reader.MoveToContent();
if (reader.IsStartElement("DataSet1"))
{
reader.ReadToDescendant("Language");
string str2 = reader.Name.ToString();
MessageBox.Show(str2);
}
}
Run Code Online (Sandbox Code Playgroud) 在下面的程序中,我尝试调用函数n()和内部n(),尝试调用m()函数中定义的main函数,但是当我编译时,我得到以下错误:
Run Code Online (Sandbox Code Playgroud)In function `n': (.text+0xa): undefined reference to `m' error: ld returned 1 exit status
为什么我会收到错误?请解释.
代码在这里:
#include <stdio.h>
void m();
void n()
{
m();
}
void main()
{
n();
void m()
{
printf("hi");
}
}
Run Code Online (Sandbox Code Playgroud)