main函数的位置
main函数只能放在固定位置还是可以放在符合语法的任意位置。如果可以的话能不能举几个例子。ps:百度告诉我可以放在任意位置,但是我不知道怎么放! 可以放在符合语法的任意位置,我比较喜欢放在最后面,这样可以省去声明函数#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
size_t lines(const char *filename) {
struct stat sb;
if(lstat(filename, &sb) != 0) return 0;
if(sb.st_size == 0) return 0;
size_t count = 1;
int fd = open(filename, O_RDONLY);
char *buf = malloc(sb.st_size);
read(fd, buf, sb.st_size);
for(long i = 0; i < sb.st_size; ++i) {
if(buf == '\n') ++count;
}
free(buf);
close(fd);
return count;
}
const char *suffix(const char *filename) {
return strrchr(filename, '.');
}
size_t count_lines(const char *path) {
size_t count = 0;
DIR *dirp = opendir(path);
if(!dirp) return 0;
struct dirent *de;
char *cwd = malloc(PATH_MAX); getcwd(cwd, PATH_MAX); chdir(path);
while((de = readdir(dirp))) {
if(!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
if(de->d_type == DT_DIR) {
count += count_lines(de->d_name);
}
const char *s = suffix(de->d_name);
if(!s || strcmp(s, ".c")) continue;
count += lines(de->d_name);
}
chdir(cwd); free(cwd);
closedir(dirp);
return count;
}
int main(void) {
printf("%lu\n", count_lines("../"));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
size_t lines(const char *filename);
const char *suffix(const char *filename);
size_t count_lines(const char *path);
int main(void) {
printf("%lu\n", count_lines("../"));
return 0;
}
size_t lines(const char *filename) {
struct stat sb;
if(lstat(filename, &sb) != 0) return 0;
if(sb.st_size == 0) return 0;
size_t count = 1;
int fd = open(filename, O_RDONLY);
char *buf = malloc(sb.st_size);
read(fd, buf, sb.st_size);
for(long i = 0; i < sb.st_size; ++i) {
if(buf == '\n') ++count;
}
free(buf);
close(fd);
return count;
}
const char *suffix(const char *filename) {
return strrchr(filename, '.');
}
size_t count_lines(const char *path) {
size_t count = 0;
DIR *dirp = opendir(path);
if(!dirp) return 0;
struct dirent *de;
char *cwd = malloc(PATH_MAX); getcwd(cwd, PATH_MAX); chdir(path);
while((de = readdir(dirp))) {
if(!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
if(de->d_type == DT_DIR) {
count += count_lines(de->d_name);
}
const char *s = suffix(de->d_name);
if(!s || strcmp(s, ".c")) continue;
count += lines(de->d_name);
}
chdir(cwd); free(cwd);
closedir(dirp);
return count;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
size_t lines(const char *filename) {
struct stat sb;
if(lstat(filename, &sb) != 0) return 0;
if(sb.st_size == 0) return 0;
size_t count = 1;
int fd = open(filename, O_RDONLY);
char *buf = malloc(sb.st_size);
read(fd, buf, sb.st_size);
for(long i = 0; i < sb.st_size; ++i) {
if(buf == '\n') ++count;
}
free(buf);
close(fd);
return count;
}
const char *suffix(const char *filename);
size_t count_lines(const char *path);
int main(void) {
printf("%lu\n", count_lines("../"));
return 0;
}
const char *suffix(const char *filename) {
return strrchr(filename, '.');
}
size_t count_lines(const char *path) {
size_t count = 0;
DIR *dirp = opendir(path);
if(!dirp) return 0;
struct dirent *de;
char *cwd = malloc(PATH_MAX); getcwd(cwd, PATH_MAX); chdir(path);
while((de = readdir(dirp))) {
if(!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
if(de->d_type == DT_DIR) {
count += count_lines(de->d_name);
}
const char *s = suffix(de->d_name);
if(!s || strcmp(s, ".c")) continue;
count += lines(de->d_name);
}
chdir(cwd); free(cwd);
closedir(dirp);
return count;
}
意思就是你不用把main函数放在程序的第一行,在第任意行都可以 chxchxkkk 发表于 2021-3-16 09:23
意思就是你不用把main函数放在程序的第一行,在第任意行都可以
不可以放结尾吗?或者在其他位置吗 人造人 发表于 2021-3-16 09:42
可以放在符合语法的任意位置,我比较喜欢放在最后面,这样可以省去声明函数
搭车问一下,您是否用过vscode+gcc
这个组合,我现在在vscode里无法编译 wp231957 发表于 2021-3-16 10:00
搭车问一下,您是否用过vscode+gcc
这个组合,我现在在vscode里无法编译
什么意思呢?如何无法编辑? wp231957 发表于 2021-3-16 10:00
搭车问一下,您是否用过vscode+gcc
这个组合,我现在在vscode里无法编译
之前弄过,详细说明你的问题
qq: 1440332527
页:
[1]