Skip to main content

More_Inferences_in_C++(1Aug'19-16thOct'19)

Programmer/Tester- Aditya Gupta (aka Techy3.5 :-)

I used g++ to compile these on linux

1. Type casting of floating point numeral to integer (either automatic or by type casting)-
Conversion of floating point to integer is by taking the GIF (Greatest Integer Funtion) and NOT ROUNDING OFF

Greatest Integer Function(Maths,12th) it gives the integer part when the original number is written as integer+fractional part (where fractional part is less than 1 but positive)

2. NEsted MultiLine Comment [actually wont work as expected?!]
if you do this in C++....
/*  blah /*blah blah blah*/ blah*/
the last blah will be visible to the compiler!!! (Inference- Multiline-Comment ends whenever */ is encountered, no matter where!!)NEsted MultiLine Comment

3. Size of various types (of C++) on 'Linux' Mint - (and same as in later versions of windows, and most PCs nowadays)
char - 1
int - 4  (and: short - 2, unsigned long - 8)
long long - 8
float - 4
double - 8 (and: long double - 16)

NOTE- All sizes in bytes

4. The Control Characters (for eg. newline and tab) require a BACKSLASH
   *Seems obvious, but in cases where i dont do programming for some time, i get confused in the slash to be used... though i get it from the different colour in vim editor

5. Conversion of int* to char shows the error 'conversion of int* to char loses precision'
   While conversion of int* to char* is OK.

6. Entering Out Of Range value in a declared variable...
   In such a case, the variable will hold the maximum value it can hold, for example an int when given a very long number, it stores 2147483647 (ie. (2^31)-1).

7. Maximum value of unsigned variable = maximum value of signed variable
   The thing I got surprised at was that, it stored the same number in both cases, let it be signed or unsigned! (size of int on my system is 4 bytes(32 bits) as on most)

//THINGS About ARRAYS
8. We cannot copy an array into another array of same type and same size by just simple assignment.
for eg. A=B; //where B is initialised, and both are in arrays of size 3
It shows 'error: invalid array assignment'

9. We can use initializer list to give values to elements of array, only when it is declared, after that we can't use it, it will show an error then...
for eg. int A[3]={34,5,4}; //is OK
BUT,    int A[3];
A={34,5,4};
The second one shows 'error: invalid array assignment'
//XX XX XX//
10. I have posted this observation in another post also, ie. "Default return type of any function is int, nowadays atleast... and that main() be default returns 0, if return statement not typed"
    But, I still expected it to be raised as a 'Warning' by the compiler
    So... nor gcc nor g++ raises even any warning about missing return statement.

11. Entering Characters and MIXED data types when asked for an integer...
Case 1: Entered a,b,c etc. -> int variable stores 0
Case 2: Entered string Hi,Bye,etc -> int variable stores 0
//ACTUALLY, It ignores any succeeding character or integer, if first thing entered wasnt a number, but was an integer... So, for it Hi is same as H only!
Case 3: Entered 454d : Stores 454
//Ignores any other data type, in case first entered was some number, then it will just take any character as end of number (for eg. 'Enter key' is also a character)
Case 4: Entered 454d45: Stores 454
//Since it took any further character to be end of number (NOTE: In all cases i am saying number ended, actually the input hasnt ended, but i mean it will be treated like that later...)
Case 5: Enter f344, fsg45, d454fg, etc (ie. character at start) -> int variable stores 0
//Maybe since it just treated b, which will cause its value 0, and since being a character further things weren't even accounted for

12. 'long' and 'long long' have same size, ie. 8 bytes in g++ compiler
   But, 'double' has 8 bytes, while 'long double' has 16 bytes

Comments

Popular posts from this blog

AP, GP Generator (using Linked List)

//Future advancements-> add save as file, in text or in a database option (for the sequences) //                      add more series to this //Programmer - Aditya Gupta #include<iostream> #include<stdlib.h> #include<math.h> //#include<fstream> //reqd when save support is added to this! using namespace std; struct Node{ int data; Node* next; }; void inline title(void){ system("cls"); cout<<"\tNAMASTE!...\n\nProgram-AP,GP Generator\nPrograammer - MR. ADITYA GUPTA (TEChY 3)\n\n"; } class List{ //NOTE: I HAVE REMOVED ALL COMMENTS, REFER TO THE LINKED LIST TEMPLATE FOR DETAILED COMMENTS             //here only generator specific comments are there, and that too which are not in template Node *top,*rear; public: void Push(int dat); int isempty(); int search(int dat); int noofelements(); void Pop(); //CAUTION- will decrease number of terms by 1 each time it runs, and pushing the last element again will be same

Decimal to Binary (wo Array) - A DIFFERENT APPROACH (the way i do it)

 /* * Title - Decimal to Binary Converter - My Way  *  * Programmer - Aditya Gupta (@Techy15)  *  * */ /*The more general approach is to take remainders after dividing by 2, but this one is by comparison*/ /*Compiled with g++ on Zorin OS (Linux)*/ #include<iostream> using namespace std; long pow(int a, int power){         if(power==0) return 1;         else if(power==1) return a;         else return a*pow(a,power-1);         } int main(){         int dec,bin=0,wtemp1,wtemp2,i=0;         cin>>dec; wtemp1=wtemp2=dec;         while(wtemp1>0){                         while(wtemp1>=pow(2,i)){                                  i++;                         }                 bin+=pow(10,i-1);                 wtemp1-=pow(2,i-1);                 i=0;            }         cout<<bin;         return 0; } ~