當前位置:首頁 » 操作系統 » printf源碼

printf源碼

發布時間: 2022-05-12 13:07:19

⑴ 求c語言中的庫函數的源代碼 如printf()函數,我要它的源代碼

在stdio.h中。如果是數學函數如sin()等的,在math.h中。而string類的函數則在string.h中。自己看吧

⑵ printf函數的實現方式

其實printf和getchar()類似,它們都是一個」外殼「,真正實現功能的不是它本身,而是通過調用別的函數。
getchar() is equivalent to getc(stdin).
printf有一家子print函數

printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - formatted output conversion
它們的聲明在不同的header file裡面

#include <stdio.h>

int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

#include <stdarg.h>

int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
snprintf(), vsnprintf():
這兩個函數是C99新加的,編譯的時候 注意 -std=c99

實現之前還是「復習」一下printf比較好,就當是鋪墊
有意思的是printf的declaration。
int printf(const char *format, ...);
返回值是int,第一個參數是const字元串指針,第二個參數是個...
先看看返回值int有哪些情況
Return value
Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).

嗯哼。。。返回的是成功列印的字元的個數,這里不包括NULL
demo:
#include<stdio.h>

int main()
{
int counter = 0;

counter = printf("hello world! %d\n",10);

printf("counter is %d\n",counter);

return 0;
}
jasonleaster@ubuntu:~$ ./a.out
hello world! 10
counter is 16

接著,第一個參數是一個指針,指向const字元串
Format of the format string
The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of
zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications,
each of which results in fetching zero or more subsequent arguments. Each conversion specification is introced by the character %,
and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width,
an optional precision and an optional length modifier.

很少人會用下面這種用法
printf("%*d",10,5);
我第一次遇到的時候,可以說是「驚愕」,究竟會列印什麼東西。折騰了好久,最後搞定了。總結在這里
http://blog.csdn.net/cinmyheart/article/details/10116359

Format of the format string

The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, an optional precision and an optional length modifier.
The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each '*' and each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given). One can also specify explicitly which argument is taken, at each place where an argument is required, by writing "%m$" instead of '%' and "*m$" instead of '*', where the decimal integer m denotes the position in the argument list of the
desired argument, indexed starting from 1. Thus,
printf("%*d", width, num);
and
printf("%2$*1$d", width, num);
are equivalent. The second style allows repeated references to the same argument. The C99 standard does not include the style using '$', which comes from the Single UNIX Specification. If the style using '$' is used, it must be used throughout for all conversions taking an argument and all width and precision arguments, but it may be mixed with "%%" formats which do not consume an argument.
There may be no gaps in the numbers of arguments specified using '$'; for example, if arguments 1 and 3 are specified, argument 2
must also be specified somewhere in the format string.

第三個參數 ...
嗯,這傢伙有點屌,叫做變長參數。把這個搞定,C總會有點長進的
這個stdarg.h 我在現在的GCC和現在的linux 3.0版本的內核裡面找了好久,都木有,估計是封裝到被的地方了。。。。
__builtin_va_start(v,l) 線索就死在這個地方。。。之後就找不到__builtin_va_start的定義了

還是看早起內核的實現吧
0.12內核裡面的stdarg.h
#ifndef _STDARG_H
#define _STDARG_H

typedef char *va_list;

/* Amount of space required in an argument list for an arg of type TYPE.
TYPE may alternatively be an expression whose type is used. */

#define __va_rounded_size(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))

