图灵俱乐部第六期C语言基础TypeConversionsAcharisjustasmallinteger举个栗子1/*atoi:convertstointeger*/intatoi(chars[]){ inti,n; n=0; for(i=0;s[i]>='0'&&s[i]<='9';++i)n=10*n+(s[i]-'0');returnn;}TypeConversions举个栗子2/*lower:convertctolowercase;ASCIIonly*/intlower(intc){ if(c>='A'&&c<='Z') returnc+'a'-'A';elsereturnc;}TypeConversionsThestandardheader<>,,ifanoperatorlike+or*thattakestwooperands(abinaryoperator)hasoperandsofdifferenttypes,the“lower”typeispromotedtothe“higher”,,ifeitheroperandisdouble,,ifeitheroperandisfloat,,,ifeitheroperandislong,!Floatsinanexpressionarenotautomaticallyconvertedtodouble;Ingeneral,mathematicalfunctionslikethosein<>,or,lessoften,tosavetimeonmachineswheredouble-:-1L<1U,because1U,whichisanunsignedint,-1L>1UL,because-,explicittypeconversionscanbeforced(“coerced”)inanyexpression,:(type-name)expressionTypeConversions举个栗子3unsignedlongintnext=1;/*rand:returnpseudo-randomintegeron0..32767*/intrand(void){ next=next***********+12345; return(unsignedint)(next/65536)%32768;}/*srand:setseedforrand()*/vooidsrand(unsignedintseed){ next=seed;}
图灵俱乐部第六期C语言基础 来自淘豆网m.daumloan.com转载请标明出处.