如何在没有'main'的情况下编写C程序?

时间:2011-06-02 17:38:08

标签: c assembly

如何在不使用Main的情况下编写C程序......! 当我正在学习如何通过一个简单的C文件(长度为3行)编写ASM文件时,我对此产生了怀疑。 我在汇编文件中使用了序言和po​​st ambles,在函数中。

3 个答案:

答案 0 :(得分:3)

有一篇很棒的文章并创建了尽可能小的精灵二进制文件here。它有很多关于os可以运行的东西的信息。

答案 1 :(得分:3)

这是合乎逻辑的伎俩。那些不知道它的人可以学习这个技巧

#include<stdio.h>
#include<conio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
void begin()
{
    clrscr();
    printf("\nHello !!! Kaushal Patel.");
    getch();
}

说明:
该  预处理器指令#define with arguments用于给出印象 该程序在没有main()的情况下运行。但实际上它运行的是 hidden main()。

'##' operator被称为令牌粘贴或令牌合并运算符。那是 如何,我可以合并两个或更多字符。

#define decode(s,t,u,m,p,e,d) m##s##u##t

decode(s,t,u,m,p,e,d)正在扩展为“msut”(##运算符 合并m,s,u&amp;进入msut)。逻辑是我通过的时候 (s,t,u,m,p,e,d)作为参数合并第4,第1,第3和第4版。第二个 字符。

#define begin decode(a,n,i,m,a,t,e)

在这里 预处理器用扩展替换宏“begin” 解码(A,N,I,M,A,T,E)。根据前面的宏定义  行必须扩展参数,以便第4,第1,第3&amp;该 必须合并第二个字符。在论证中(a,n,i,m,a,t,e) 4号,1号,3号和4号第二个字符是'm','a','i'和amp; “N”。

因此,在为编译器传递程序之前,预处理器将第三行“void begin”替换为“void main”。

资料来源: http://ctechnotips.blogspot.in/2012/04/writing-c-c-program-without-main.html

答案 2 :(得分:0)

以下是您的回答: - &gt;

#include <stdio.h>

extern void _exit(register int);

int _start(){

printf(“Hello World\n”);

_exit(0);

}
相关问题