Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review C/C++ Programming Language

Similar presentations


Presentation on theme: "Review C/C++ Programming Language"— Presentation transcript:

1 Review C/C++ Programming Language
BASIC DATA TYPES Review C/C++ Programming Language Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department

2 c Datatype short int 2

3 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

4 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

5 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

6 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

7 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

8 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

9 c Datatype int 9

10 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

11 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

12 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

13 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 #

14 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 = ; printf("[ ] Quantity = %i\n\n", Quantity); Quantity++; printf("[ ] Quantity = %d\n\n", Quantity); printf("[ ] 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

15 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

16 Prompt The User For The Age & Fill Interactively with Cin
Quantity = 50000; cout << "Enter Quantity: "; cin >> Quantity; cout << "Quantity = " << Quantity << endl; 16

17 c Datatype long int 17

18 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

19 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

20 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

21 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 #

22 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 = ; printf("[ ] NoAutos = %i\n\n", NoAutos); NoAutos++; printf("[ ] NoAutos = %d\n\n", NoAutos); printf("[ ] 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

23 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

24 Prompt The User For The Age & Fill Interactively Using Cin
long int NoAutos = 50000; cout << "Enter NoAutos : "; cin >> NoAutos; cout << "NoAutos = " << NoAutos << endl; 24

25 c Buffered Files 25

26 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

27 More On Formatted Output
c More On Formatted Output 27

28 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(" \n"); printf(" \n"); printf("J = *%6ld*\n", J); printf("K = *%6hi*\n", K); 28

29 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(" \n"); printf(" \n"); printf("J = *%-6ld*\n", J); printf("K = *%-6d*\n", K); What Happens If Field Is Not Big Enough? printf(" \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

30 c Datatype __int64 Not On Exam/Quiz 30

31 Need A Variable For # Grains Of Sand On The Earth?
New __int64 puts (" Start Of Main \n"); __int64 N = ; 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

32 c Datatype float 32

33 Display The Contents Of A Variable Display The Contents Of A Variable
Create A Variable float Value = ; 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

34 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 = ; 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

35 float Will Have At Least 6 Digits Of Precision
printf("N[ ] = %f\n\n", N); N = ; printf("N[ ] = %f\n\n", N); N = ; printf("N[ ] = %f\n\n", N); N = ; printf("N[ ] = %f\n\n", N); 35

36 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

37 Fractional Datatypes - C/C++a
Minimal Standard Range / Precision Visual C++ Compiler float 4 bytes 6+ digits Know 6+ digits of precision 37 #

38 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 = ; printf(" \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 = ; printf(" \n"); printf(" \n"); printf("i = %-8.3f\n\n", i); 38

39 Display With 2 Digits To The Right Of Decimal Point
float i = ; printf("i = %.2f\n\n", i); 39

40 Prompt The User For The Value & Fill Interactively Using Scanf
float Value = ; printf("Enter Value: "); scanf("%f", &Value); printf("Value = %f\n\n", Value); 40

41 Prompt The User For The Value & Fill Interactively Using Cin
float Value = ; cout << "Enter Value: "; cin >> Value; cout << "Value = " << Value << endl; 41

42 c Datatype double 42

43 Display The Contents Of A Variable Display The Contents Of A Variable
Create A Variable double Gross = ; 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

44 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 = ; 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

45 printf("R[1.23456789012345] = %lf\n\n", R);
double Will Have At Least 15 Digits Of Precision double R; R = ; printf("R[ ] = %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[ ] = %.15lf\n\n", R); R = ; printf("R[ ] = %20.15lf\n\n", R); 45

46 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

47 Fractional Datatypes - C/C++a
Minimal Standard Range / Precision Visual C++ Compiler double 8 bytes 15+ digits Know 15+ digits of precision 47 #

48 Prompt The User For The Value & Fill Interactively Using Scanf
double Gross = ; printf("Enter Gross: "); scanf("%lf", & Gross); printf("Gross = %lf\n\n", Gross); 48

49 Prompt The User For The Value & Fill Interactively Using Cin
double Gross = ; cout << "Enter Gross: "; cin >> Gross; cout << "Gross = " << Gross << endl; 49

50 c Datatype long double 50

51 Note That The Size Is 8 Bytes - Same As Double ?
Create A Variable long double V = ; printf("V = %lf\n\n", V); cout << "V = " << V << "\n\n"; printf("sizeof(V) = %ld\n\n", sizeof(V)); printf("V[ ] = %.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

52 c Datatype char 52

53 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

54 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

55 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

56 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

57 c Datatype bool 57

58 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

59 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

60 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

61 c Container Capacities 61

62 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

63 Some Way To Verify The Range ?
# include <limits.h> #define SHRT_MIN (-32768) /* minimum (signed) short value */ #define SHRT_MAX /* maximum (signed) short value */ #define USHRT_MAX 0xffff /* maximum unsigned short value */ #define INT_MIN ( ) /* minimum (signed) int value */ #define INT_MAX /* maximum (signed) int value */ #define UINT_MAX xffffffff /* maximum unsigned int value */ #define LONG_MIN ( L - 1) /* minimum (signed) long value */ #define LONG_MAX L /* maximum (signed) long value */ #define ULONG_MAX 0xffffffffUL /* maximum unsigned long value */ etc. 63

64 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


Download ppt "Review C/C++ Programming Language"

Similar presentations


Ads by Google