CH2-1 类型 操作 表达式
生活随笔
收集整理的這篇文章主要介紹了
CH2-1 类型 操作 表达式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2.1 變量名 (1) 大小寫敏感,小寫一般做變量,大寫做符號常量(symbolic constant) (2) 以字母和_開頭,有些類庫用了"_"
2.2 數(shù)據(jù)類型和長度 (1) char?????? //一般一個字節(jié),8位 unsigned char? (0~255)??????? signed char (-128~127)? #include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char** argv) {
??????printf("char_bit = %d\n", CHAR_BIT);
??????printf("char_min = %d, char_max = %d\n", CHAR_MIN, CHAR_MAX);
??????printf("signed_char_min = %d, signed_char_max = %d\n", SCHAR_MIN, SCHAR_MAX);
??????printf("unsigned_char_min = %d, unsigned_char_max = %d\n", 0, UCHAR_MAX);
??????return (EXIT_SUCCESS);
} (2) int short? (int 16 bits) <= int <= long (int? 32 bits) #include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char** argv) {
????????printf("short_min = %d, short_max = %d\n", SHRT_MIN, SHRT_MAX);
????????printf("int_min = %d, int_max = %d\n", INT_MIN, INT_MAX);
????????printf("long_min = %d, long_max = %d\n", LONG_MIN, LONG_MAX);
????????return (EXIT_SUCCESS);
} 缺省情況下是signed, 如unsigned short (0~65535)?? signed short (-32768 ~ 32767) #include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char** argv) {
????????printf("unsigned_short_max = %u\n", USHRT_MAX);
????????printf("unsigned_int_max = %u\n", UINT_MAX);
????????printf("unsigned_long_max = %u\n", ULONG_MAX); //注意%ud,u表示unsigned
????????return (EXIT_SUCCESS);
} (3)? float (single-precision floating point), double, long double IEEE-754?
??????????float (4 bytes) or double (8 bytes) seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm s = sign bit, e = exponent, m = mantissa
????????? N = mantissa * (2 ^? x),? x 是指數(shù) #include <stdio.h>
#include <stdlib.h>
#include <float.h>
int main(int argc, char** argv) {
??????printf("float: decimal digits of precision: %d\n", FLT_DIG);
??????printf("float: number of base FLT_RADIX digits in mantissa: %d\n", FLT_MANT_DIG);
??????printf("float: min_float = %e, max_float = %e\n", FLT_MIN, FLT_MAX);
??????printf("======================================\n");
??????printf("double: decimal digits of precision: %d\n", DBL_DIG);
??????printf("double: number of base FLT_RADIX digits in mantissa: %d\n", DBL_MANT_DIG);
??????printf("double: min_double = %e, max_double = %e\n", DBL_MIN, DBL_MAX);
??????return (EXIT_SUCCESS);
} (4)? 長度范圍等可查看頭文件 <limits.h> <float.h>中,依賴于不同的機器
2.3 常量 (1) 一般常量 int: 1234,31 = 037 = 0x1f = 0X1F //八進制 或者 16進制 long: 123456789L??? usigned long: 123456789UL double: 123.4, 1e-2,? 當(dāng)末尾加f或者F時,表明是float (2) 字符常量 '0' == 48 '\0' == 0 '\ooo'??//八進制?? #define VTAB '\013'??? //#define? BELL? '\007' '\xhh'? //16進制??? #define VTAB '\xb'????? //#define BELL '\x7' \a? alert (bell) \b? backspace \f?? formfeed \n? newline \r?? carriage return \t?? horizontal tab \v? vertical tab \\?? backslash \??? question mark \'??? single quote \"?? double quote (3) 表達式常量 #define MAXLINE 1000 char line[MAXLINE + 1]; #define LEAP 1? //leap year 閏年 int days[31+28+LEAP+31+30+31+30+31+31+30+31+30+31]; (4) 字符串常量 "I am a string" //字符串實質(zhì)上是字符數(shù)組,以'\0'結(jié)尾 ""?? //空字符串? "?" //空格 字符串? 區(qū)分 'x' 和?"x" 'x' 是字符,本質(zhì)上整型 "x"是字符串,以'\0'結(jié)尾 <string.h> 中 strlen(s) 求字符串的長度,不包括'\0' (5) enumeration constant enum boolean {NO, YES};? //第一個為0, 下一個為1, 依次增長 enum escapes {BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r'}; enum months {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; //從1開始增長
2.4 變量聲明與初始化 (1) ??變量聲明 所有變量在使用之前必須先聲明 char c, line[100];? char c; char line[100]; (2) ??變量初始化 float eps = 1.0e-5; const double e = 2.71828182845905; const char msg[] = "hello world!"; // const 表明該數(shù)組內(nèi)的字符不可改 ? int strlen(const char[]);
2.5 數(shù)學(xué)運算符 +?? -?? *?? /??? % if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
????????????????printf("%d is a leap year \n", year);
????????else printf("%d is not a leap year \n", year);
2.6 關(guān)系運算符和邏輯運算符 (1) 關(guān)系運算符 >??? >=??? <??? <= ==??? != 關(guān)系運算符的優(yōu)先級 低于?數(shù)序運算符,即? i?< lim -1??等同于 i < (lim -1) (2) 邏輯運算符 &&? ||? ! 從左到右,evaluation stops as soon as the truth or false of the result is known. ????? for( i = 0; i < lim -1 && (c = getchar()) != '\n'? && c != EOF; ++i ) ?????????????s[i] = c; 邏輯表達式 真為1,假為0 !取反
2.2 數(shù)據(jù)類型和長度 (1) char?????? //一般一個字節(jié),8位 unsigned char? (0~255)??????? signed char (-128~127)? #include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char** argv) {
??????printf("char_bit = %d\n", CHAR_BIT);
??????printf("char_min = %d, char_max = %d\n", CHAR_MIN, CHAR_MAX);
??????printf("signed_char_min = %d, signed_char_max = %d\n", SCHAR_MIN, SCHAR_MAX);
??????printf("unsigned_char_min = %d, unsigned_char_max = %d\n", 0, UCHAR_MAX);
??????return (EXIT_SUCCESS);
} (2) int short? (int 16 bits) <= int <= long (int? 32 bits) #include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char** argv) {
????????printf("short_min = %d, short_max = %d\n", SHRT_MIN, SHRT_MAX);
????????printf("int_min = %d, int_max = %d\n", INT_MIN, INT_MAX);
????????printf("long_min = %d, long_max = %d\n", LONG_MIN, LONG_MAX);
????????return (EXIT_SUCCESS);
} 缺省情況下是signed, 如unsigned short (0~65535)?? signed short (-32768 ~ 32767) #include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char** argv) {
????????printf("unsigned_short_max = %u\n", USHRT_MAX);
????????printf("unsigned_int_max = %u\n", UINT_MAX);
????????printf("unsigned_long_max = %u\n", ULONG_MAX); //注意%ud,u表示unsigned
????????return (EXIT_SUCCESS);
} (3)? float (single-precision floating point), double, long double IEEE-754?
??????????float (4 bytes) or double (8 bytes) seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm s = sign bit, e = exponent, m = mantissa
????????? N = mantissa * (2 ^? x),? x 是指數(shù) #include <stdio.h>
#include <stdlib.h>
#include <float.h>
int main(int argc, char** argv) {
??????printf("float: decimal digits of precision: %d\n", FLT_DIG);
??????printf("float: number of base FLT_RADIX digits in mantissa: %d\n", FLT_MANT_DIG);
??????printf("float: min_float = %e, max_float = %e\n", FLT_MIN, FLT_MAX);
??????printf("======================================\n");
??????printf("double: decimal digits of precision: %d\n", DBL_DIG);
??????printf("double: number of base FLT_RADIX digits in mantissa: %d\n", DBL_MANT_DIG);
??????printf("double: min_double = %e, max_double = %e\n", DBL_MIN, DBL_MAX);
??????return (EXIT_SUCCESS);
} (4)? 長度范圍等可查看頭文件 <limits.h> <float.h>中,依賴于不同的機器
2.3 常量 (1) 一般常量 int: 1234,31 = 037 = 0x1f = 0X1F //八進制 或者 16進制 long: 123456789L??? usigned long: 123456789UL double: 123.4, 1e-2,? 當(dāng)末尾加f或者F時,表明是float (2) 字符常量 '0' == 48 '\0' == 0 '\ooo'??//八進制?? #define VTAB '\013'??? //#define? BELL? '\007' '\xhh'? //16進制??? #define VTAB '\xb'????? //#define BELL '\x7' \a? alert (bell) \b? backspace \f?? formfeed \n? newline \r?? carriage return \t?? horizontal tab \v? vertical tab \\?? backslash \??? question mark \'??? single quote \"?? double quote (3) 表達式常量 #define MAXLINE 1000 char line[MAXLINE + 1]; #define LEAP 1? //leap year 閏年 int days[31+28+LEAP+31+30+31+30+31+31+30+31+30+31]; (4) 字符串常量 "I am a string" //字符串實質(zhì)上是字符數(shù)組,以'\0'結(jié)尾 ""?? //空字符串? "?" //空格 字符串? 區(qū)分 'x' 和?"x" 'x' 是字符,本質(zhì)上整型 "x"是字符串,以'\0'結(jié)尾 <string.h> 中 strlen(s) 求字符串的長度,不包括'\0' (5) enumeration constant enum boolean {NO, YES};? //第一個為0, 下一個為1, 依次增長 enum escapes {BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r'}; enum months {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; //從1開始增長
2.4 變量聲明與初始化 (1) ??變量聲明 所有變量在使用之前必須先聲明 char c, line[100];? char c; char line[100]; (2) ??變量初始化 float eps = 1.0e-5; const double e = 2.71828182845905; const char msg[] = "hello world!"; // const 表明該數(shù)組內(nèi)的字符不可改 ? int strlen(const char[]);
2.5 數(shù)學(xué)運算符 +?? -?? *?? /??? % if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
????????????????printf("%d is a leap year \n", year);
????????else printf("%d is not a leap year \n", year);
2.6 關(guān)系運算符和邏輯運算符 (1) 關(guān)系運算符 >??? >=??? <??? <= ==??? != 關(guān)系運算符的優(yōu)先級 低于?數(shù)序運算符,即? i?< lim -1??等同于 i < (lim -1) (2) 邏輯運算符 &&? ||? ! 從左到右,evaluation stops as soon as the truth or false of the result is known. ????? for( i = 0; i < lim -1 && (c = getchar()) != '\n'? && c != EOF; ++i ) ?????????????s[i] = c; 邏輯表達式 真為1,假為0 !取反
轉(zhuǎn)載于:https://blog.51cto.com/amao99/216230
總結(jié)
以上是生活随笔為你收集整理的CH2-1 类型 操作 表达式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: perl pop和push函数,不使用索
- 下一篇: tomcat自启动设置