我对getchar()函数感到困惑,所以我搜索了这个很棒的网站并阅读了所有相关主题,并在K&R书中阅读了关于getchar()的内容.但是当我运行代码时,在控制台屏幕上键入一个单词,然后点击回车,没有任何反应.我期望显示的字符数.
#include<stdio.h>
int main(void)//doesn't work??
{
int c ;
int count ;
while ( ( c = getchar() ) != EOF )
count ++ ;
printf( "%d characters\n" , count ) ;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我读到*(乘法)具有比/(除法)更高的优先级.因此,如果有既与方程*和/,则*必须首先发生.
但我看到一个程序输出一些奇怪的东西
#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
float s=10, u=30, t=2, a;
a = 2*(s-u*t)/SQUARE(t);
printf("Result = %f", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行时,我认为输出为-25,但事实上它是-100.
当我寻找解释时
Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,
// Here SQUARE(t) is replaced by macro to t*t
=> a = 2 * (10 - 30 * 2) / t * t;
=> a = 2 * (10 - 30 * 2) / 2 * 2;
=> …Run Code Online (Sandbox Code Playgroud) 我试图通过updateGUI每隔500毫秒调用一个线程中的函数来更新主窗口.除非我关闭窗口,否则会显示窗口但不会使用新值更新.当我这样做时,会打开一个带有新值的新窗口.我发现了这个问题,但它没有回答我的问题.我知道(如qt文档中所述)
QApplication::exec进入主事件循环并等待直到exit()被调用.
我尝试使用,processEvents()但主窗口反复打开和关闭,非常快,我甚至看不到它.这是我的代码:
float distanceToObject;
bool objectDetected;
Modes currentMode;
void timerStart(std::function<void(void)> func, unsigned int interval)
{
std::thread([func, interval]()
{
while (true)
{
auto x = std::chrono::steady_clock::now() + std::chrono::milliseconds(interval);
func();
std::this_thread::sleep_until(x);
}
}).detach();
}
int updateGUI(void)
{
int argc = 0;
char **argv = NULL;
QApplication a(argc, argv);
MainWindow w;
// Set text of a label
w.setDistance(QString::number(distanceToObject));
// Also update objectDetected and currentMode values
w.show();
//a.processEvents();
return a.exec();
}
void sendMsg(void)
{ …Run Code Online (Sandbox Code Playgroud) 我正在编写此代码以使用带有7个元素的char数组的sizeof()函数,我认为输出应该是8因为7个元素PLUS数组的终结符但是惊讶于输出是5?怎么会?
#include<stdio.h>
int main(void)
{
char str[]="S\065AB";
printf("\n%d",sizeof(str));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想知道指向结构的指针.该代码使用COSMIC编译器编译,并下载到STM8S微控制器.
以下是我在代码中的问题:
typedef struct GPIO_struct
{
volatile unsigned char CR1;
volatile unsigned char CR2;
}
GPIO_TypeDef;
void GPIO_DeInit(GPIO_TypeDef* GPIOx)
{
// This variable will be used for a testing purpose
GPIO_TypeDef vGPIO = (GPIO_TypeDef)*GPIOx; // HERE IS THE PROBLEM
}
int main(void)
{
GPIO_TypeDef GPIOY @0x5000; // Reference to an absolute address
GPIO_DeInit(&GPIOY);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这段代码时,会发生错误:invalid indirection operand.为什么编译器拒绝这段代码?
注意:代码基于STM标准库.
更新:
我知道我可以使用->运算符访问struct成员但我仍然希望将struct成员保存在与结构相同类型的变量中以用于某些测试目的.我只想在这个时间点获得寄存器的快照.
更新2
使用上面发布的代码我得到另一个错误 invalid cast type
当我删除演员表时,它会编译.
但我不明白为什么!! 请有人解释一下
我是电子工程系的学生,去年我们研究过VHDL基础知识,我希望提高我在这个领域和数字设计领域的经验,我想成为这个领域的专家.我怎样才能做到这一点?我在网上搜索了很多,没有找到任何东西.请帮忙.非常感谢.
写下面的代码给我奇怪的答案
#include <stdio.h>
int main (void)
{
char a=0x03 ,b=0x01 ,x;
printf("Enter two numbers : \n");
scanf("%c %c",&a,&b);
printf("0x%x 0x%x\n",a,b);
printf("0x%x || 0x%x = 0x%x \n0x%x ^ 0x%x = 0x%x\n0x%x << 0x%x = 0x%x\n0x%x >> 0x%x = 0x%x\n",a,b,a||b,a,b,a^b,a,b,a<<b,a,b,a>>b);
scanf("%i",x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给了我以下结果
Enter two numbers :
2 3
32 33
0x32 || 0x33 = 0x1
0x32 ^ 0x33 = 0x1
0x32 << 0x33 = 0x1900000
0x32 >> 0x33 = 0x0
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它确实将错误的值带入a和b虽然我已经尝试使用int并且它运行良好?!