當前位置:首頁 » 編程語言 » c語言strtod函數使用方法
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言strtod函數使用方法

發布時間: 2022-09-10 05:27:47

c語言strcpy()用法

1、strcpy函數是復制字元串的,接受兩個參數,一個是被復制字元串,另一個新字元串。具體的用法,首先打開編輯器新建一個c語言的程序文件,寫入頭文件和主函數:

⑵ C語言strtol函數用法

1 函數名:

strtol

2 聲明:

long int strtol(const char *nptr,char **endptr,int base);

3 功能:

將參數nptr字元串根據參數base來轉換成長整型數。

4 說明:

參數base范圍從2至36,或0。參數base代表採用的進制方式,如base值為10則採用10進制,若base值為16則採用16進制等。當base值為0時則是採用10進製做轉換,但遇到如』0x』前置字元則會使用16進製做轉換、遇到』0』前置字元而不是』0x』的時候會使用8進製做轉換。

一開始strtol()會掃描參數nptr字元串,跳過前面的空格字元,直到遇上數字或正負符號才開始做轉換,再遇到非數字或字元串結束時('')結束轉換,並將結果返回。若參數endptr不為NULL,則會將遇到不合條件而終止的nptr中的字元指針由endptr返回;若參數endptr為NULL,則會不返回非法字元串。

5 示例代碼:

#include<stdlib.h>
#include<stdio.h>
intmain()
{
char*string,*stopstring;
doublex;
intbase;
longl;

string="-1011Thisstoppedit";//源字元串
l=strtol(string,&stopstring,10);//轉換
printf("string=%s ",string);//列印原始字元串
printf("strtol=%ld ",l);//列印轉換結果
printf("Stoppedscanat:%s ",stopstring);//列印不合條件而終止的字元

return0;
}

輸出:

string=-1011Thisstoppedit
strtol=-1011
Stoppedscanat:Thisstoppedit

⑶ C語言請求幫助

下面為string.h文件中函數的詳細用法,附加實例:
1、strcpy
函數名: strcpy
功 能: 拷貝一個字元串到另一個
用 法: char *strcpy(char *destin, char *source);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strcpy(string, str1);

