字符串类型转换

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

 

int main0101()

{

  char ch[] = "   -123-456";

//atoi会扫描字符串,跳过前面的空格字符,直到遇到数字或正负号才开始转换,而遇到非数字或字符串结束符\0才结束

  int i=atoi(ch);

  printf("%d\n", i);//-123

  return EXIT_SUCCESS;

}

int main0102(void)

{

  char ch[] = "   -123.456-456abc123";

  double i = atof(ch);  

  printf("%.2f\n", i);//-123.56

  return 0;

}

int main(void)

{

  char ch[] = "   -123.456-456abc123";

  long i = atol(ch);

  printf("%ld\n", i);//-123

  return 0;

}

posted @ 2020-09-04 22:36  wh19991213  阅读(124)  评论(0编辑  收藏  举报