编写求n!函数,并编写main()函数shell调用main函数该函数

4、 编写一个求1--n内的偶数和的函数,在main()函数中输入n,然后调用该函数求1--n的偶数和并输出_百度知道
4、 编写一个求1--n内的偶数和的函数,在main()函数中输入n,然后调用该函数求1--n的偶数和并输出
WriteLine(&;using S
int total = 0.ReadLine())C#代码using S i &= key.ToInt32(Console,
Console.Linq,total).C i++)
if (i % 2 == 0)
total = total +namespace test{
class Program
static void Main(string[] args)
int key = Cusing S{0}以内所有偶数和是{1}&quot.T
for (int i = 0.Gusing System
有没有C语言的
网上搜了个,你试试#include &stdio.h&void main(){int sun.i,n;scanf(&%d&,&n)sum=0;for(i=0;i&n;i+=2)sum=sum+i;printf(&%d&,sum);}
其他类似问题
为您推荐:
其他3条回答
n&; printf(& }
i &n&,sum(7)); printf(&int sum(int n){ int s = 0; i+=2) {
s += i,sum(6));.h& (n+1); printf(&;n&n&quot,sum(1));%d\}void main(){ printf(&%d\ for (int i = 2#include&%d&#92,sum(8));%d&#92
测试通过#include &iostream&using std::using std::using std::int count(int n){ int sum=0; for(int i=1;i&=n;++i) {
if(i%2==0)
//为偶数,则累加
sum+=i;; }
//sum即为和}int main(){
cout && &input a number: & && cin && cout && count(n) && return 0;}有问题请百度hi
int f(int n){ int s=0; for(int i=1;i&=n;i++) {
if(i%2==0)
s+=i; } }其实很简单的
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C语言中没有main函数生成可执行程序的几种方法_Linux编程_Linux公社-Linux系统门户网站
你好,游客
C语言中没有main函数生成可执行程序的几种方法
来源:Linux社区&
作者:justlinux2010
1、define预处理指令这种方式很简单,只是简单地将main字符串用宏来代替,或者使用##拼接字符串。示例程序如下:#include &stdio.h&
#define begin main
int begin(void){printf("Hello, World!\n");return 0;}
#include &stdio.h&
#define begin m##a##i##n
int begin(void){printf("Hello, World!\n");return 0;}严格来说,这种方式只算是一种技巧......2、_start函数_start函数是C程序的入口函数,会调用main函数。在调用main函数之前,会先执行_start函数分配必要的资源,然后再调用main函数。但是在用gcc编译程序时可以使用-nostartfiles选项来重写_start函数。示例程序如下:#include &stdio.h&#include &stdlib.h&
_start(void) {printf("Hello, World!\n");exit(0);}编译上面的程序的命令为:gcc -nostartfiles _start.c -o a.out反汇编生成的可执行程序,如下所示:a.out: file format elf64-x86-64
Disassembly of section .plt:
0320 &puts@plt-0x10&:400320: ff 35 ea 01 20 00 pushq 0x2001ea(%rip) # 600510 &_GLOBAL_OFFSET_TABLE_+0x8&400326: ff 25 ec 01 20 00 jmpq *0x2001ec(%rip) # 600518 &_GLOBAL_OFFSET_TABLE_+0x10&40032c: 0f 1f 40 00 nopl 0x0(%rax)
0330 &puts@plt&:400330: ff 25 ea 01 20 00 jmpq *0x2001ea(%rip) # 600520 &_GLOBAL_OFFSET_TABLE_+0x18& 00 00 00 00 pushq $0x040033b: e9 e0 ff ff ff jmpq 400320 &puts@plt-0x10&
0340 &exit@plt&:400340: ff 25 e2 01 20 00 jmpq *0x2001e2(%rip) # 600528 &_GLOBAL_OFFSET_TABLE_+0x20& 01 00 00 00 pushq $0x140034b: e9 d0 ff ff ff jmpq 400320 &puts@plt-0x10&
Disassembly of section .text:
0350 &_start&: push %rbp 89 e5 mov %rsp,%rbp400354: bf 68 03 40 00 mov $0x400368,%edi400359: e8 d2 ff ff ff callq 400330 &puts@plt&40035e: bf 00 00 00 00 mov $0x0,%edi400363: e8 d8 ff ff ff callq 400340 exit@plt 上面的结果是完整的反汇编结果,我们可以看到_start函数中只有我们调用printf和exit函数相关的一些指令,并且.txt段中只有_start函数,没有看到main函数。如果将源代码中的_start替换为main,重新编译程序,反汇编的结果中会看到_start函数会调用到main。另外还有一点需要注意,因为这里重写了_start函数,所以gcc为默认的main函数准备的清理动作就没用上,所以如果退出的时候直接使用return,会导致程序崩溃。所以这里要使用exit()来退出程序。具体的原因可以参见这篇文章。3、gcc的-e选项示例程序如下:#include &stdio.h&#include &stdlib.h&
int nomain(int i, int j, int k) {printf("Hello, World!\n");exit(0);}将上面的程序保存为m.c,编译命令如下所示:gcc -nostartfiles -e nomain m.c -o a.out继续使用objdump反汇编生成的可执行程序,结果如下:a.out: file format elf64-x86-64
Disassembly of section .plt:
0320 &puts@plt-0x10&:400320: ff 35 f2 01 20 00 pushq 0x2001f2(%rip) # 600518 &_GLOBAL_OFFSET_TABLE_+0x8&400326: ff 25 f4 01 20 00 jmpq *0x2001f4(%rip) # 600520 &_GLOBAL_OFFSET_TABLE_+0x10&40032c: 0f 1f 40 00 nopl 0x0(%rax)
0330 &puts@plt&:400330: ff 25 f2 01 20 00 jmpq *0x2001f2(%rip) # 600528 &_GLOBAL_OFFSET_TABLE_+0x18& 00 00 00 00 pushq $0x040033b: e9 e0 ff ff ff jmpq 400320 &puts@plt-0x10&
0340 &exit@plt&:400340: ff 25 ea 01 20 00 jmpq *0x2001ea(%rip) # 600530 &_GLOBAL_OFFSET_TABLE_+0x20& 01 00 00 00 pushq $0x140034b: e9 d0 ff ff ff jmpq 400320 &puts@plt-0x10&
Disassembly of section .text:
0350 &nomain&: push %rbp 89 e5 mov %rsp,%rbp 83 ec 10 sub $0x10,%rsp 7d fc mov %edi,-0x4(%rbp)40035b: 89 75 f8 mov %esi,-0x8(%rbp)40035e: 89 55 f4 mov %edx,-0xc(%rbp)400361: bf 75 03 40 00 mov $0x400375,%edi400366: e8 c5 ff ff ff callq 400330 &puts@plt&40036b: bf 00 00 00 00 mov $0x0,%edi400370: e8 cb ff ff ff callq 400340 &exit@plt&从上面我们可以看到指定的nomain函数位于.text段的开始位置,同样在函数结束的时候没有gcc为main函数准备的清理动作,所以在这里也只能使用exit()来退出程序,而不能使用return。4、nostartfiles选项前面已经多次使用了该选项,不过都是配合其他选项使用的,这个选项也可以单独使用,其含义为"Do not use the standard system startup files when linking"。示例程序如下:#include &stdio.h&#include &stdlib.h&
void func() {printf("I am func....\n");}
int nomain1(int i, int j, int k) {func();printf("%s: Hello, World!\n", __func__);exit(0);}上面的程序保存为k.c,然后使用下面的命令编译:[root@_190 ~]# gcc -nostartfiles p.c/usr/bin/ld: warning: cannot find entry symbol _ defaulting to 0398在单独使用nostartfiles选项时会报警告,生成的可执行程序可以执行,但是会产生段错误,去掉对func()函数的调用就不会产生段错误了。将生成的可执行程序反汇编,和使用前面的方法生成可执行程序的反汇编结果比较,发现除了函数名不一样外,没有其他区别,不知道为什么会产生段错误。知道的麻烦告知一声,拜谢!
相关阅读:
C语言变长数组之剖析
C语言需要注意的问题
C语言位域的使用及其注意点
相关资讯 & & &
& (07/25/:51)
& (03/09/:10)
& (03/15/:43)
& (01/02/:15)
图片资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款标准答案:暂无
答案解析:double fun(int n)
double s=0.0;
for (i=1; i<=n; i++)
s=s+(1.0/(2*i-1)-1.0/(2*i)); /*计算S= (1-1/2) + (1/3-1/4) +…+ (1/(2n-1)-l/2n) */
[解析] 本题中s=s+(1.0/(2*i-1)-1,O/(2*i));语句是用C程序去表达题目中的每一项,这是关键,其他问题不难理解。
本题选自:
leonh** 回复
20584**:这道题目如果选择B其实也是正确的,根据我国统计局的数据来做的报告,是不靠谱的
leonh** 回复
20584**:这道题目如果选择B其实也是正确的,根据我国统计局的数据来做的报告,是不靠谱的
相关试题:
如需更多试题请下载
相关考试软件
维普考试系列软件是由维普公司研发并推广的一系列自助考试辅导软件,该软件诣在帮助参与各类考试的考生通过考试软件自助进行模拟考试,自我检测和复习考题,本考试软件包含海量的试题试卷,供考生选用,软件安装简便,使用效率高,是参加公共基础知识考试考生的理想选择...
相关考试资讯}

我要回帖

更多关于 shell调用main函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信