C语言编程的printf [英] c Programming printf

查看:407
本文介绍了C语言编程的printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是这个程序的输出?结果
有人说应该是11 6 5结果
但上开发的编译器它产生0和2垃圾值结果
简单的C程序

 #包括LT&;&stdio.h中GT;
    #包括LT&;&CONIO.H GT;
    无效的主要()
    {
        诠释1 = 5,B = 6,C = 11;
        的printf(%D%D);没有给变量名//打印
        残培();
    }


解决方案

在执行的printf(%D%D); ,printf的预计值%d个。根据你的情况,它不提供。一些垃圾值将从栈服用。

 有人说应该是11 6 5。

由于声明的变量是自动。因此,将在堆栈进行分配。所以他们可能会认为,使用的printf()无参数打印这些变量的值。

编辑:

如何函数执行它的参数是纯粹的具体制度。无论如何,我想从Linux的观点GCC点加一些细节。
如何具体的C函数的工作?的意志给一些想法如何的printf()的作品。

大会code(的printf(),除了格式说明没有参数

  .cfi_def_cfa_register 6
        SUBQ $ 16%,可吸入悬浮粒子
        MOVL $ 5 -12(RBP%)
        MOVL $ 6 -8(%RBP)
        MOVL $ 11 -4(RBP%)
        MOVL $ .LC0,EAX%
        MOVQ%RAX,%RDI
        MOVL $ 0,%EAX
        调用printf
        MOVL $ 0,%EAX
        离开

大会code(的printf()带参数),即的printf(%D%D,A,B,C);

  .cfi_def_cfa_register 6
        SUBQ $ 16%,可吸入悬浮粒子
        MOVL $ 5 -12(RBP%)
        MOVL $ 6 -8(%RBP)
        MOVL $ 11 -4(RBP%)
        MOVL $ .LC0,EAX%
        MOVL -4(RBP%),ECX%寄存器//这里使用,储存args作为
        MOVL -8(%RBP),EDX%// printf()函数。
        MOVL -12(RBP%),ESI%//但是在previous情况下,变量值
        MOVQ%RAX,%RDI //不会移动到寄存器。
        MOVL $ 0,%EAX
        调用printf
        MOVL $ 0,%EAX
        离开

what will be the output of this program?
some people say it should be 11 6 5.
but on Dev compiler it produces 0 and 2 garbage values
simple C program

 #include<stdio.h>   
    #include<conio.h>    
    void main()  
    {  
        int a=5,b=6,c=11;  
        printf("%d %d %d");  //printing without giving variable name  
        getch();  
    }  

解决方案

While executing printf("%d %d %d");, printf expects values for %d. In your case, it is not supplied. some garbage value will be taken from stack.

some people say it should be 11 6 5.

Because the variables declared are auto. so it will be allocated in stack. so they might have think that using printf() without argument will print the values of those variables.

EDIT::

How a function executes its argument is purely system specific. Anyway I wish to add some detail from Linux gcc point of view.
How does the particular C function work? will gives some idea how printf() works.

Assembly code(printf() with no arguments except format specifier)

   .cfi_def_cfa_register 6
        subq    $16, %rsp
        movl    $5, -12(%rbp)
        movl    $6, -8(%rbp)
        movl    $11, -4(%rbp)
        movl    $.LC0, %eax
        movq    %rax, %rdi
        movl    $0, %eax
        call    printf
        movl    $0, %eax
        leave

Assembly code(printf() with arguments) i.e printf("%d %d %d", a,b,c);

.cfi_def_cfa_register 6
        subq    $16, %rsp
        movl    $5, -12(%rbp)
        movl    $6, -8(%rbp)
        movl    $11, -4(%rbp)
        movl    $.LC0, %eax
        movl    -4(%rbp), %ecx  //Registers are used here, for storing args for  
        movl    -8(%rbp), %edx  //printf() function.
        movl    -12(%rbp), %esi //But in previous case, values of variables are 
        movq    %rax, %rdi      //not moved to registers.
        movl    $0, %eax
        call    printf
        movl    $0, %eax
        leave

这篇关于C语言编程的printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