c語言庫函數源碼
① c語言庫函數qsort源代碼
void __fileDECL qsort (
void *base,
size_t num,
size_t width,
int (__fileDECL *comp)(const void *, const void *)
)
#endif /* __USE_CONTEXT */
{
char *lo, *hi; /* ends of sub-array currently sorting */
char *mid; /* points to middle of subarray */
char *loguy, *higuy; /* traveling pointers for partition step */
size_t size; /* size of the sub-array */
char *lostk[STKSIZ], *histk[STKSIZ];
int stkptr; /* stack for saving sub-array to be processed */
/此或老* validation section */
_VALIDATE_RETURN_VOID(base != NULL || num == 0, EINVAL);
_VALIDATE_RETURN_VOID(width > 0, EINVAL);
_VALIDATE_RETURN_VOID(comp != NULL, EINVAL);
if (num < 2)
return; /* nothing to do */
stkptr = 0; /* initialize stack */
lo = (char *)base;
hi = (char *)base + width * (num-1); /* initialize limits */
/森升* this entry point is for pseudo-recursion calling: setting
lo and hi and jumping to here is like recursion, but stkptr is
preserved, locals aren't, so we preserve stuff on the stack */
recurse:
size = (hi - lo) /團基 width + 1; /* number of el's to sort */
/* below a certain size, it is faster to use a O(n^2) sorting method */
if (size <= CUTOFF) {
__SHORTSORT(lo, hi, width, comp, context);
}
else {
/* First we pick a partitioning element. The efficiency of the
algorithm demands that we find one that is approximately the median
of the values, but also that we select one fast. We choose the
median of the first, middle, and last elements, to avoid bad
performance in the face of already sorted data, or data that is made
up of multiple sorted runs appended together. Testing shows that a
median-of-three algorithm provides better performance than simply
picking the middle element for the latter case. */
mid = lo + (size / 2) * width; /* find middle element */
/* Sort the first, middle, last elements into order */
if (__COMPARE(context, lo, mid) > 0) {
swap(lo, mid, width);
}
if (__COMPARE(context, lo, hi) > 0) {
swap(lo, hi, width);
}
if (__COMPARE(context, mid, hi) > 0) {
swap(mid, hi, width);
}
/* We now wish to partition the array into three pieces, one consisting
of elements <= partition element, one of elements equal to the
partition element, and one of elements > than it. This is done
below; comments indicate conditions established at every step. */
loguy = lo;
higuy = hi;
/* Note that higuy decreases and loguy increases on every iteration,
so loop must terminate. */
for (;;) {
/* lo <= loguy < hi, lo < higuy <= hi,
A[i] <= A[mid] for lo <= i <= loguy,
A[i] > A[mid] for higuy <= i < hi,
A[hi] >= A[mid] */
/* The doubled loop is to avoid calling comp(mid,mid), since some
existing comparison funcs don't work when passed the same
value for both pointers. */
if (mid > loguy) {
do {
loguy += width;
} while (loguy < mid && __COMPARE(context, loguy, mid) <= 0);
}
if (mid <= loguy) {
do {
loguy += width;
} while (loguy <= hi && __COMPARE(context, loguy, mid) <= 0);
}
/* lo < loguy <= hi+1, A[i] <= A[mid] for lo <= i < loguy,
either loguy > hi or A[loguy] > A[mid] */
do {
higuy -= width;
} while (higuy > mid && __COMPARE(context, higuy, mid) > 0);
/* lo <= higuy < hi, A[i] > A[mid] for higuy < i < hi,
either higuy == lo or A[higuy] <= A[mid] */
if (higuy < loguy)
break;
/* if loguy > hi or higuy == lo, then we would have exited, so
A[loguy] > A[mid], A[higuy] <= A[mid],
loguy <= hi, higuy > lo */
swap(loguy, higuy, width);
/* If the partition element was moved, follow it. Only need
to check for mid == higuy, since before the swap,
A[loguy] > A[mid] implies loguy != mid. */
if (mid == higuy)
mid = loguy;
/* A[loguy] <= A[mid], A[higuy] > A[mid]; so condition at top
of loop is re-established */
}
/* A[i] <= A[mid] for lo <= i < loguy,
A[i] > A[mid] for higuy < i < hi,
A[hi] >= A[mid]
higuy < loguy
implying:
higuy == loguy-1
or higuy == hi - 1, loguy == hi + 1, A[hi] == A[mid] */
/* Find adjacent elements equal to the partition element. The
doubled loop is to avoid calling comp(mid,mid), since some
existing comparison funcs don't work when passed the same value
for both pointers. */
higuy += width;
if (mid < higuy) {
do {
higuy -= width;
} while (higuy > mid && __COMPARE(context, higuy, mid) == 0);
}
if (mid >= higuy) {
do {
higuy -= width;
} while (higuy > lo && __COMPARE(context, higuy, mid) == 0);
}
/* OK, now we have the following:
higuy < loguy
lo <= higuy <= hi
A[i] <= A[mid] for lo <= i <= higuy
A[i] == A[mid] for higuy < i < loguy
A[i] > A[mid] for loguy <= i < hi
A[hi] >= A[mid] */
/* We've finished the partition, now we want to sort the subarrays
[lo, higuy] and [loguy, hi].
We do the smaller one first to minimize stack usage.
We only sort arrays of length 2 or more.*/
if ( higuy - lo >= hi - loguy ) {
if (lo < higuy) {
lostk[stkptr] = lo;
histk[stkptr] = higuy;
++stkptr;
} /* save big recursion for later */
if (loguy < hi) {
lo = loguy;
goto recurse; /* do small recursion */
}
}
else {
if (loguy < hi) {
lostk[stkptr] = loguy;
histk[stkptr] = hi;
++stkptr; /* save big recursion for later */
}
if (lo < higuy) {
hi = higuy;
goto recurse; /* do small recursion */
}
}
}
/* We have sorted the array, except for any pending sorts on the stack.
Check if there are any, and do them. */
--stkptr;
if (stkptr >= 0) {
lo = lostk[stkptr];
hi = histk[stkptr];
goto recurse; /* pop subarray from stack */
}
else
return; /* all subarrays done */
}
② 求C語言標准函數庫的源代碼
標准庫只是定義介面,具體怎麼實現就得看操作系統,你說win下和linux下這些函數的實現會一樣嗎。當然不一樣,看這些學源碼,不如看看c標准,c89或c99.
那可以看內核,看系統調用是怎麼樣實現的,你說的那些都是基於系統調用的
③ C語言庫函數源代碼在哪裡有看
有安裝vs2008或2010嗎,在安裝目錄下面的VC/src中自帶有源代碼。比如我的就在
D:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src中。沒有的話發給你
④ 如何查看C語言,內庫的源代碼
如果是「.cpp」文件並且有VC++的環境,可直接雙擊文件打開或者先打開編譯環境,在新建一個控制台下的源文件,然後,選擇file菜單下的open找到你的文件導入,然後編譯運行;如果是其他格式的,如txt文件,也可先打開編譯環境,新建一個控制台下的源文件,然後直接復制粘貼進去,然後編譯運行;
便已運行的操作如圖:
⑤ C語言庫函數如何編寫
/***
*printf.c - print formatted
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines printf() - print formatted data
*
*******************************************************************************/
#include
#include
#include
#include
#include
#include
#include
/***
*int printf(format, ...) - print formatted data
*
*Purpose:
* Prints formatted data on stdout using the format string to
* format data and getting as many arguments as called for
* Uses temporary buffering to improve efficiency.
* _output does the real work here
*
*Entry:
* char *format - format string to control data format/number of arguments
* followed by list of arguments, number and type controlled by
* format string
*
*Exit:
* returns number of characters printed
*
*Exceptions:
*
*******************************************************************************/
int __cdecl printf (
const char *format,
...
)
/*
* stdout ''PRINT'', ''F''ormatted
*/
{
va_list arglist;
int buffing;
int retval;
va_start(arglist, format);
_ASSERTE(format != NULL);//斷言宏。如果輸出格式字元串指針為空,則在DEBUG版下斷言,報告錯誤。
_lock_str2(1, stdout);
buffing = _stbuf(stdout);//stdout:指定輸出到屏幕
retval = _output(stdout,format,arglist);
_ftbuf(buffing, stdout);
_unlock_str2(1, stdout);
return(retval);
}
以上為printf()的源代碼
1、從含有可選參數函數中獲得可選參數,以及操作這些參數
typedef char *va_list;
void va_start( va_list arg_ptr, prev_param );
type va_arg( va_list arg_ptr, type );
void va_end( va_list arg_ptr );
假定函數含有一個必選參數和多個可選參數,必選參數聲明為普通數據類型,且能通過參數名來獲得該變數的值。可選參數通過宏va_start、va_arg和va_end(定義在stdarg.h或varargs.h中)來進行操作,即通過設置指向第一個可選參數指針、返回當前參數、在返回參數後重新設置指針來操作所有的可選參數。
va_start:為獲取可變數目參數的函數的參數提供一種便捷手段。設置arg_ptr為指向傳給函數參數列表中的第一個可選參數的指針,且該參數必須是va_list類型。prev_param是在參數列表中第一個可選參數前的必選參數。
va_arg:返回由arg_ptr所指向的參數的值,且自增指向下一個參數的地址。type為當前參數的類型,用來計算該參數的長度,確定下一個參數的起始位置。它可以在函數中應用多次,直到得到函數的所有參數為止,但必須在宏va_start後面調用。
va_end:在獲取所有的參數後,設置指針arg_ptr為NULL。
下面舉例說明:
#include
#include
int average( int first, ... );
void main( void )
{
/* Call with 3 integers (-1 is used as terminator). */
printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );
/* Call with 4 integers. */
printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );
/* Call with just -1 terminator. */
printf( "Average is: %d\n", average( -1 ) );
}
int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;
va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}
返回值為:
Average is: 3
Average is: 8
Average is: 0
綜上所述,在printf()函數中,可以只輸出一個字元串,也可按照一定的形式輸出含有多個可選參數的字元串信息。因此,首先就要通過這些宏來獲取所有的可選參數。在上面的源碼可以看出printf()中,只使用了宏at_start,將可選參數的首地址賦給了arglist。
2、鎖定字元串及輸出字元串到屏幕
#define _lock_str2(i,s) _lock_file2(i,s)
void __cdecl _lock_file2(int, void *);
#define _unlock_str2(i,s) _unlock_file2(i,s)
void __cdecl _unlock_file2(int, void *);
int __cdecl _stbuf(FILE *);
void __cdecl _ftbuf(int, FILE *);
int __cdecl _output(FILE *, const char *, va_list);
在output函數中,讀取格式字元串中的每一個字元,然後對其進行處理,處理方式根據每一個字元所代表的意義來進行,如:普通字元直接利用函數WRITE_CHAR(ch, &charsout);輸出到控制台。
其中的主要部分是對轉換說明符(d,c,s,f)的處理,現在將對其中的部分代碼進行詳細說明,這里只說明最基本的轉換說明符,對這些須基本的轉換說明符進行修飾的修飾符,程序中單獨進行處理。下面是函數output()(output.c)部分源代碼:
case ST_TYPE:
//表示當前處理的字元的類型為轉換說明符。
...
switch (ch) {
//下面對參數的獲取都是利用宏va_arg( va_list arg_ptr, type );來進行的。
case ''c'': {
//從參數表中獲取單個字元,輸出到緩沖字元串中,此時,type=int
buffer[0] = (char) get_int_arg(&argptr); /* get char to print */
text = buffer;
textlen = 1; /* print just a single character */
}
break;
case ''s'': {
//從參數表中獲取字元串,輸出到緩沖字元串中,此時,type=char*
int i;
char *p; /* temps */
text = get_ptr_arg(&argptr);
...
}
break;
case ''w'': {
//對寬字元進行處理
...
} /* case ''w'' */
break;
...
case ''e'':
case ''f'':
case ''g'': {
//對浮點數進行操作
...
#if !LONGDOUBLE_IS_DOUBLE
/* do the conversion */
if (flags & FL_LONGDOUBLE) {
_cldcvt((LONGDOUBLE*)argptr, text, ch, precision, capexp);
va_arg(argptr, LONGDOUBLE);
//對長雙精度型進行處理,此時,type=long double
}
else
#endif /* !LONGDOUBLE_IS_DOUBLE */
{
//對雙精度型進行處理,此時,type=double
_cfltcvt((DOUBLE*)argptr, text, ch, precision, capexp);
va_arg(argptr, DOUBLE);
}
...
break;
//對整型變數處理
case ''d'':
case ''i'':
...
goto COMMON_INT;
case ''u'':
radix = 10;
goto COMMON_INT;
case ''p'':
...
goto COMMON_INT;
case ''o'':
...
註:對於浮點型double和long double,有相應的轉換說明符(%f表示雙精度型,%lf表示長雙精度型),而float卻沒有。其中的原因是,在K&RC下,float值用於表達式或用作參數前,會自動轉換成double類型。而ANSI C一般不會自動把float轉換成double。有些程序已假定其中的float參數會被轉換成double,為了保護大量這樣的程序,所有printf()函數的float參數還是被自動轉換成double型。因此,在K&RC或ANSI C下,都無需用特定的轉換說明符來顯示float型。
綜上所述,轉換說明符必須與待列印字元的類型。通常,用戶有種選擇。例如,如要列印一個int類型的值。則只可以使用%d,%x或%o。所有這些說明符都表示要列印一個int類型的值;它們只不過提供了一個數值的幾種不同表示。類似一,可以用%f、%g和%e來表示double類型的值。但如果轉換說明與類型不匹配,將會出現意想不到的結果。為什麼呢?問題就在於C向函數傳遞信息的方式。
這個失敗的根本細節與具體實現相關。它決定了系統中的參數以何方式傳遞。函數調用如下:
float n1;
double n2;
long n3;
long n4;
...
printf("%ld,%ld,%ld,%ld",n1,n2,n3,n4);
這個調用告訴計算機,要把變數n1,n2,n3和n4的值交給計算機,它把這些變數放進稱作棧(stack)的內存區域中,來完成這一任務。計算機把這些值放進棧中,其根據是變數的類型而不是轉換說明符,比如n1,把8個位元組放入棧中(float被轉換成double),類似地,為n2放了8位元組,其後給n3和n4各放了4個位元組。接著,控制的對象轉移到printf();此函數從棧中讀數,不過在這一過程中,它是在轉換說明符的指導下,讀取數值的。說明符%ld指定printf()應讀4個位元組(va_arg( va_list arg_ptr, type )中type=long),因此printf()讀入棧中的4個位元組,作為它的第一個值。但是這只是n1的前半部分,這個值被看成一個long整數。下一個說明符%ld讀入4個位元組,這正是n1的後半部分,這個值被看成第二個long整數。類似地,第三、第四次又讀入n2的前後兩部分。因此,盡管我們對n3和n4使用了正確的說明符,printf()仍然會產生錯誤。
⑥ 如何看c語言標准庫函數的源代碼
1、首先標准只是規定了這些函數的介面和具體的運行效率的要求,這些函數具體是怎麼寫得要看各個編譯器的實現和平台。
2、例如使用的編譯器是visual studio,微軟提供了一部分C運行時(CRT)的源碼,裡面會有memcpy,strcpy之類的函數的實現,在visual studio 2005下的路徑是C:Program FilesMicrosoft Visual Studio 8VCcrtsrc。
⑦ 在C語言里,關於庫函數中各種數學函數的代碼。
你說的就是庫函數的源碼,也就是glibc,源碼在ftp://ftp.gnu.org/gnu/glibc可以下到,比如下載ftp://ftp.gnu.org/gnu/glibc/glibc-2.9.tar.gz,打開後就可以看到你需要的各種庫的具體實現代碼,比如在string中的strcat.c中就有
char*strcat(dest,src)
char*dest;
constchar*src;
{
char*s1=dest;
constchar*s2=src;
reg_charc;
/*Findtheendofthestring.*/
do
c=*s1++;
while(c!='