printf("%s\n", string);
return 0;
}
2、strcat
函數名: strcat
功 能: 字元串拼接函數
用 法: char *strcat(char *destin, char *source);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s\n", destination);
return 0;
}
3、strchr
函數名: strchr
功 能: 在一個串中查找給定字元的第一個匹配之處\
用 法: char *strchr(char *str, char c);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
printf("The character %c is at position: %d\n", c, ptr-string);
else
printf("The character was not found\n");
return 0;
}
4、strcmp
函數名: strcmp
功 能: 串比較
用 法: int strcmp(char *str1, char *str2);
看Asic碼,str1>str2,返回值 > 0;兩串相等,返回0
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");
ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");
return 0;
}
5、strncmpi
函數名: strncmpi
功 能: 將一個串中的一部分與另一個串比較, 不管大小寫
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1\n");
if (ptr == 0)
printf("buffer 2 equals buffer 1\n");
return 0;
}
6、strcpy
函數名: strcpy
功 能: 串拷貝
用 法: char *strcpy(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strcpy(string, str1);
printf("%s\n", string);
return 0;
}
7、strcspn
函數名: strcspn
功 能: 在串中查找第一個給定字元集內容的段
用 法: int strcspn(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";
int length;
length = strcspn(string1, string2);
printf("Character where strings intersect is at position %d\n", length);
return 0;
}
8、strp
函數名: strp
功 能: 將串拷貝到新建的位置處
用 法: char *strp(char *str);
程序例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *p_str, *string = "abcde";
p_str = strp(string);
printf("%s\n", p_str);
free(p_str);
return 0;
}
9、stricmp
函數名: stricmp
功 能: 以大小寫不敏感方式比較兩個串
用 法: int stricmp(char *str1, char *str2);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = stricmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1\n");
if (ptr == 0)
printf("buffer 2 equals buffer 1\n");
return 0;
}
10、strerror
函數名: strerror
功 能: 返回指向錯誤信息字元串的指針
用 法: char *strerror(int errnum);
程序例:
#include <stdio.h>
#include <errno.h>
int main(void)
{
char *buffer;
buffer = strerror(errno);
printf("Error: %s\n", buffer);
return 0;
}
11、strcmpi
函數名: strcmpi
功 能: 將一個串與另一個比較, 不管大小寫
用 法: int strcmpi(char *str1, char *str2);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1\n");
if (ptr == 0)
printf("buffer 2 equals buffer 1\n");
return 0;
}
函數名: strncmp
功 能: 串比較
用 法: int strncmp(char *str1, char *str2, int maxlen);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
int ptr;
ptr = strncmp(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");
ptr = strncmp(buf2,buf3,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");
return(0);
}
12、strncpy
函數名: strncpy
功 能: 串拷貝
用 法: char *strncpy(char *destin, char *source, int maxlen);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strncpy(string, str1, 3);
string[3] = '\0';
printf("%s\n", string);
return 0;
}
13、strnicmp
函數名: strnicmp
功 能: 不注重大小寫地比較兩個串
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr = strnicmp(buf2, buf1, 3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1\n");
if (ptr == 0)
printf("buffer 2 equals buffer 1\n");
return 0;
}
14、strnset
函數名: strnset
功 能: 將一個串中的所有字元都設為指定字元
用 法: char *strnset(char *str, char ch, unsigned n);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';
printf("string before strnset: %s\n", string);
strnset(string, letter, 13);
printf("string after strnset: %s\n", string);
return 0;
}
15、strpbrk
函數名: strpbrk
功 能: 在串中查找給定字元集中的字元
用 法: char *strpbrk(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string1 = "abcdefghijklmnopqrstuvwxyz";
char *string2 = "onm";
char *ptr;
ptr = strpbrk(string1, string2);
if (ptr)
printf("strpbrk found first character: %c\n", *ptr);
else
printf("strpbrk didn't find character in set\n");
return 0;
}
16、strrchr
函數名: strrchr
功 能: 在串中查找指定字元的最後一個出現
用 法: char *strrchr(char *str, char c);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strrchr(string, c);
if (ptr)
printf("The character %c is at position: %d\n", c, ptr-string);
else
printf("The character was not found\n");
return 0;
}
17、strrev
函數名: strrev
功 能: 串倒轉
用 法: char *strrev(char *str);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *forward = "string";
printf("Before strrev(): %s\n", forward);
strrev(forward);
printf("After strrev(): %s\n", forward);
return 0;
}
18、strset
函數名: strset
功 能: 將一個串中的所有字元都設為指定字元
用 法: char *strset(char *str, char c);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10] = "123456789";
char symbol = 'c';
printf("Before strset(): %s\n", string);
strset(string, symbol);
printf("After strset(): %s\n", string);
return 0;
}
19、strspn
函數名: strspn
功 能: 在串中查找指定字元集的子集的第一次出現
用 法: int strspn(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *string1 = "1234567890";
char *string2 = "123DC8";
int length;
length = strspn(string1, string2);
printf("Character where strings differ is at position %d\n", length);
return 0;
}
20、strstr
函數名: strstr
功 能: 在串中查找指定字元串的第一次出現
用 法: char *strstr(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %s\n", ptr);
return 0;
}
21、strtod
函數名: strtod
功 能: 將字元串轉換為double型值
用 法: double strtod(char *str, char **endptr);
程序例:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf\n", input, value);
return 0;
}
22、strtok
函數名: strtok
功 能: 查找由在第二個串中指定的分界符分隔開的單詞
用 法: char *strtok(char *str1, char *str2);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char input[16] = "abc,d";
char *p;
/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%s\n", p);
/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s\n", p);
return 0;
}
23、strtol
函數名: strtol
功 能: 將串轉換為長整數
用 法: long strtol(char *str, char **endptr, int base);
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *string = "87654321", *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 10);
printf("string = %s long = %ld\n", string, lnumber);
return 0;
}
24、strupr
函數名: strupr
功 能: 將串中的小寫字母轉換為大寫字母
用 法: char *strupr(char *str);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
/* converts string to upper case characters */
ptr = strupr(string);
printf("%s\n", ptr);
return 0;
}
25、swab
函數名: swab
功 能: 交換位元組
用 法: void swab (char *from, char *to, int nbytes);
程序例:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char source[15] = "rFna koBlrna d";
char target[15];
int main(void)
{
swab(source, target, strlen(source));
printf("This is target: %s\n", target);
return 0;
原型:extern char *strstr(char *haystack, char *needle);
*所在頭文件:#include <string.h>
*功能:從字元串haystack中尋找needle第一次出現的位置(不比較結束符NULL)。
*說明:返回指向第一次出現needle位置的指針,如果沒找到則返回NULL

⑷ 高手幫忙解釋下這個C語言庫函數strtod的執行過程

if ( (*p | 32) == 'e' ) //判斷*p是e或E

{

expo = 0;

factor = 10.L;

switch (*++p)

{

case '-': //負號乘積因子就從0.1開始

factor = 0.1;

case '+': //正號就從10開始

p++;

break;

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

break;

default ://非法字元就返回

value = 0.L;

p = s;

goto done;

}

while ( (unsigned int)(*p - '0') < 10u )//把指數取出來

expo = 10 * expo + (*p++ - '0');

while ( 1 )//把指數乘到value上

{

if ( expo & 1 )

value *= factor;

if ( (expo >>= 1) == 0 )

break;

factor *= factor;

}

}

done:

if ( endptr != 0 )

*endptr = (char*)p;

return (sign == '-' ? -value : value);

}

