我只是想知道在编译器中如何实现反斜杠转义序列?如果我们在字符串中写"\n",编译器如何用新行字符替换它?编译器如何用退格符替换"\ b"?
我问,因为我写了代码:
#include<stdio.h>
main()
{
printf("Hello \c");
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Hello Exited: ExitFailure 7
我在键盘中运行它,我正在通过KnR书籍问题编号1.2.
提前致谢
昨天,我接受了采访.在那里,他们问我代码优化何时发生?说,
int abc;//Global variable
abc = 3;
if(abc == 3)
{
printf("abc will be always 3");
}
else
{
printf("This will never executed");
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是优化何时发生?A ...在运行时B ...在编译时.我在编译时回答...我想,编译器在编译时检查volatile关键字.如果变量未声明为volatile,则优化代码.但是,当编译器知道这一点时,这个变量永远不会超过3吗?如果是在运行时,那么当编译器知道变量永远不会超过3时?因为如果在执行这部分代码之后要改变变量.请清除我的怀疑
几天前我接受了采访,但仍然在寻找答案.我想了解使用volatile关键字的意义.
找到下面的代码:两种不同的场景.
//project1
//File1.c
int abc;//Global variable
/*And this variable is getting used in some other files too.*/
if(abc == 3) //Say
{
printf("abc == 3");
}
else
{
printf("abc != 3");
}
/*So if or else part will not be optimized
because "abc" can not be predicted,
the value can chage at any point of time */
//Project2
//file1.c
volatile int abc;//Global variable with volatile keyword
/*And this variable is getting used in some other files too.*/
if(abc == …Run Code Online (Sandbox Code Playgroud) 经过大量研究后Google,我找到了以下程序
#include <stdio.h>
int main()
{
int val;
char *a = (char*) 0x1000;
*a = 20;
val = *a;
printf("%d", val);
}
Run Code Online (Sandbox Code Playgroud)
但它正在抛出一个run time错误*a = 20.
那我怎么写和读取特定的内存位置?
请帮我
今天我在那里接受采访,他们问我可以包含.c file一个源文件吗?我说yes.因为几年前我在一些项目中看到了同样的情况.c file.但刚才我也在尝试.
abc.c
#include<stdio.h>
void abc()
{ printf("From ABC() \n"); }
Run Code Online (Sandbox Code Playgroud)
main.c中
#include<stdio.h>
#include "abc.c"
int main()
{ void abc();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
得到错误:
D:\Embedded\...\abc.c :- multiple definition of 'abc'
哪里出错了?
我写了一个abc.h文件(abc.h的主体是{ extern void abc(void); }),并将文件包含在abc.c(注释掉#include abc.c)中.工作得很好.
你好,我有以下代码:
enum {a, b, c, d, ..., z} abc;
int main()
{
int val = 20;
if (val == a || val == b ||val == c||val == d..... || val == z)
{
/*Do something*/
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以跳过OR操作,因为如果有1000个枚举成员,那么我们如何才能提前检查所有成员.请帮忙.
我们举一个例子,
class base{
public:
virtual void abstract() = 0;
};
class derived:public base{
public:
void abstract(){cout << "Abstract\n";}
};
int main{
derived d;
d.abstract();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它可以用其他方式编写,
class base{
public:
void abstract(){cout << "Abstract\n";}
};
int main{
base b;
b.abstract();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它也提供相同的结果,事实上我不需要派生类.我确实阅读了许多关于抽象类的文章,it says we can not instantiate base class并pure virtual function强迫用户使用define the function.但是如果我们在两种情况下都会看到上面的代码,那么我得到相同的结果(or output).那么我的问题是abstract课程如何帮助我们?
在下面的程序中,我重载了commaoperator.但是,为什么comma operator不考虑这个问题first element/object.
class Point {
int x, y;
public:
Point() {}
Point(int px, int py)
{x = px;y = py;}
void show() {
cout << x << " ";
cout << y << "\n";
}
Point operator+(Point op2);
Point operator,(Point op2);
};
// overload comma for Point
Point Point::operator,(Point op2)
{
Point temp;
temp.x = op2.x;
temp.y = op2.y;
cout << op2.x << " " << op2.y << "\n";
return temp;
}
// Overload + …Run Code Online (Sandbox Code Playgroud) 我有一个要求,我需要使用printf并cout显示数据console and file.因为printf我已经做到了,但因为cout我在挣扎,怎么办呢?
#ifdef _MSC_VER
#define GWEN_FNULL "NUL"
#define va_copy(d,s) ((d) = (s))
#else
#define GWEN_FNULL "/dev/null"
#endif
#include <iostream>
#include <fstream>
using namespace std;
void printf (FILE * outfile, const char * format, ...)
{
va_list ap1, ap2;
int i = 5;
va_start(ap1, format);
va_copy(ap2, ap1);
vprintf(format, ap1);
vfprintf(outfile, format, ap2);
va_end(ap2);
va_end(ap1);
}
/* void COUT(const char* fmt, ...)
{
ofstream out("output-file.txt");
std::cout << "Cout to file";
out …Run Code Online (Sandbox Code Playgroud) 我被问到一个问题,一个班级有,multiple constructors但为什么它只有one destructor?
我举了下面的例子,
class abc
{
public:
int a;
abc()
{
cout << "Default\n";
}
abc(int)
{
cout << "Int\n";
}
~abc()
{
cout << "Destructor\n";
}
};
int main()
{
abc ab;
abc a(5);
}
Run Code Online (Sandbox Code Playgroud)
我在abc之前解释过(5); 被调用的析构函数将被调用,因此在特定时间点只会有一个对象.我现在在我的电脑上运行上面的代码,但它给了我输出
Default
Int
Destructor
Destructor
Run Code Online (Sandbox Code Playgroud)
如果是这样,那么我们为什么one destructor呢?
c ×6
c++ ×6
codeblocks ×1
compile-time ×1
embedded ×1
enums ×1
include ×1
optimization ×1
runtime ×1
volatile ×1
windows ×1