#ifndef __sparc__
#define va_start(AP, LASTARG) \
(AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
#else
#define va_start(AP, LASTARG) \
(__builtin_saveregs (), \
AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
#endif

void va_end (va_list); /* Defined in gnulib */
#define va_end(AP)

#define va_arg(AP, TYPE) \
(AP += __va_rounded_size (TYPE), \
*((TYPE *) (AP - __va_rounded_size (TYPE))))

#endif /* _STDARG_H */

va_list 是一個指向字元串的指針
分析上面的宏定義#define __va_rounded_size(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
這個用來得到TYPE元素類型的位元組大小,若不足4位元組(例如short 和char),那麼認為這個元素的大小為4位元組,簡單的說就是檢測元素的大小,不足4位元組的當作4位元組看待。。。#define va_start(AP, LASTARG) \
(AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))

AP一般都是va_list,LASTARG則是 指向參數變長函數的格式化字元串的指針.
va_start的作用就很明顯了。取得變長參數列表的第一個參數的地址。
va_end 則是把指針va_list 置0 (通過google知道的,這個va_end真沒找到定義,代碼裡面就一句#define 我無能為力啊。。。)
不過知道用va_start 和va_end 就OK啦
下面先來個變長參數的demo
/*****************************************************************************
code writer : EOF
code date : 2014.04.26
e-mail:[email protected]
code purpose:
just a demo for varible parameter function.

usage: va_sum(a number,anohter number...,0);
va_sum(1,2,3,4,5,0); return 15

******************************************************************************/

#include <stdarg.h>
#include <stdio.h>

int va_sum(int* a,...);

int main()
{
int number = 1;

int foo = 0;

foo = va_sum(&number,2,3,4,5,0);

return 0;
}

int va_sum(int* a,...)
{
int counter = 0;
int element = 0;

va_list arg;

va_start(arg,a);

while((element = va_arg(arg,int)) != 0)
{
counter += element;
}

va_end(arg);

return counter;
}

#define va_arg(AP, TYPE) \
(AP += __va_rounded_size (TYPE), \
*((TYPE *) (AP - __va_rounded_size (TYPE))))

⑶ 關於c語言printf 與scanf問題 漏洞

這個是可以實現的,這樣就行。#include#include#include#includeintmain(){intn;printf("請輸入數字:【】\b\b");scanf("%d",&n);}\b是退格符號。因為「【】」是中文符號,所以佔得位元組數與英文字元不一樣。你自己可以調一下格式。而且這兒還有一個關鍵的問題,就是當「【】」內的內容夠長時,就會把後面的括弧「】」覆蓋掉。你可以自己試一下

⑷ C語言中如何使用printf來輸出C語言中的程序源代碼

裡面的雙引號加轉義符\
printf("for(i=1;i<=100;i++) \
scanf(\"%d\",&a);");

⑸ C語言中printf這個函數的代碼實現從哪裡可以看到,這個函數是如何實現的

printf格式化char*,int,float等的格式是固定的第一個p是char*,第二個*p對應int
字元串傳地址,整型直接傳值就可以
第一個可以改成輸出單個字元
1234char s[10]="abc";char *p=s;printf("%c\n",*p);printf("%c\n",*(p+1));

⑹ 請問到哪裡可以找C語言的庫函數的代碼,例如PRINTF函數的代碼

這個你找不到,我曾經在學習過程中也找過,只能在include文件夾下找到對於函數的定義,但函數體部分不可見.你可以到LINUX系統下找找,LINUX是完全開放源代碼的.

⑺ printf的問題

首先,總結性地回答:
windows和linux和Dos三種操作系統的printf源碼應該是略有區別的。
其次,介紹一下printf的底層顯示函數。
printf是在屏幕上列印一個字元串的函數。
(註:數字其實也是按照字元的形式顯示在屏幕上的。)
因為printf其實是解析format格式,例如%d,\n,等等。

真正在屏幕上顯示的函數是決定與操作系統底層的。
也就是都是調用底層的顯示策略函數實現的在屏幕的指定位置進行顯示。

例如:stdio.h中的printf.c的源碼中:
printChar函數就是頂層的顯示策略函數。

code]int putchar (int c) {
switch ((unsigned char) c) {
case '\n' :
newLine ();
break;
case '\r' :
carriageReturn ();
break;
case '\f' :
clearScreen ();
break;
case '\t' :
printChar (32); printChar (32); /* 32 = space */
printChar (32); printChar (32);
printChar (32); printChar (32);
printChar (32); printChar (32);
break;
case '\b':
backspace ();
break;
case '\a':
beep ();
break;
default :
printChar ((unsigned char) c);
}
return c;
}[/code]

接下來看printChar也就是輸出部分最低層的操作咯

[code]void printChar (const byte ch) {
*(word *)(VIDEO + y * 160 + x * 2) = ch | (fill_color << 8);
x++;
if (x >= WIDTH)
newLine ();
setVideoCursor (y, x);
}[/code]這里VIDEO表示顯存地址也就是0xB8000.通過 y * 160 + x 屏幕(x,y)坐標在顯存中的位置.這里需要知道,一個字元顯示需要兩個位元組,一個是ASCII碼,第二個是字元屬性代碼也就是顏色代碼.因此才必須 y * 80 * 2 + x = y * 160 + x.那麼ch | (fill_color << 8)也自然就是寫入字元及屬性代碼用的了.每寫一個字元游標位置加1,如果大於屏幕寬度WIDTH就換行.最後通過setVideoCursor設置新的游標位置.完成了整個printChar過程.

⑻ C語言中printf是庫函數,那麼printf的代碼到底在哪裡呢

在安裝目錄下有個叫Include的文件夾,裡面有很多.H的文件,用記事本打開STDIO.H的文件就是STDIO.H的代碼,裡麵包含了printf,scanf等函數的代碼。

⑼ 為什麼學完C語言,我還是看不懂printf的源代碼

你學的知識語法,知識入門,真正的高手是能駕馭它,為自己所用。看不懂也很正常,也沒必要這么氣累,需要繼續努力前進

⑽ 急求printf函數原代碼(匯編,api實現的都可以)

#include <cruntime.h>
#include <stdio.h>
#include <dbgint.h>
#include <stdarg.h>
#include <file2.h>
#include <internal.h>
#include <mtdll.h>

int __cdecl printf (
const char *format,
...
)
{
va_list arglist;
int buffing;
int retval;

va_start(arglist, format);

_ASSERTE(format != NULL);

_lock_str2(1, stdout);

buffing = _stbuf(stdout);

retval = _output(stdout,format,arglist);

_ftbuf(buffing, stdout);

_unlock_str2(1, stdout);

return(retval);
}

熱點內容
安卓怎麼卸載插件 發布:2024-10-09 19:07:13 瀏覽:930
see腳本 發布:2024-10-09 19:07:12 瀏覽:423
sqlleftouter 發布:2024-10-09 18:46:46 瀏覽:760
汽車燃氣解壓閥漏氣是什麼原因 發布:2024-10-09 18:18:51 瀏覽:424
java操作類 發布:2024-10-09 18:08:13 瀏覽:842
傳統編譯是靜態編譯嗎 發布:2024-10-09 18:08:13 瀏覽:857
360搜索無法訪問 發布:2024-10-09 18:04:49 瀏覽:51
bp神經網路演算法詳解 發布:2024-10-09 17:59:26 瀏覽:246
sql注入的過程 發布:2024-10-09 16:24:25 瀏覽:196
命令行ftp初始賬號密碼 發布:2024-10-09 16:24:24 瀏覽:292