其他不用解釋了吧。這個程序寫的不錯。

⑸ strcpy函數如何使用

1、strcpy 函數使用方法:

strcpy()函數是C語言中的一個復制字元串的庫函數。

2、空指針檢查:源指針和目的指針都有可能會出現空指針的情況,所以應該對其進行檢查。

3、const 修飾:源字元串參數用const修飾,防止修改源字元串;

4、為什麼要設置ret 指針以及返回ret指針的位置[3],由於目的指針dst已經在進行移動了,所以用輔助指針ret表明首指針;

(5)c語言strtod函數使用方法擴展閱讀:

strcpy 函數的錯誤用法及反饋:

1、不檢查指針的有效性,說明答題者不注重代碼的健壯性。

2、(A)return new string("Invalid argument(s)");,說明答題者根本不知道返回值的用途,並且他對內存泄漏也沒有警惕心。從函數中返回函數體內分配的內存是十分危險的做法,他把釋放內存的義務拋給不知情的調用者,絕大多數情況下,調用者不會釋放內存,這導致內存泄漏。

3、(B)return 0;,說明答題者沒有掌握異常機制。調用者有可能忘記檢查返回值,調用者還可能無法檢查返回值(見後面的鏈式表達式)。妄想讓返回值肩負返回正確值和異常值的雙重功能,其結果往往是兩種功能都失效。應該以拋出異常來代替返回值,這樣可以減輕調用者的負擔、使錯誤不會被忽略、增強程序的可維護性。

4、(A)忘記保存原始的strDest值,說明答題者邏輯思維不嚴密。

⑹ c語言中strlen()怎麼用

函數聲明:extern unsigned int strlen(char *s);

所屬函數庫:<string.h>

功能:返回s所指的字元串的長度,其中字元串必須以』\0』結尾

參數:s為字元串的初始地址

使用舉例:

代碼如下

編譯運行結果

說明:

函數strlen比較容易理解,其功能和sizeof很容易混淆。其中sizeof指的是字元串聲明後佔用的內存長度,它就是一個操作符,不是函數;而strlen則是一個函數,它從第一個位元組開始往後數,直到遇見了』\0』,則停止

第一 string頭文件中的函數strlen 只可以用來測試字元型數組的長度,其他類型不可以

第二 如果要測試int 修改原函數中的參數,會導致數組長度變長(計算機會一直找下去,直到遇到\0)

第三 如果是字元數組,當裡面的字元數少於數組長度時,可以正常輸出用strlen,如果等於數組長度,則會出現問題

函數介紹:

(推薦教程:c語言教程)

strlen()函數用於計算字元串的長度,直到空結束字元,但不包括空結束字元。

語法結構:

size_t strlen(const char *str)

參數說明:

str -- 要計算長度的字元串。

返回值:

該函數返回字元串的長度

⑺ c語言strtod()函數的用法

index對應第一個操作數的字元數,這個前提是操作數前面沒有空格一類另strtod自動跳過的字元。
因為endptr是strtod轉換字元串的結尾位置,比如buf中的字元串是1234abc,轉換時將轉換到1234為止,strtod發現字元a不屬於合法數字內容,會在遇到a時結束轉換,並另endptr指向a的這個位置。index=endptr-buf;會得到a字元前面有幾個字元的計數。

⑻ c語言中string怎麼用啊

用於輸入輸出的字元串函數, 在使用前應包含頭文件"stdio.h" ; 使用其它字元串函數則應包含頭文件"string.h"。