我试图弄清楚C中的define命令的这种行为(我是新的).我有这个代码,我不知道为什么我在输出中看到myAge = 15而不是16(我知道它是15,但我不知道为什么).任何人都可以帮我找出它为什么会发生?
这是代码:
#include <stdio.h>
#include <stdlib.h>
#define AGE 15;
int main(void)
{
float myAge = AGE + 1;
printf("Hello!\n");
printf("My name is Raz and I am %d years old!\n", myAge);
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助:)
我有个问题。使用 fgets 函数后,我尝试查看某些字符串的长度。如果我在字符串中可以包含的字母数下输入字符串(例如:字符串中的最大字母为 9,我输入 4 个字母),我会得到字符串的长度+1。为什么?
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char name[10]={0};
printf("enter your name\n");
fgets(name, 10, stdin);
printf("your name is %s and it is %d letters\n", name, strlen(name)); // length problem
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想检查目录中的所有文件时,如果目录中的文件/项目之一是文件夹(另一个目录)
我已经开始的代码(使用 dirent.h):
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
if (ent.is_folder()) // here is what I want to implement
printf ("Folder: %s\n", ent->d_name);
else
printf("File %s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
Run Code Online (Sandbox Code Playgroud)