Operator Precedence in C & CPP

Operator Precedence Chart Operator Type Operator Associativity 1. Primary Expression Operators () [] . -> expr++ expr– left-to-right 2. Unary Operators * & + - ! ~ ++expr –expr right-to-left (typecast) sizeof() 3. Binary Operators * / % left-to-right + - » « < > <= >= == != & ^ | && || 4. Ternary Operator ?: right-to-left 5. Assignment Operators = += -= *= /= %= »= right-to-left [Read More]

How does throwing and catching exceptions differ from using setjmp and longjmp?

The throw operation calls the destructors for automatic objects instantiated since entry to the try block. Exceptions are in the mainstream of C++ now, so most programmers, if they are familiar with setjmp and longjmp, should know the difference. Both idioms return a program from the nested depths of multiple function calls to a defined position higher in the program. The program stack is “unwound” so that the state of the program with respect to function calls and pushed arguments is restored as if the calls had not been made. [Read More]

Stack ADT

Stacks are an elementary Data Structure where all interaction with the data is done through the looking at the first element. Stacks are last in first out (LIFO) data structures. It is named on the basis of how the elements are removed from it. Opposite to stack is queue, in which the last element is removed, the element which was most old item, and hence it is FIFO or first in first out data structure. [Read More]

Miscellaneous cases with printf

**Strings
**
1. printf(5+Fascimile);
O
mile
2. printf(5+"Good Morning”);
O
Morning
3. printf("%c”, abcdefg[4]);
O.
e

Strings

static char s[]= “Oinks Grunts and Guffaws”; Now we see some results on this string: 1. *(&s[2])=s[2]=*(s+2)=*(2+s ) = n 2. strlen(s) = length of the string so strlen(s)+s= last character of the string = \0 , whose ascii value is 0. So when %c is used , \0 is printed and when %d is used 0 is printed. 3. when is printed with %d we get the base address of the string. [Read More]

Access in cpp classes

When a base class is inherited as private, all public and protected members of that class become private members of the derived class. However, in certain circumstances, you may want to restore one or more inherited members to their original access specification. For example, **you might want to grant certain public members of the base class public status in the derived class even though the base class is inherited as private. [Read More]

C preprocessor

The C preprocessor (cpp) is the preprocessor for the C programming language. In many C implementations, it is a separate program invoked by the compiler as the first part of translation. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if). The language of preprocessor directives is not strictly specific to the grammar of C, so the C preprocessor can also be used independently to process other types of files. [Read More]

Complicated Declaration

Function Declaration

Function

Returning

Accepting

int *f();

int ptr

any number of arg in c and no arg in cpp

char(*(*f())[10])();

pointer to the array of pointers to function

int (*(*f())[10]) ()

pointer to the array of 10 pointers to function that returns integer

Pointer

Expression

p is the pointer to

int *p;

iint **p;

int (*p)();

function that returns the integer

int (*p)[30]

array of 30 int

Making a String class in cpp

Program to manage strings(Source Code) // Program Ch03pr01 // Program to manage strings #include <string.h\> #include <iostream.h\> #include <conio.h\> const int MAX \= 10 ; class string { private : char strMAXMAX ; public : string( ) {} string ( char \*s ) {strcpy(str,s);} int xstrlen( ) ; static void xstrcpy ( string &amp;t, string &amp;s ) ; static void xstrcat ( string &amp;t, string &amp;s ) ; static int xstrcmp ( string &amp;s1, string &amp;s2 ) ; void show( ) ; } ; // finds the length of the string int string::xstrlen( ) { int l \= 0 ; char \*s \= str ; while ( \*s ) { l++ ; s++ ; } return l ; } // copies source string s to the target string t void string :: xstrcpy ( string &amp;t, string &amp;s ) { char \*s1 \= t. [Read More]

Program to Add two polynomials

Program to Add two polynomials(Source Code) // Program Ch02pr05 // Program to Add two polynomials #include <iostream\> #include <conio.h\> using namespace std; const int MAX \= 10 ; class poly { private : struct term { int coeff ; int exp ; } tMAXMAX ; int noofterms ; public : poly( ) ; void polyappend ( int c, int e ) ; void polyadd ( poly &amp;p1, poly &amp;p2 ) ; void display( ) ; } ; // initializes data members of class poly poly :: poly( ) { noofterms \= 0 ; for ( int i \= 0 ; i &lt; MAX ; i++ ) { tii. [Read More]