鱼C论坛

 找回密码
 立即注册
查看: 2299|回复: 6

[已解决]main函数的位置

[复制链接]
发表于 2021-3-16 09:02:09 | 显示全部楼层 |阅读模式
9鱼币
main函数只能放在固定位置还是可以放在符合语法的任意位置。如果可以的话能不能举几个例子。ps:百度告诉我可以放在任意位置,但是我不知道怎么放!
最佳答案
2021-3-16 09:02:10
可以放在符合语法的任意位置,我比较喜欢放在最后面,这样可以省去声明函数
#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[i] == '\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[i] == '\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[i] == '\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;
}

最佳答案

查看完整内容

可以放在符合语法的任意位置,我比较喜欢放在最后面,这样可以省去声明函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-16 09:02:10 | 显示全部楼层    本楼为最佳答案   
可以放在符合语法的任意位置,我比较喜欢放在最后面,这样可以省去声明函数
#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[i] == '\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[i] == '\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[i] == '\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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-16 09:23:57 | 显示全部楼层
意思就是你不用把main函数放在程序的第一行,在第任意行都可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-16 09:37:57 | 显示全部楼层
chxchxkkk 发表于 2021-3-16 09:23
意思就是你不用把main函数放在程序的第一行,在第任意行都可以

不可以放结尾吗?或者在其他位置吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-16 10:00:00 From FishC Mobile | 显示全部楼层
人造人 发表于 2021-3-16 09:42
可以放在符合语法的任意位置,我比较喜欢放在最后面,这样可以省去声明函数

搭车问一下,您是否用过vscode+gcc
这个组合,我现在在vscode里无法编译
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-16 10:29:21 From FishC Mobile | 显示全部楼层
wp231957 发表于 2021-3-16 10:00
搭车问一下,您是否用过vscode+gcc
这个组合,我现在在vscode里无法编译

什么意思呢?如何无法编辑?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-16 10:40:25 | 显示全部楼层
wp231957 发表于 2021-3-16 10:00
搭车问一下,您是否用过vscode+gcc
这个组合,我现在在vscode里无法编译

之前弄过,详细说明你的问题
qq: 1440332527
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-14 17:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表