我在Linux GCC中使用了fflush()但它没有用.该功能有其他替代方案吗?这是我的代码:
#include<stdio.h>
void main()
{
char ch='y';
while(ch=='y')
{
int a;
printf("Enter some value:");
scanf("%d",&a);
fflush(stdin);
printf("Do you want to continue?");
scanf("%c",&ch)
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
Enter some value: 10
Run Code Online (Sandbox Code Playgroud)
然后程序结束.就这样.我在Linux中可以做什么?有替代功能吗?
前缀和后缀增量运算符的必要性是什么?还不够吗?
到目前为止,存在类似的while/do-while必要性问题,然而,将它们两者混淆(在理解和使用方面)并没有那么多.但同时具有前缀和后缀(如这些运算符的优先级,它们的关联,使用,工作).并且有没有人经历过你说过的情况"嘿,我将使用postfix增量.它在这里很有用."
typedef union status
{
int nri;
char cit[2];
}Status;
int main() {
Status s;
s.nri = 1;
printf("%d \n",s.nri);
printf("%d,%d,\n",s.cit[0],s.cit[1]);
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
1
0,1
Run Code Online (Sandbox Code Playgroud)
我知道第二行的输出取决于CPU的结束.我怎么能在平台独立的程序中写这样的?有没有办法检查CPU的endianess?
我问过这个问题,但我没有得到答复.
我使用以下代码打开移动数据(3G).
private static void setMobileDataEnabled(Context context, boolean enabled){
try{
ConnectivityManager conman = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Method setMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(conman, enabled);
}catch(NoSuchMethodException e){e.printStackTrace();}
catch(InvocationTargetException e){e.printStackTrace();}
catch(IllegalAccessException e){e.printStackTrace();}
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
setMobileDataEnabled(getBaseContext(), true/false);
Run Code Online (Sandbox Code Playgroud)
它正确启用/禁用移动数据,但此代码在双SIM卡设备上无法正常工作.我在三星Dual-SIM上的摩托罗拉Razr D1,D3上进行了测试(现在不记得),但这段代码不起作用.一切正常,应用程序不会崩溃.
我试过"getApplicationContext()"和"this"而不是"getBaseContext()",但没有改变.
我了解到Android并非专为双芯片设备设计,这可能是一个问题,因为我无法定位任何SIM卡,所以我无法找到任何技巧或其他任何"修复"代码,我我对吗?
我可以做些什么来在双芯片设备上打开/关闭移动数据?我把在源代码一看,setMobileDataEnabled是"公共",磨片不应该访问它?
我也找到了IConnectivityManager类,但它不是java扩展,我认为它是.aidl或者什么(不记得了),它可以有用吗?
我不知道该怎么办,我需要帮助.
对不起我的英语不好.
谢谢.
我想找到两个索引之间的子字符串。C++ 中的substr (start_index, number_of_characters) 函数根据字符数返回子字符串。因此,将它与开始和结束索引一起使用的小技巧如下:
// extract 'go' from 'iamgoodhere'
string s = "iamgoodhere";
int start = 3, end = 4;
cout<<s.substr(start,end-start+1); // go
Run Code Online (Sandbox Code Playgroud)
C++ 中还有哪些其他方法可以获取两个索引之间的子字符串?
我正面临MISRA C 2012违规,我无法理解.以下是代码:
#define I2C_CCRH_FS ((uint8_t)0x80)
#define I2C_CCRH_DUTY ((uint8_t)0x40)
#define I2C_CCRH_CCR ((uint8_t)0x0F)
typedef struct I2C_struct
{
volatile uint8_t CR1;
volatile uint8_t CR2;
volatile uint8_t CCRL;
volatile uint8_t CCRH;
} I2C_TypeDef;
#define I2C_BaseAddress 0x5210
#define I2C ((I2C_TypeDef *) I2C_BaseAddress)
I2C->CCRH &= ~(uint8_t)((I2C_CCRH_FS | I2C_CCRH_DUTY) | I2C_CCRH_CCR);
Run Code Online (Sandbox Code Playgroud)
在之前的代码中,PC-Lint抱怨说:
Unpermitted operand to operator '|' [MISRA 2012 Rule 10.1, required]
Mismatched essential type categories for binary operand [MISRA 2012 Rule 10.4, required]
Run Code Online (Sandbox Code Playgroud)
规则10.1规定ORing应该没有问题unsigned int.(PC-Lint通过第一次OR操作并抱怨第二次!)
规则10.4规定操作的操作数应具有相同的基本类型.
虽然所有操作数都被声明为,但我无法理解为什么存在这些违规行为uint8_t?
我试过在每两个ORed常量周围加上括号.我也尝试过将它们全部uint8_t …
我试图在int, char, short, long不使用头文件的情况下打印最小值<limit.h>.所以按位操作将是一个不错的选择.但有些奇怪的事发生了
该声明
printf("The minimum of short: %d\n", ~(((unsigned short)~0) >> 1));
Run Code Online (Sandbox Code Playgroud)
给我
The minimum of short: -32768
Run Code Online (Sandbox Code Playgroud)
但声明
printf("The minimum of short: %d\n", ~((~(unsigned short)0) >> 1));
Run Code Online (Sandbox Code Playgroud)
给我
The minimum of short: 0
Run Code Online (Sandbox Code Playgroud)
这种现象也发生在char.但它不会发生在long, int.为什么会这样?
值得一提的是我使用VS Code作为我的编辑器.当我unsigned char在语句中移动光标时
printf("The minimum of char: %d\n", (short)~((~(unsigned char)0) >> 1));
Run Code Online (Sandbox Code Playgroud)
它给了我一个提示,(int) 0而不是(unsigned char)0我所期望的.为什么会这样?
c bit-shift bitwise-operators implicit-conversion visual-studio-code
简单:如果我在 GCC 中测试有符号变量与无符号变量,编译时-Wall我会收到警告。
使用此代码:
#include <stdio.h>
int main(int argc, char* argv[])
{
/* const */ unsigned int i = 0;
if (i != argc)
return 1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到此警告:
<source>: In function 'int main(int, char**)':
<source>:6:8: warning: comparison of integer expressions of different signedness: 'unsigned int' and 'int' [-Wsign-compare]
6 | if (i != argc)
| ~~^~~~~~~
Compiler returned: 0
Run Code Online (Sandbox Code Playgroud)
但是,如果我取消注释const- 编译器很高兴。我几乎可以在每个 GCC 版本上重现这一点(参见https://godbolt.org/z/b6eoc1)。这是 GCC 中的错误吗?
我正在从这里查看一些递归函数:
int get_steps_to_zero(int n)
{
if (n == 0) {
// Base case: we have reached zero
return 0;
} else if (n % 2 == 0) {
// Recursive case 1: we can divide by 2
return 1 + get_steps_to_zero(n / 2);
} else {
// Recursive case 2: we can subtract by 1
return 1 + get_steps_to_zero(n - 1);
}
}
Run Code Online (Sandbox Code Playgroud)
我检查了反汇编,以检查 gcc 是否管理尾部调用优化/展开。看起来确实如此,尽管使用 x86-64 gcc 12.2 -O3 我得到了一个像这样的函数,以两条ret指令结尾:
get_steps_to_zero:
xor eax, eax
test edi, …Run Code Online (Sandbox Code Playgroud)