#include <stdio.h>
int main()
{
int a=-1?2:5 + 8?4:5;
printf("%d\n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以上程序的输出是2.但为什么呢?请解释
c operators ternary-operator conditional-operator operator-precedence
如果我在C程序中使用未初始化的全局变量,会发生什么?是不确定的行为?
#include <stdio.h>
int i;
int main()
{
while(i < 5)
{
i++;
}
printf("%d\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我写了一个小C程序来反转一个字符串.即便如此,我声明str为
char*str
然后
str = (char*)malloc(20);
str = "this is a test";
,如果我使用,我不会得到SEGFAULT
char str[20] = "this is a test"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char *a, char *b)
{
char temp;
temp = *a;
*a = *b;
*b = temp;
}
char* reverse(char *str)
{
int len = strlen(str);
printf("len = %d \t %s\n", len, str);
int i = 0;
if (len == 0)
return NULL;
for (i=0; i<len/2; i++)
{
swap((str+i), (str+len-1-i));
printf("%s\n", str); …Run Code Online (Sandbox Code Playgroud) 我想明白之间的差别a,并&a当是一个指针.
在以下示例代码中:
int main()
{
int b = 100;
int *a;
a = &b;
printf("%d %d %d", a , &a , *a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,a是给出地址的名称.那是 :
因此,我期待a并且&a当a指针时是相同的.但是在输出中,我得到的前两个值(a和&a)是不同的.
我哪里错了?
我正在制作一个需要访问相机和相机胶卷的通用iOS应用程序.我怎么会这样呢?我还没有代码显示,因为该应用程序主要基于此.
#include<stdio.h>
int main()
{
int const SIZE=5;
int expr;
double value[SIZE]={2.0,4.0,6.0,8.0,10.0};
expr=1|2|3|4;
printf("%f",value[expr]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何评估上述代码?特别声明:
expr=1|2|3|4;
Run Code Online (Sandbox Code Playgroud) 我在我们的一个组织数据文档中浏览,我遇到了以下代码.
struct A {
unsigned short int i:1;
unsigned short int j:1;
unsigned short int k:14;
};
int main(){
A aa;
int n = sizeof(aa);
cout << n;
}
Run Code Online (Sandbox Code Playgroud)
最初我认为大小将是6个字节,因为unsigned short int的大小是2个字节.但上面代码的输出是2个字节(On visual studio 2008).
有一点点的可能性i:1,j:1并k:14使它有点领域或什么?它只是一个猜测,我不是很确定.有人可以帮我吗?
任何人都可以解释我为什么会收到错误
无法将int**转换为argument1的int*
我已经看到所有堆栈溢出的答案,但没有找到解决我的问题的方法.我的代码
#include<stdio.h>
int* sum(int,int*);
int main()
{
int a=5;
int *b;
*b=6;
int *res;
res=sum(a,&b);
printf("\n%d\n%d\n",a,*b);
printf("%d",*res);
}
int* sum(int x,int *y)
{
x=x+1;
*y=*y+3;
return (&y);
}
Run Code Online (Sandbox Code Playgroud)
这是一个基本问题,但我发现很难解决错误.
我写了一个简单的函数来计算目录中非隐藏文件的数量.但是我注意到,当我++以前增加计数值时,我得到了奇怪的结果,比如负数和非常大的数字.当我切换*count++;到*count = *count + 1;函数行为时,我的行为符合我的预期.有人可以解释这种行为吗?
要使用此示例程序,请将目录路径作为第一个参数传递.
#include <stdio.h>
#include <dirent.h>
int count_files_directory(unsigned int *count, char *dir_path)
{
struct dirent *entry;
DIR *directory;
/* Open the directory. */
directory = opendir(dir_path);
if(directory == NULL)
{
perror("opendir:");
return -1;
}
/* Walk the directory. */
while((entry = readdir(directory)) != NULL)
{
/* Skip hidden files. */
if(entry->d_name[0] == '.')
{
continue;
}
printf("count: %d\n", *count);
/* Increment the file count. */
*count++;
}
/* Close the …Run Code Online (Sandbox Code Playgroud) 我的值不应该是0吗?从x开始
#include<stdio.h>
int main(void)
{
int x = 10,y=20,z=5,i;
i=x<y<z;
printf("%d",i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)