我有这个代码来计算一个值的指数幂,这两个都是由用户输入的,例如,用户输入2 ^ 3 = 8,它假设这样工作但是有些错误,最终结果是608,当我在计数器的pwra函数中调试,甚至在计数器启动之前设置结果值,从那里我不知道,因为我没有设置它所以最终结果是608.我觉得它是一个缓冲区问题,但我试过fflush无论是进出,它都不起作用.因此,当我将此代码复制到一个新窗口时,它会工作一段时间,然后再次相同,之前它显示624作为最终结果.
#include <stdio.h>
int pwra (int, int);
int main()
{
int number, power, xx;
printf("Enter Number: ");
scanf("%i", &number);
printf("Enter Number: ");
scanf("%i", &power);
xx=pwra (number,power);
printf("Result: %i", xx);
return 0;
}
int pwra (int num, int pwr)
{
int count, result;
for(count=1;count<=pwr;count++)
{
result = result*num;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
另外一件事,我如何计算a的指数值float,因为当我改变所有int到float最终结果时总是0.00000甚至是%lf.
我对C编程并尝试改进尚不陌生。我已经看到了一些与此类似的问题,并尝试使用strncpy实施他们的建议,但仍然无法正常工作。谁能给我正确方向的指针?谢谢。
该代码的目的是给一个字符串,并在找到空白时将其拆分。我故意不使用strtok()。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[10] ;
char word[10] ;
int i, j, k, l ;
int main()
{
printf("Please enter a string of ten characters and I'll find the blanks\n") ;
fgets(str,10,stdin) ;
printf("Original string is %s\n", str) ;
j = 0 ;
for(i == 0 ; i < 10 ; i++){
if(!isblank(str[i])){
strncpy(word[j], &str[i], 1) ;
printf("i = %d, j = %d, str[i] = %c, word[j] = %c\n",i, j, str[i], word[j]) ; …Run Code Online (Sandbox Code Playgroud) 我有以下功能,如果我的值是0,它会给出一些奇怪的输出.有人可以解释一下为什么这表现得很奇怪吗?我的意图是仅在非零值时打印该值,否则应为空白
# include<stdio.h>
int main(int argc, char *argv[]) {
int i=0;
printf("Number is %d\n", i ? i : "");
return 0;
}
-> gcc print.c
print.c: In function 'main':
print.c:5: warning: pointer/integer type mismatch in conditional expression
-> ./a.out
Number is 4195848
Run Code Online (Sandbox Code Playgroud)
我知道我可以像下面这样做,但我想用上面的逻辑做同样的事情
if(i != 0) {
printf("%d", i);
} else {
printf("%s", "");
}
Run Code Online (Sandbox Code Playgroud) 我正在查看示例代码并发现此操作:
displayMap[x + (y/8)*LCD_WIDTH]|= 1 (shift by) shift;
Run Code Online (Sandbox Code Playgroud)
在哪里
byte shift = y % 8;
Run Code Online (Sandbox Code Playgroud)
我理解|操作数,=但是它们两个一起做什么。
这个程序的输出是什么以及如何?
#include<stdio.h>
int main(){
int a=0,b=10;
a=b---
printf("the value of b=%d",b);
printf("the value of a=%d",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这里有一个奇怪的代码:
const double a[] = {0,1,2,3,4};
int main()
{
double *p = a;
printf("%f\n",p[2]); //2.000000
printf("%f\n",p); //2.000000
}
Run Code Online (Sandbox Code Playgroud)
它返回2.000000,为什么?
1| typedef struct container{
2| char* abc;
3| }container;
4|
5|
6| int main(void){
7|
8| container* xyz = malloc(sizeof(container));
9|
10| xyz->abc = malloc(10);
11| xyz->abc = "abcdefghi\0";
12|
13| free(xyz->abc);
14| free(xyz);
15| }
Run Code Online (Sandbox Code Playgroud)
根据Valgrind的说法,第10行有泄漏,这意味着免费(xyz-> abc)不起作用.我怎么能解脱这个?
我有这个代码:
int * m = (int *)malloc(sizeof(int));
printf("%p %i\n", m, *m);
(*m) = 6;
printf("%p %i\n", m, *m);
free(m);
printf("%p %i\n", m, *m);
Run Code Online (Sandbox Code Playgroud)
我在Linux上运行它.为什么内存地址的值始终为0?
为什么我在Windows上运行它会改变这个值?
为什么在此代码中指针转移到另一个位置:
#include <stdio.h>
void f(int *p)
{
int j=2;
p=&j;
printf("%d\n%p\n%d\n",*p,&j,p);
}
int main(void)
{
int *q;
int m=98;
q=&m;
f(q);
printf("%p ",q);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
2
0x7ffff5bf1bcc
0x7ffff5bf1bcc
0x7ffff5bf1bc8
我明白,当函数f()与打印值完成j和地址 j的内存占用j追溯到堆栈,但IMO p应继续指向该位置甚至在功能上与应该打印相同的地址主同样.这有什么问题?
您好我是C的新手,我正在尝试打印我自己设置的字符串,但它会打印垃圾.
我知道id[4]是'\0'这样,我没有设置它.
int main(){
char id[5];
printf("Enter a string\n");
id[0]=1;id[1]=2;id[2]=3;id[3]=4;
printf("You entered the string %s\n",id);
}
Run Code Online (Sandbox Code Playgroud)