1. Trang chủ
  2. » Công Nghệ Thông Tin

C and cplusplus tutorial

63 321 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 63
Dung lượng 283 KB

Nội dung

C/C++ Tutorial CSU480 CSU480 Who is the TA?            !!"#$  !!"#$ %& %& '())' '())' *&+,+++)))'--. *&+,+++)))'--. Outline  / / &&#&'0 &&#&'0 1 1  23 23 $,& $,&  34 34  5 5 6&3 6&3  *'& *'&  63 63  527 527  Pointers Pointers  Functions Functions  Command-Line Argument Command-Line Argument  Data Structure Data Structure  Memory Allocation Memory Allocation  Programming Tips Programming Tips  C C v v s. C++ s. C++  Books recommended Books recommended Hello World Program  ' ' 8&'9'): 8&'9'):     ; ; 340&&#&'<0= 340&&#&'<0= = = > >   Hello World Program  +3&? +3&? @ @ &&)A&& &&)A&& gcc gcc compiling command compiling command hello.c hello.c source file source file hello hello compiler-generated executable file compiler-generated executable file Note: the default output filename is “ Note: the default output filename is “ a.out a.out ” ”  +B +B ? ?   )-&& )-&& / / )-C'4&&+D&/&&C )-C'4&&+D&/&&C '''2) '''2) Hello World Program Q: why “.” is not included in $PATH Q: why “.” is not included in $PATH environment variable? environment variable? Hello World Program 52') 52') Command Command Location Location Comment Comment ls ls /bin/ls /bin/ls provided by the provided by the system system ls ls current directory current directory virus virus Name Name Description Description Size* Size* Range* Range* char char Character or small Character or small integer integer 1 byte 1 byte signed: -128 to 127 signed: -128 to 127 unsigned: 0 to 255 unsigned: 0 to 255 short int short int (short) (short) Short integer Short integer 2 bytes 2 bytes signed: -32768 to 32767 signed: -32768 to 32767 unsigned: 0 to 65535 unsigned: 0 to 65535 int int Integer Integer 4 bytes 4 bytes signed: -2147483648 to signed: -2147483648 to 2147483647 2147483647 unsigned: 0 to 4294967295 unsigned: 0 to 4294967295 long int long int (long) (long) Long integer Long integer 4 bytes 4 bytes signed: -2147483648 to signed: -2147483648 to 2147483647 2147483647 unsigned: 0 to 4294967295 unsigned: 0 to 4294967295 float float Floating point Floating point number number 4 bytes 4 bytes 3.4e +/- 38 (7 digits) 3.4e +/- 38 (7 digits) double double Double precision Double precision floating point number floating point number 8 bytes 8 bytes 1.7e +/- 308 (15 digits) 1.7e +/- 308 (15 digits) long long double double Long double Long double precision floating precision floating point number point number 8 bytes 8 bytes 1.7e +/- 308 (15 digits) 1.7e +/- 308 (15 digits) Data types  $,&& $,&&   &EF= &EF=   EGHI= &J&KL EGHI= &J&KL M M '3E)K= '3E)K= ' ' NEBKK= NEBKK= 24&&+"'+ 24&&+"'+ 33 33 ' ' J&EF= J&EF= 34/J&O'<C"J&= 34/J&O'<C"J&= ' '     J&EP= J&EP= 34/J&O'<C"J&= 34/J&O'<C"J&= Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value = -1 value = -1 11111111 11111111 255 255 unsigned char unsigned char value = 300 value = 300 00101100 00101100 44 44 overflow overflow [...]...Variable types  Local variable Local variables are declared within the body of a function, and can only be used within that function  Static variable Another class of local variable is the static type It is specified by the keyword static in the variable declaration The most striking difference from a non-static local variable is, a static variable is not destroyed on exit from the function  Global... escape sequence \a \b \f \n \r \t \v alert (bell) character backspace formfeed newline carriage return horizontal tab vertical tab \\ \? \’ \” \000 \xhh backslash question mark single quote double quote octal number hexadecimal number Arithmetic Operations Arithmetic Assignment Operators Increment and Decrement Operators awkward easy easiest x = x+1; x += 1 x++ x = x-1; x -= 1 x Example Arithmetic... 5200; char * name = “Mike”; printf(“%s ‘s ID is %d \n”, name, stud_id);  Format Identifiers %d decimal integers %x hex integer %c character %f float and double number %s string %p pointer  How to specify display space for a variable ? printf(“The student id is %5d \n”, stud_id); The value of stud_id will occupy 5 characters space in the print-out  Why “\n” It introduces a new line on the terminal screen... the correct answers? Conditionals  if statement Three basic formats, if (expression){ statement … } if (expression) { statement … }else{ statement … } if (expression) { statement… } else if (expression) { statement… } else{ statement… }  An example if(score >= 90){ a_cnt ++; }else if(score >= 80){ b_cnt++; }else if(score >= 70){ c_ cnt++; }else if (score>= 60){ d_cnt++ }else{ f_cnt++ }  The switch... global variable declaration looks normal, but is located outside any of the program's functions So it is accessible to all functions  An example int global = 10; //global variable int func (int x) { static int stat_var; //static local variable int temp; //(normal) local variable int name[50]; //(normal) local variable …… } Variable Definition vs Declaration Definition Tell the compiler about the... Tell the compiler about the variable: its type and name, as well as allocated a memory cell for the variable Declaration Describe information ``about'' the variable, doesn’t allocate memory cell for the variable http://www-ee.eng.hawaii.edu/~tep/EE150/book/chap14/subsection2.1.1.4.html printf() The printf() function can be instructed to print integers, floats and string properly  The general syntax is... B? Is A less than or equal to B? Don’t be confused && and || have different meanings from & and | & and | are bitwise operators Short circuiting  Short circuiting means that we don't evaluate the second part of an AND or OR unless we really need to Some practices Please compute the value of the following logical expressions? int i = 10; int j = 15; int k = 15; int m = 0; if( i < j && j < k) if( i !=... product = i * j; // 150 int quotient = j / i; // 1 int residual = j % i; // 5 i++; //Increase by 1 i ; //Decrease by 1   Comparing them int i = 10; int j = 15; float k = 15.0; j/i=? j%i=? k/i=? k%i=? The Answer j / i = 1; j % i = 5; k / i = 1.5; k % i It is illegal Note: For %, the operands can only be integers  Logical Operations  What is “true” and “false” in C In C, there is no specific data... represent “true” and “false” C uses value “0” to represent “false”, and uses non-zero value to stand for “true”  Logical Operators A && B => A and B A || B => A or B A == B => Is A equal to B? A != B => Is A not equal to B? A >B A >= B Is A greater than B? Is A greater than or equal to B? A => => Is A less than B? Is A less than or equal to B? Don’t be confused && and || have different... The switch statement switch (expression)  { case item1: statement; break; case item2: statement; break; default: statement; break; } Loops  for statement for (expression1; expression2; expression3){ statement… } expression1 initializes; expression2 is the terminate test; expression3 is the modifier;  An example int x; for (x=0; x . Program  +3&? +3&? @ @ &&)A&& &&)A&& gcc gcc compiling command compiling command hello .c hello .c source file source file hello hello compiler-generated executable file compiler-generated executable file Note: the. Outline  / / &&#&'0 &&#&'0 1 1  23 23 $,& $,&  34 34  5 5 6&3 6&3  *'& *'&  63 63  527 527  Pointers Pointers  Functions Functions  Command-Line Argument Command-Line Argument  Data Structure Data Structure  Memory Allocation Memory Allocation  Programming Tips Programming Tips  C C v v s. C+ + s Program 52') 52') Command Command Location Location Comment Comment ls ls /bin/ls /bin/ls provided by the provided by the system system ls ls current directory current directory virus virus Name Name Description Description Size* Size* Range* Range* char char Character

Ngày đăng: 23/10/2014, 15:07