这是我的程序,我总是%在每个程序的输出结束时得到一个标志.例如,在以下程序的输出结束时,我得到:
name1 and name2: You will reach the top.%
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <stdio.h>
#define message_for(a, b) \
printf(#a " and " #b ": You will reach the top.'\0'")
int main() {
message_for(name1, name2);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 有没有办法多次初始化srand?我有一个特定的函数来生成随机数,但每次我调用它都会给出相同的数字。有任何解决这个问题的方法吗?
int whoatk2() {
srand(time(NULL));
int a;
a = rand() % 50;
return a;
}
Run Code Online (Sandbox Code Playgroud) 我想double动态声明一个类型数组,所以这是我的代码
void function(int length, ...)
{
...
double *a = malloc(sizeof(double) * length);
memset(a, 1, sizeof(double) * length);
for (int i = 0; i < length; i++)
{
printf("%f", a[i]);
}
...
}
Run Code Online (Sandbox Code Playgroud)
当我通过length时2,代码不会打印所有1.它只打印以下内容:
7.7486e-304
7.7486e-304
Run Code Online (Sandbox Code Playgroud)
那么,我该怎么做才能修复它?
这是我的代码,输出的每个字母都打印两次.它不是文本文件,它只在我插入我的putchar(tolower)语句时才会发生,但它的格式应该完全正确.这个陈述有什么问题?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_STRING_SIZE 20
#define MAX_LIST_SIZE 50
int readFile(char *filename); /* function declaration for readFile, defined below */
void punct();
/* main function */
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("%s: usage %s textFileName \n", argv[0], argv[0]);
exit(1);
}
readFile(argv[1]);
return 0;
}
int readFile(char *filename) {
char ch;
FILE *fPtr;
fPtr = fopen(filename, "r"); /*open file filename, r is read only */
if (!fPtr) {
return …Run Code Online (Sandbox Code Playgroud) 我只是在阅读"C接口和实现".书中描述了一些非常有趣的概念.有时(在我看来)代码非常难看,但现在我有一个关于将整数/长转换为字符串(char数组)的问题.书中描述的是:
const char *Atom_int(long n) {
char str[43];
char *s = str + sizeof str;
unsigned long m;
if (n == LONG_MIN)
m = LONG_MAX + 1UL;
else if (n < 0)
m = -n;
else
m = n;
do
*--s = m%10 + '0';
while ((m /= 10) > 0);
if (n < 0)
*--s = '-';
return Atom_new(s, str + sizeof str - s);
}
Run Code Online (Sandbox Code Playgroud)
因为没有描述为什么使用这个函数的方式......我想知道为什么它不仅仅是简单的东西:
const char *Atom_int(long n)
{
char str[43];
char *s = str;
sprintf(str, …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main() {
int x = 9;
int y = 2;
int z = x - (x / y) * y;
printf("%d", z);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码会打印出值x % y?
从严格的数学角度来看,(x/y)*y是相同的x,所以如果以这种方式观察,人们可能会期望打印0.
在这个特定的代码中,我无法看到它是如何输出的1.控件到达函数返回int类型的末尾而不会遇到return语句,因此不应该有运行时错误.我尝试将其他一些值传递给函数,所有都打印相同的值,1因为b之前结果是奇数0.
我试着搜索int返回函数的默认返回值,但无法得到满意的答案.有人说,默认的返回值是0,那么为什么1是这里的输出?
int fun(int a, int b) {
if (b == 0) {
return 0;
}
if (b % 2 == 0) {
return fun(a + a, b / 2);
}
}
int main() {
printf("%d", fun(6, 3));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我的示例代码.
#include <stdio.h>
#include <string.h>
int main() {
char a[3] = { 'H', 'E', 'L', 'L', 'O', '\0' };
printf("Length is %zd ", strlen(a));
}
Run Code Online (Sandbox Code Playgroud)
我知道这会产生以下警告!
test.c:5:26: warning: excess elements in array initializer
char a[3] = {'H','E','L','L','O','\0'};
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,如果我指定大小为a[6]或大于我的输出的实际大小.
对于 char a[100] = {'H','E','L','L','O','\0'};
输出:
Length is 5
Run Code Online (Sandbox Code Playgroud)
对于 char a[10] = {'H','E','L','L','O','\0'};
输出:
Length is 5
Run Code Online (Sandbox Code Playgroud)
对于任何等于或大于数组大小的东西,我都得到正确的输出.
但是当我给出比实际尺寸更小的东西时,我总是得到 6作为输出.
为char a[5] = {'H','E','L','L','O','\0'};或为a[4]或a[3]或或a[2]它总是
Length is 6
虽然为 …
所以,我对 C 完全陌生。我正在测试这些语言的性能。
我做了什么?
我用 C 编写了两个程序,一个是用 Java 编写的,它们都做同样的事情,但是与 Java 相比,C 非常慢?
以下是程序:
主文件:
#include <stdio.h>
#include <time.h>
#include <conio.h>
void work();
int main() {
time_t seconds;
seconds = time(NULL);
printf("time1 : %ld\n", seconds);
work();
seconds = time(NULL);
printf("time2 : %ld\n", seconds);
return 0;
}
void work() {
int a[1000][500];
for (int k = 0; k < 10000; ++k) {
for (int i = 0; i < 1000; ++i) {
for (int j = 0; j < 500; ++j) …Run Code Online (Sandbox Code Playgroud) int FindingMoves(int input1) {
int result = 0;
int input2 = input1 + 1;
result = (2 * input2 + 1) * (2 * input2 + 1);
return result;
}
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能优化上述C程序,这必须被认为是有效的?
我应该使用另一种编程语言来获得更好的结果吗?Java8,C++?