Review C/C++ Programming Language BASIC DATA TYPES Review C/C++ Programming Language Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department
c Datatype short int 2
Display The Contents Of A Variable Display The Contents Of A Variable Create A Variable short int Age = 21; Display The Contents Of A Variable printf("Age = %hi\n", Age); Display The Contents Of A Variable cout << "Age = " << Age << endl << endl; The Symbolic Name For This Container is Age 3
We can use %d, %hi, or %i to display a short int Display The Size (in bytes) Of The Short Int Container With This Compiler short int Age = 21; printf("sizeof(Age) = %ld\n\n", sizeof(Age)); Display The Largest Value That Can Be Placed In The Short Int Container printf("SHRT_MAX = %i\n\n", SHRT_MAX); Display The Smallest Value That Can Be Placed In The Short Int Container printf("SHRT_MIN = %d\n\n", SHRT_MIN); We can use %d, %hi, or %i to display a short int 4
We can use %d for decimal , %x for hexadecimal, or %o for octal Display The Address Where The Variable Is Stored In Memory At This Instant In Time Our Numbers Will Generally Be Different) short int A = 1, B = 2, C = 3; printf("&A = %ld = %x = %o\n\n", &A, &A, &A); printf("&B = %ld = %x = %o\n\n", &B, &B, &B); printf("&C = %ld = %x = %o\n\n", &C, &C, &C); We can use %d for decimal , %x for hexadecimal, or %o for octal 5
Hopefully You Had This In Computer Science I Bad Things Happen When We Try To Insert Numbers That Are Too Large Or Too Small Into A Container short int Age = 21; Age = 32766; printf("[32766] Age = %hi\n\n", Age); Age++; printf("[23767] Age = %i\n\n", Age); printf("[32768] Age = %d\n\n", Age); Knowing Binary Arithmetic & How Integers Are Stored In Memory Are Required If One Is To Really Understand Why This Goes Negative Hopefully You Had This In Computer Science I 6
Prompt The User For The Age & Fill Interactively With Scanf short int Age = 21; printf("Enter Age: "); scanf("%hi", &Age); printf("Age = %hi\n\n", Age); 7
Prompt The User For The Age & Fill Interactively With Cin short int Age = 21; cout << "Enter Age: "; cin >> Age; cout << "Age = " << Age << endl << endl; 8
c Datatype int 9
Display The Contents Of A Variable Display The Contents Of A Variable Create A Variable int Quantity = 50000; Display The Contents Of A Variable printf("Quantity = %d\n", Quantity); Display The Contents Of A Variable cout << "Quantity = " << Quantity << "\n\n"; The Symbolic Name For This Container is Quantity 10
Display The Size (in bytes) Of The Int Container With This Compiler Quantity = 50000; printf("sizeof(Quantity) = %ld\n\n", sizeof(Quantity)); Display The Largest Value That Can Be Placed In The Int Container printf("INT_MAX = %i\n\n", INT_MAX); Display The Smallest Value That Can Be Placed In The Int Container printf("INT_MIN = %ld\n\n", INT_MIN); We can use %d, %ld, or %i to display a int 11
We can use %d for decimal , %x for hexadecimal, or %o for octal Display The Address Where The Variable Is Stored In Memory At This Instant In Time Our Numbers Will Generally Be Different) int D = 1, E = 2, F = 3; printf("&D = %ld = %x = %o\n\n", &D, &D, &D); printf("&E = %ld = %x = %o\n\n", &E, &E, &E); printf("&F = %ld = %x = %o\n\n", &F, &F, &F); We can use %d for decimal , %x for hexadecimal, or %o for octal 12
Integer Datatypes - C/C++a Minimal Standard Range / Precision Visual C++ Compiler int 2 bytes -32,768 to 32, 767 4 bytes -2,147,483,648 to 2,147,483,647 Knowing The Minimal Standards help us when using Microcontrollers and other 32 bit compiler applications. Knowing the range will help us make better variable choices If the variable is the number of students at a university an int is a good choice. Know -2 billion to + 2 billion 13 #
Hopefully You Had This In Computer Science I Bad Things Happen When We Try To Insert Numbers That Are Too Large Or Too Small Into A Container int Quantity = 50000; Quantity = 2147483646; printf("[2147483646] Quantity = %i\n\n", Quantity); Quantity++; printf("[2147483647] Quantity = %d\n\n", Quantity); printf("[2147483648] Quantity = %ld\n\n", Quantity); Knowing Binary Arithmetic & How Integers Are Stored In Memory Are Required If One Is To Really Understand Why This Goes Negative Hopefully You Had This In Computer Science I 14
Prompt The User For The Age & Fill Interactively with Scanf Quantity = 50000; printf("Enter Quantity: "); scanf("%d", & Quantity); printf("Quantity = %d\n\n", Quantity); 15
Prompt The User For The Age & Fill Interactively with Cin Quantity = 50000; cout << "Enter Quantity: "; cin >> Quantity; cout << "Quantity = " << Quantity << endl; 16
c Datatype long int 17
Display The Contents Of A Variable Display The Contents Of A Variable Create A Variable long int NoAutos = 50000; Display The Contents Of A Variable printf("NoAutos = %ld\n", NoAutos); Display The Contents Of A Variable cout << "NoAutos = " << NoAutos << "\n\n"; The Symbolic Name For This Container is NoAutos 18
long int NoAutos = 50000; printf(" LONG_MAX = %i\n\n", LONG_MAX); Display The Size (in bytes) Of The long Int Container With This Compiler long int NoAutos = 50000; printf("sizeof(NoAutos) = %ld\n\n", sizeof(NoAutos)); Display The Largest Value That Can Be Placed In The Int Container printf(" LONG_MAX = %i\n\n", LONG_MAX); Display The Smallest Value That Can Be Placed In The Int Container printf(" LONG_MIN = %ld\n\n", LONG_MIN); We can use %d, %ld, or %i to display a long int 19
We can use %d for decimal , %x for hexadecimal, or %o for octal Display The Address Where The Variable Is Stored In Memory At This Instant In Time Our Numbers Will Generally Be Different) long int G = 1, H = 2, I = 3; printf("&G = %ld = %x = %o\n\n", &G, &G, &G); printf("&H = %ld = %x = %o\n\n", &H, &H, &H); printf("&I = %ld = %x = %o\n\n", &I, &I, &I); We can use %d for decimal , %x for hexadecimal, or %o for octal 20
Integer Datatypes - C/C++a Minimal Standard Range / Precision Visual C++ Compiler long int 4 bytes -2,147,483,648 to 2,147,483,647 Knowing The Minimal Standards help us when using Microcontrollers and other 32 bit compiler applications. Knowing the range will help us make better variable choices If the variable is the population of a state an int is a good choice. Know -2 billion to + 2 billion 21 #
Hopefully You Had This In Computer Science I Bad Things Happen When We Try To Insert Numbers That Are Too Large Or Too Small Into A Container int NoAutos = 50000; NoAutos = 2147483646; printf("[2147483646] NoAutos = %i\n\n", NoAutos); NoAutos++; printf("[2147483647] NoAutos = %d\n\n", NoAutos); printf("[2147483648] NoAutos = %ld\n\n", NoAutos); Knowing Binary Arithmetic & How Integers Are Stored In Memory Are Required If One Is To Really Understand Why This Goes Negative Hopefully You Had This In Computer Science I 22
Prompt The User For The Age & Fill Interactively Using Scanf long int NoAutos = 50000; printf("Enter NoAutos : "); scanf("%d", & NoAutos ); printf("NoAutos = %d\n\n", NoAutos ); 23
Prompt The User For The Age & Fill Interactively Using Cin long int NoAutos = 50000; cout << "Enter NoAutos : "; cin >> NoAutos; cout << "NoAutos = " << NoAutos << endl; 24
c Buffered Files 25
Good Idea To Flush stdin (keyboard) Buffer Before & After Scanf Many C++ Compilers, including older Visual Studio Versions, Require The Programmer To Flush The Keyboard Buffer Before & After Scanf long int NoAutos = 50000; char First[15]; flush_stream(stdin); // or fflush(stdin); printf("Enter NoAutos : "); scanf("%d", &NoAutos); flush_stream(stdin); printf ("Enter First Name : "); scanf("%s", &First); printf("\nNoAutos = %d\n", NoAutos); printf("First = %s\n\n", First); I Have Not Needed To Flush The Buffers In Preliminary Testing Of Visual Studio 2017 26
More On Formatted Output c More On Formatted Output 27
Display Variables (Right Justified) In A 6 Character Field Use These Variables long J = 50000; short K = 21; Display Variables (Right Justified) In A 6 Character Field printf(" 123456 \n"); printf(" ------\n"); printf("J = *%6ld*\n", J); printf("K = *%6hi*\n", K); 28
Display Variables (Let Justified) In A 6 Character Field Use These Variables long J = 50000; short K = 21; Display Variables (Let Justified) In A 6 Character Field printf(" 123456 \n"); printf(" ------\n"); printf("J = *%-6ld*\n", J); printf("K = *%-6d*\n", K); What Happens If Field Is Not Big Enough? printf(" 123456 \n"); printf(" ------\n"); printf("J = *%-2ld*\n", J); printf("J = *%2ld*\n", J); The Format May Get Messed Up But It Will Display The Data! 29
c Datatype __int64 Not On Exam/Quiz 30
Need A Variable For # Grains Of Sand On The Earth? New __int64 puts ("---------------- Start Of Main -----------------\n"); __int64 N = 9223372036854775807; printf("sizeof(__int64) = %ld\n", sizeof(__int64)); printf("sizeof(N) = %ld\n", sizeof(N)); printf("N = %I64d\n", N); printf("INT64_MAX = %I64d\n", INT64_MAX); printf("INT64_MIN = %I64d\n\n", INT64_MIN); N++; puts ("------------------- End Of Main ------------------\n"); Will Not Be On Exam Or Quiz 9,223,372,036,854,775,807 Most 19 Digit Nos 31
c Datatype float 32
Display The Contents Of A Variable Display The Contents Of A Variable Create A Variable float Value = 1.2345; Display The Contents Of A Variable printf("Value = %f\n", Value); Display The Contents Of A Variable cout << " Value = " << Value << "\n\n"; The Symbolic Name For This Container is Value 33
printf("Value = %f\n\n", Value); Value = 2.0 / 3.0; Display The Size (in bytes) Of The Float Container With This Compiler float Value = 1.2345; printf("sizeof(Value) = %ld\n\n", sizeof(Value)); If You Understand How Floating Point Numbers Are Stored In Memory, You Will Remember That They Are Approximate and Not Exact Value = 1.0 / 3.0; printf("Value = %f\n\n", Value); Value = 2.0 / 3.0; We can use %f to display a short float 34
float Will Have At Least 6 Digits Of Precision printf("N[1.23456789012345] = %f\n\n", N); N = 12.3456789012345; printf("N[12.3456789012345] = %f\n\n", N); N = 123.456789012345; printf("N[123.456789012345] = %f\n\n", N); N = 1234.56789012345; printf("N[1234.56789012345] = %f\n\n", N); 35
We can use %d for decimal , %x for hexadecimal, or %o for octal Display The Address Where The Variable Is Stored In Memory At This Instant In Time Our Numbers Will Generally Be Different) float O = 1.1, P = 2.2, Q = 3.3; printf("&O = %ld = %x = %o\n\n", &O, &O, &O); printf("&P = %ld = %x = %o\n\n", &P, &P, &P); printf("&Q = %ld = %x = %o\n\n", &Q, &Q, &Q); We can use %d for decimal , %x for hexadecimal, or %o for octal 36
Fractional Datatypes - C/C++a Minimal Standard Range / Precision Visual C++ Compiler float 4 bytes 6+ digits Know 6+ digits of precision 37 #
float i = 12.3456; printf(" 12345678 \n"); printf(" -------- \n"); Display Right Justified In A 8 Character Field With 2 Digits To The Right Of Decimal Point float i = 12.3456; printf(" 12345678 \n"); printf(" -------- \n"); printf("i = %8.2f\n\n", i); Display Left Justified In A 8 Character Field With 3 Digits To The Right Of Decimal Point float i = 12.3456; printf(" 12345678 \n"); printf(" -------- \n"); printf("i = %-8.3f\n\n", i); 38
Display With 2 Digits To The Right Of Decimal Point float i = 12.3456; printf("i = %.2f\n\n", i); 39
Prompt The User For The Value & Fill Interactively Using Scanf float Value = 1.2345; printf("Enter Value: "); scanf("%f", &Value); printf("Value = %f\n\n", Value); 40
Prompt The User For The Value & Fill Interactively Using Cin float Value = 1.2345; cout << "Enter Value: "; cin >> Value; cout << "Value = " << Value << endl; 41
c Datatype double 42
Display The Contents Of A Variable Display The Contents Of A Variable Create A Variable double Gross = 100.2345; Display The Contents Of A Variable printf(" Gross = %lf\n", Gross ); Display The Contents Of A Variable cout << " Gross = " << Gross << "\n\n"; The Symbolic Name For This Container is Gross 43
printf(" Gross = %lf\n\n", Gross ); Gross = 2.0 / 3.0; Display The Size (in bytes) Of The Double Container With This Compiler double Gross = 100.2345; printf("sizeof(Gross) = %ld\n\n", sizeof(Gross)); If You Understand How Floating Point Numbers Are Stored In Memory, You Will Remember That They Are Approximate and Not Exact Gross = 1.0 / 3.0; printf(" Gross = %lf\n\n", Gross ); Gross = 2.0 / 3.0; printf("Gross = %lf\n\n", Gross ); We can use %lf to display a double 44
printf("R[1.23456789012345] = %lf\n\n", R); double Will Have At Least 15 Digits Of Precision double R; R = 1.23456789012345; printf("R[1.23456789012345] = %lf\n\n", R); You Might Ask Where Is The Precision Difference Between float & double? It Is There, But… We Seldom Want To See That Degree Of Precision printf("R[1.23456789012345] = %.15lf\n\n", R); R = 1234.56789012345; printf("R[1234.56789012345] = %20.15lf\n\n", R); 45
We can use %d for decimal , %x for hexadecimal, or %o for octal Display The Address Where The Variable Is Stored In Memory At This Instant In Time Our Numbers Will Generally Be Different) double S = 1.1, T = 2.2, U = 3.3; printf("&S = %ld = %x = %o\n\n", &S, &S, &S); printf("&T = %ld = %x = %o\n\n", &T, &T, &T); printf("&U = %ld = %x = %o\n\n", &U, &U, &U); We can use %d for decimal , %x for hexadecimal, or %o for octal 46
Fractional Datatypes - C/C++a Minimal Standard Range / Precision Visual C++ Compiler double 8 bytes 15+ digits Know 15+ digits of precision 47 #
Prompt The User For The Value & Fill Interactively Using Scanf double Gross = 100.2345; printf("Enter Gross: "); scanf("%lf", & Gross); printf("Gross = %lf\n\n", Gross); 48
Prompt The User For The Value & Fill Interactively Using Cin double Gross = 100.2345; cout << "Enter Gross: "; cin >> Gross; cout << "Gross = " << Gross << endl; 49
c Datatype long double 50
Note That The Size Is 8 Bytes - Same As Double ? Create A Variable long double V = 1.2345678901234567890123; printf("V = %lf\n\n", V); cout << "V = " << V << "\n\n"; printf("sizeof(V) = %ld\n\n", sizeof(V)); printf("V[1.2345678901234567890123] = %.20lf\n\n", V); Note That The Size Is 8 Bytes - Same As Double ? The Long Double - 12 bytes - Was Included In Many Of The Previous Versions Of Visual Studio - It Is Still Native In The Built-In Functions Many Compilers Do Not Support Long Double It Is No Longer Native To Visual Studio 2017 51
c Datatype char 52
Display The Contents Of A Variable Display The Contents Of A Variable Create A Variable char Choice = 'Y'; Display The Contents Of A Variable printf("Choice = %c\n", Choice); Display The Contents Of A Variable cout << "Choice = " << Choice<< "\n\n"; The Symbolic Name For This Container is Choice 53
char Choice = 'Y'; printf("sizeof(Choice) = %ld\n\n", sizeof(Choice)); Display The Size (in bytes) Of The Short Int Container With This Compiler char Choice = 'Y'; printf("sizeof(Choice) = %ld\n\n", sizeof(Choice)); Display The Address Where The Variable Is Stored In Memory At This Instant In Time Our Numbers Will Generally Be Different) char W = '/', X = 'A', Y = 'a'; printf("&W = %ld = %x = %o\n\n", &W, &W, &W); printf("&X = %ld = %x = %o\n\n", &X, &X, &X); printf("&Y = %ld = %x = %o\n\n", &Y, &Y, &Y); 54
Prompt The User For The Value & Fill Interactively Using Scanf char Choice = 'Y' printf("Enter Choice {Y/N}: "); scanf("%c", & Choice); printf("Choice = %c\n\n", Choice); 55
Prompt The User For The Value & Fill Interactively Using Cin char Choice = 'Y' cout << "Enter Choice {Y/N}: "; cin >> Choice; cout << "Choice = " << Choice << endl; 56
c Datatype bool 57
Display The Contents Of A Variable Display The Contents Of A Variable Create A Variable bool Valid = true; Display The Contents Of A Variable if (Valid) printf("Valid = true\n\n"); else printf("Valid = false\n\n"); Display The Contents Of A Variable cout << "Valid = " << Valid << "\n\n"; 58
Display The Contents Of A Variable Display The Contents Of A Variable Create A Variable bool Valid = false; Display The Contents Of A Variable if (Valid) printf("Valid = true\n\n"); else printf("Valid = false\n\n"); Display The Contents Of A Variable cout << "Valid = " << Valid << "\n\n"; 59
Data Entry Into Booleans Is A Little Tougher Valid = true; char ValidCh; printf("Enter Valid {Y/N}: "); scanf("%c", & ValidCh); if (ValidCh == 'Y') Valid = true; else if (ValidCh == 'N') Valid = false; else puts("Invalid Choice"); cout << "Valid = " << Valid << "\n\n"; 60
c Container Capacities 61
How can I find out the limits on a given compiler? # include <stdio.h> printf ("sizeof(short int) = %ld\n", sizeof(short int)); printf ("sizeof(int) = %ld\n", sizeof(int)); printf ("sizeof(long int) = %ld\n", sizeof(long int)); printf ("sizeof(float) = %ld\n", sizeof(float)); printf ("sizeof(double) = %ld\n", sizeof(double)); printf ("sizeof(long double) = %ld\n", sizeof(long double)); printf ("sizeof(char) = %ld\n", sizeof(char)); printf ("sizeof(bool) = %ld\n", sizeof(bool)); 62
Some Way To Verify The Range ? # include <limits.h> #define SHRT_MIN (-32768) /* minimum (signed) short value */ #define SHRT_MAX 32767 /* maximum (signed) short value */ #define USHRT_MAX 0xffff /* maximum unsigned short value */ #define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */ #define INT_MAX 2147483647 /* maximum (signed) int value */ #define UINT_MAX 0xffffffff /* maximum unsigned int value */ #define LONG_MIN (-2147483647L - 1) /* minimum (signed) long value */ #define LONG_MAX 2147483647L /* maximum (signed) long value */ #define ULONG_MAX 0xffffffffUL /* maximum unsigned long value */ etc. 63
All Other Items In This Presentation Are The Same For Both C & C++ Special Note cin & cout are C++ Functions All Other Items In This Presentation Are The Same For Both C & C++ 64