你能找到以下代码的错误吗?
int main(){
char *p="hai friends",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s %s",p,p1);
}
Run Code Online (Sandbox Code Playgroud)
我预计它将打印空格后跟一个字符串!
#include <stdio.h>
int main()
{
int a,b;
a=-3--25;
b=-3--(-3);
printf("a=%d b=%d\n",a,b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这个程序中,我认为只是很好,但仍然在构建时我得到了错误[ |5|error: expected ';' before numeric constant|.
]我不知道这个程序有什么问题.
我从SEE开始编程抽象(CS106B).我很难开始任务.这是热身练习的代码.我已经咨询了各种不同的解决方案,但没有一个能够工作,因此,我们不得不在这里发布.
#include <iostream>
#include <cstring>
#include "console.h"
using namespace std;
/* Constants */
const int HASH_SEED = 5381; /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33; /* Multiplier for each cycle */
const int HASH_MASK = unsigned(-1) >> 1; /* All 1 bits except the sign */
/* Function prototypes */
int hashCode(string name);
/* Main program to test the hash function */
int main() {
string name = getLine("Please enter your name: ");
int code …
Run Code Online (Sandbox Code Playgroud) 请帮助我使用右对齐的哈希值和空格正确打印高度“n”的金字塔。我已在下面发布了代码本身。该程序正确地要求用户输入,但不会构建右对齐的金字塔。如果有人可以解决这个问题,请帮忙。
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int i=0;
int n=0;
do
{
printf("Height:");
n=GetInt();
} while (n<0 || n>23);
for (i=0; i<n; i++)
{
printf(" ");
for (int x=0; x<i+2; x++)
{
printf("#");
}
printf("\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我的C程序:
#include <stdio.h>
main(){ printf("%f", 5/3); }
Run Code Online (Sandbox Code Playgroud)
我期待输出1.000000
,但我得到的输出是0.000000
.有人可以解释原因吗?
我不明白为什么第一个循环不起作用.即使填充的欠款浮动实际上大于0,它也是一个无限循环.为什么循环不起作用?
#import <cs50.h>
#import <stdio.h>
int main(void)
{
float owed = -1 ;
while (owed < 0)
{
printf("O hai! How much change is owed?\n") ;
float owed = GetFloat() ;
owed = owed * 100 ;
}
int coins = 0 ;
while (owed >= 25)
{
owed = owed - 25 ;
coins = coins + 1 ;
}
while (owed >= 10)
{
owed = owed - 10 ;
coins = coins + 1 ;
}
while …
Run Code Online (Sandbox Code Playgroud) 考虑一个arr[10]
大小为10 的数组.在获取/显示数据时,我们使用以下常见语法的for循环
for(int i=0;i<10;i++)
Run Code Online (Sandbox Code Playgroud)
如果是字符串,就像
for(int i=0;strlen(arr)>i;i++)
Run Code Online (Sandbox Code Playgroud)
但我在某处读过一个更简单的表达式,arr[i]
可以只用在条件的位置.我已经尝试运行具有该条件的代码.但是我收到了一个错误.那么有一个类似的,简单的条件表达式可以用于数组/字符串吗?
下面给出的代码片段的输出结果是什么?
int main()
{
if(0<7<5)
printf("I am printed");
else
printf("I am not printed");
}
Run Code Online (Sandbox Code Playgroud)
我试图通过存储函数的地址来保存它的局部变量。但是,编译器似乎会自动释放该变量的内存,即使在我将其地址存储到指针变量之后也是如此。
我处于一种情况(与下面的代码不完全相同),我必须让类的成员变量存储在函数中声明的变量的地址。
#include <stdio.h>
static int* p;
void foo(){
int a = 5;
p = &a;
}
int main(){
foo();
//some delay
printf("*p = %d\n",*p);
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,*p
应该是 5,但它最终成为一个不是 5 的数字,而且在我重新运行脚本的大部分时间里它是不同的。我相信分配给a
in的内存foo
正在被自动释放。*p
尽管有任何延迟,我如何确保是 5?
当我写
int c = (3+2)*2/5;
Run Code Online (Sandbox Code Playgroud)
我的编程工具将其重写为
int c = (3 + 2) * 2 / 5;
Run Code Online (Sandbox Code Playgroud)
我有一些问题。
仅编译器接受数字和运算符之间的空格吗?数字和操作符之间的输入空间比依赖工具的自动帮助更好?