我试图在三个条件下生成三个随机数,并且这三个数字必须是从 0 到 100:
这是我的代码:
#include <stdio.h>
#include <time.h>
int main (void) {
int num1 = 0, num2 = 0, num3 = 0;
srand(time(NULL));
num1 = rand() % 100;
while (num1 % 2 != 0) {
num1 = rand() % 100;
}
num2 = rand() % 100;
while (num2 % 2 == 0) {
num2 = rand() % 100;
}
num3 = rand() % 100;
while (num3 > 50) {
num3 = rand() % 100; …Run Code Online (Sandbox Code Playgroud) 我写了这段代码:
#include <stdio.h>
int main() {
printf("Works");
int base = 1;
do {
if (base > 1) {
for (int i = 0; i <= base; i++) {
if ((base % i) == 0) {
break;
} else {
printf("%d ", base);
}
}
}
base += 1;
} while (1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译完美,但是当我运行它时,终端刚刚关闭,我不知道如何调试它,因为我对用 c 编写程序非常陌生。
编辑:这应该永远生成素数。
大数字的变量溢出是否有任何限制?一本关于阶乘的 C 书中有一个练习。
我的代码:
#include <stdio.h>
int main(void) {
unsigned int n, fn, counter;
//puts("enter nonnegative int");
//scanf("%u", &n);
n = 1;
while (n != 34) {
fn = n;
counter = n - 1;
while (counter > 0) {
fn *= counter;
counter--;
}
printf("%u\n", fn);
n++;
}
}
Run Code Online (Sandbox Code Playgroud)
注释行用于调试。
此代码打印从 n(1) 到 34 ( 'while(n!=34)')的数字的阶乘,但如果将其增加到 36 之类的值,它将在前 34 个输出后仅打印 0。我知道大部分输出都溢出了,并且对这些大数字的控制非常糟糕。
但是,我想知道导致这些零发生的限制是什么。
计算两个给定整数值的总和。如果两个值相同,则返回其总和的三倍。
int main() {
printf("%d\n", test(1, 2));
printf("%d", test(2, 2));
}
int test(int x, int y) {
int sum;
if (x == y) {
sum = (x + y) * 3;
printf("sum = %d\n", sum);
} else {
sum = (x + y);
printf("sum = %d", sum);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行这段代码时,我得到了错误的输出,但是当我使用return语句而不是 时printf,我得到了正确的输入。为什么?
我试图使用for带有 ASCII 表的循环,通过用 32 减去字母数字来使字符串中的每个字符一一大写。但我不能i在 charstr和中使用 int str2。我怎样才能做到这一点?
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define STRLEN 200
void string_lower() {
}
void string_upper(char str) {
char str2;
int length = strlen(str);
for (int i = 0; i < length; i++) {
str2[i] = str[i - 32];
}
}
int main() {
char word[STRLEN] = { 0 };
char word1 = 97;
printf("Write a word");
fgets(word, STRLEN, stdin);
string_upper(word);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我收到以下代码的 MISRA C:2012 Rule-17.7 违规。我对 C 代码和 MISRA 概念非常陌生。任何建议将不胜感激。
5277: Dem_SetEventStatus(Rte_PDAV_DemEvent_DtcC00100_1, EventStatus);
^
Msg(7:3200) 'Dem_SetEventStatus' returns a value which is not being used.
MISRA C:2012 Rule-17.7
Run Code Online (Sandbox Code Playgroud) 问题如下:
使用以写入 0 结尾的 for 循环,计算偶数之和、奇数之和,
我不知道为什么我的代码不起作用:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i = 0, num[i], sum, even, odd;
for (;;) {
printf("Write a number: ");
scanf("%d", &num[i]);
if (num[i] == 0) {
break;
}
if (num[i] % 2 == 0) {
even += num[i];
} else
odd += num[i];
i++;
}
printf("Sum of even is: %d\n", even);
printf("Sum of odd is: %d", odd);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在不同的 C 标准中应该打印什么 C 代码?
struct X { int a; char b[4]; };
struct X x, y; struct X *px;
printf("%x %x\n", x, &x); // what mean x in this context? suppose we get 0x12b45 and 0x34235
printf("%d\n", x == 0x12b45 ? 1 : 0)
px = &x;
x = *px;
printf("%x %x\n", x, *px);
x = y
printf("%x %x\n", x, y);
Run Code Online (Sandbox Code Playgroud)
换句话说,结构体的价值是什么?或者更好地说,结构解析为什么值?使用数组进行类比,数组名称解析为第一个数组项的值,什么值解析为结构名称?
int a[3];
printf("%x %x\n", a, a[0]); // prints the same value twice
struct s { int a; …Run Code Online (Sandbox Code Playgroud) 我正在尝试接受用户的输入并检查它是否等于某个选项('+',' - ','*','x')
do {
printf("Select the op.: ");
scanf("%c", &option);
} while (option != '+' || option != '-' || option != '*' || option != 'x');
printf("%c", option);
Run Code Online (Sandbox Code Playgroud)
这是输出:
Select the op.:Select the op.:Select the op.:Select the op.:
Run Code Online (Sandbox Code Playgroud)
正如您所看到的那样,printf("Select the op.: ")多次执行并且我无法理解原因,因此如果我尝试插入,例如+,这是打印输出:
++
Run Code Online (Sandbox Code Playgroud)
提前致谢
我有这段代码:
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#define DATA_SIZE 25
#define LINE 10
#define BYTES_A_ROW ((int)10)
#define ADDR_COLUMN_WIDTH ((int)(2 * sizeof(void *)) - 2) //char size of an address =$
#define BYTE_COLUMN_WIDTH ((int)(BYTES_A_ROW * 3) - 1) // 1 bytes is 2 hex chars + s$
#define COLUMN_SEPARATOR " "
int main() {
typedef unsigned char byte_t;
byte_t data[DATA_SIZE];
int i;
int j;
int remainder = DATA_SIZE % LINE;
// print table header
printf("\n%-*s", ADDR_COLUMN_WIDTH, "Address");
printf("%s", COLUMN_SEPARATOR);
printf("%-*s", BYTE_COLUMN_WIDTH, …Run Code Online (Sandbox Code Playgroud)