我只有几天的C编程,所以我不太确定这段代码有什么问题:
#include <stdio.h>
int main(int argc, char * argv[]) {
int sides;
printf("Please enter length of three sides :\n");
scanf("%d", &sides);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息如下:
忽略scanf的返回值
我在这里做错了什么,我该怎么做才能解决它?
我想编写调用另一个exe的c程序.在我调用原始exec之前,这个包装器程序除了设置一些环境变量之外什么都不做.例如,假设我有一个被调用的exe test.exe,我写了testwrapper.exe
我想把它称为testwrapper.exe < parameter >,并在内部它应该调用test.exe < parameter >
我的问题是,当我打电话test.exe的test.exe "c:\program files\input",C与逃脱"作为传递参数
是什么导致了这个问题,我该怎么做才能修复它?
对于更大任务的一部分,我被要求实现一个翻转整数中任意位的函数.问题是"整数"可以是c中的任何默认整数类型,从int8_t到uint64_t,我不知道它将是哪一个.(事实上,我的代码已经在所有这些类型上进行了测试)
这是我对这个问题的尝试:
//NOTE: g_int is the generic integer, it's typedef'd in a .h file
g_int flip_bit(g_int b, uint8_t i){
//Code that makes sure i is a valid amount to shift by, there's a macro
//that defines the upper bound of i in a .h file.
g_int flipped = b ^ (1<<i);
return flipped;
}
Run Code Online (Sandbox Code Playgroud)
此代码异或的i在第i比特b与1,而在其他位b为0.这应当翻转i个比特,同时留下其余部分保持不变.对此感到满意,我在所有这些不同的整数大小上测试了我的代码,然后将其打开.但是,我必须测试不够,因为我的代码在int64_t和uint64_t都失败了.
我对int64_t和uint64_t做了什么错误,我能做些什么来让我的方法工作而不完全改变它?
在最近切换到c之后,我被告知星期天有一千种方法来引用一个尚未初始化的值并不是一种好的做法,并导致出乎意料的行为.具体来说,(因为我以前的语言将整数初始化为0)我被告知在未初始化时整数可能不等于零.所以我决定把它考验一下.
我编写了以下代码来测试这个声明:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
int main(){
size_t counter = 0;
size_t testnum = 2000; //The number of ints to allocate and test.
for(int i = 0; i < testnum; i++){
int* temp = malloc(sizeof(int));
assert(temp != NULL); //Just in case there's no space.
if(*temp == 0) counter++;
}
printf(" %d",counter);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我这样编译它(如果它很重要):
gcc -std=c99 -pedantic name-of-file.c
根据我的教师所说的,我希望temp指向一个随机整数,并且计数器不会经常递增.但是,我的结果将这个假设从水中吹走了:
testnum: || code returns:
2 2
20 20
200 200
2000 2000
20000 20000
200000 …Run Code Online (Sandbox Code Playgroud) 这是我的代码,它为参数指定的大小矩阵分配空间,并返回指向所述矩阵的指针:
int **allocateSpace(int row, int col){
int i;
int **a;
a = malloc(sizeof(int *) * row);
for(i = 0; i < row; i ++)
a[i] = malloc(sizeof(int) * col);
return a;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码按预期工作.
但是,当我将代码更改为以下形式时,我得到一个分段错误:
void allocateSpace(int **a, int row, int col){
int i;
a = malloc(sizeof(int *) * row);
for(i = 0; i < row; i ++)
a[i] = malloc(sizeof(int) * col);
}
Run Code Online (Sandbox Code Playgroud)
似乎从allocateSpace()函数返回时,分配的内存被释放(因为我遇到了分段错误).但为什么?我所做的只是为给定的指针分配内存,而这一切都是在子函数中完成的.
总结一下我的问题:为什么我在第二段代码中出现分段错误?非常感谢你!
我想将一个整数作为参数发送到system()C中的函数,但我无法做到.
我想这样做是因为我有一些常规命名的jpg文件1.jpg , 2.jpg ... 17.jpg ... ect..程序会将一个随机选择的值分配给一个整数变量,并使用该system()函数打开与随机选择的整数同名的图像文件.
我想象的是:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
srand(time(NULL));
i=rand()%30+1; // for example i=17
system("eog %d.jpg &",i); //and i want to open 17.jpg here with eog
}
Run Code Online (Sandbox Code Playgroud)
我知道system()上面的函数有太多的论据; 我只是想举一个我想要的例子.
有没有办法做到这一点,如果没有,我怎么能去做我上面描述的?
我在程序中不断收到以下编译错误.我想编写一个使用数组的程序,该程序p[]被传递给计算第n度多项式的函数(下面设置为5)并返回该值.
我的错误如下:
poly.c:4:39:错误:在数字常量之前预期';',','或')'
poly.c:16:39:错误:在数字常量之前预期';',','或')'
我的节目:
#include <stdio.h>
#define N 5
double eval(double p[], double x, int N)
int main()
{
double p[N+1] = {0,1,2,3,4};
double x;
printf("what value of x would you like?: ");
scanf("%lf", &x);
p[N+1] = eval(p[], x, n);
printf("%lf", p[N+1]);
}
double eval(double p[], double x, int N)
{
double y;
y = x^(p[N+1]);
return y;
}
Run Code Online (Sandbox Code Playgroud) 我是C的新手,我遇到了一个我无法理解的分段错误.我有以下程序,它试图计算严格正数的因子数:
#include <stdio.h>
#include <math.h>
int numberOfFactors (int number, int factor) {
if (number % factor == 0) {
number = number/factor;
return numberOfFactors(number, factor) + 1;
} else {
return 0;
}
}
int check (int x) {
if (x>0) {
return 1;
} else {
return 0;
}
}
int main(void) {
int number;
printf("Please enter a positive integer n such that n >= 1: ");
scanf("%d", &number);
if (check(number)){
int i;
for (i=1; i<=number; i++) {
int …Run Code Online (Sandbox Code Playgroud)