我刚刚开始学习C并想问一些非常基本的问题.我根本没有任何编程背景.我只是在教程的开头(http://www.cprogramming.com/tutorial/c-tutorial.html),并且有些事情没有解释.例如,在下面的介绍性简单程序中......
问题:为什么他使用"%d" - 为什么d而不是另一封信?我理解%是模数操作数,意思是余数,但为什么d?据我所知,stdio.h导入了一个要使用的术语库/术语表,比如printf等......是不是和%d相同?:
#include <stdio.h>
int main()
{
int this_is_a_number;
printf( "Please enter a number: " );
scanf( "%d", &this_is_a_number );
printf( "You entered %d", this_is_a_number );
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main()
{
char *str;
gets(str);
puts(str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
output =分段错误
为什么它会给出分段错误?
考虑以下代码段:
void f() {
int arr[10];
arr = malloc(sizeof(int) * 100);
for (int i = 0 ; i < 100 ; i++) {
printf("%d ", arr[i]);
}
puts("");
free(arr);
}
Run Code Online (Sandbox Code Playgroud)
arr[10]当函数f返回时,是否会释放原始堆栈内存?(或者这是堆栈内存泄漏?)
我看到一个教程,解释如何在循环中使用"break"语句,但每次我尝试使用它时,我收到编译错误说:
"break语句不在循环或开关内
break;"
这是我的代码:
if (finalFirstChar > 6 || finalFirstChar < 1)
{
printf("You didn't entered a proper number! \n");
break;
}
Run Code Online (Sandbox Code Playgroud) 下面是我为了解类型转换而编写的一些代码,但我不明白为什么浮点数或双精度值的值打印为"0.000000",即使我从整数数组中输入或尝试从联合的地址解释.
#include <stdio.h>
union test1
{
int x;
float y;
double z;
};
int main()
{
int x[2]={200,300};
float *f;
f=(float*)(x);
printf("%f\n",*f); /*(1)why is this value 0.0*/
*f=(float)(x[0]); /*(2)this works as expected and prints 200.00000*/
printf("%f\n",*f);
union test1 u;
u.x=200;
/*(3)this line give compilation error why*/
//printf ("sizeof(test1) = %d \n", sizeof(test1));
/*(4) this line also prints the value of float and double as 0.0000 why*/
printf ("sizeof(test1) = %lu u.x:%d u.y:%f u.z:%lf \n", sizeof(u), u.x, u.y, u.z);
return 0; …Run Code Online (Sandbox Code Playgroud) 为什么我的代码错误?
int *x ;
x = new int[5];
x[0] = 3;
x[1] = 4;
x[2] = 5;
x[3] = 1;
x[4] = 2;
x[5] = 11;
x[6] = 90;
int *y ;
y = new int[5];
cout << "if no error, then this command should be run" << endl;
Run Code Online (Sandbox Code Playgroud)
但输出是:
进程在0.07883秒后退出,返回值为3221226356
按任意键继续 ...
我有一个与此代码相关的问题.
#include <stdio.h>
void main()
{
char array[0];
scanf("%s", array);
printf("%s", array);
return;
}
Run Code Online (Sandbox Code Playgroud)
当我在输入字段中输入数据时,它会在数组中存储并获得输出,但它的大小为0.
另一件事情是,当我输入假设'a'12次然后以某种方式控制到达scanf()接受输入并再次进入'a'再次12次控制到达scanf()并继续直到我输入少于12个字符.
如果输入超过12个字符,程序崩溃.
为什么会这样?
我想读一个名字并打印出来.但是控件不会等我输入名称并直接打印出一些无意义的字符输出.
#include <stdio.h>
using namespace std;
int main() {
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s", name);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里有什么错误吗?
在编写程序时,我遇到了一个奇怪的情况.我需要知道一个for loop是正常结束还是正常结束break.有没有办法知道这个?