Virtual Functions Design Patterns Important C/C++ Basic Concepts Program startup and termination are facilitated by using two functions: main and exit. Other startup and termination code may be executed. The arguments in the prototype int main( int argc[ , charContinue reading… Virtual Functions (C++ Concepts)
Category: Programming
Sum two numbers using bits
int main(int argc,char *argv[]){ int a=atoi(argv[1]),b=atoi(argv[2]),sum,carry; sum = a^b; carry = a&b; while(carry){ carry = carry
Avoid multiple inclusion of header file
Generally, a header (.h) file should have (A header file need not have a .h extension, its just a convention): 1. Macro definitions (preprocessor #defines). 2. Structure, union, and enumeration declarations. 3. Any typedef declarations. 4. External function declarations. 5.Continue reading… Avoid multiple inclusion of header file
Interview questions
Linked Lists Given only a pointer to a node to be deleted in a singly linked list, how do you delete it? Write a C program to implement a Generic Linked List. How would you detect a loop in aContinue reading… Interview questions
Bit wise operators
Bit wise operators and % operator cannot be applied to float fmod() is to find modulus for float as % is for int but it needs math.h; This can be used to detect whether a given number is an integerContinue reading… Bit wise operators
conditional compilation, preprocessor expression
#if something == 0 int some=0; #endif main(){ int thing = 0; printf(“%d %dn”, some ,thing); } Answer 0 0 Explanation This code is to show that preprocessor expressions are not the same as the ordinary expressions. If a nameContinue reading… conditional compilation, preprocessor expression
Lifetime and visibility of a variable
main(){ int *j; { int i=10; j=&i; } printf(“%d”,*j); } Answer: 10 The variable i is a block level variable and the visibility is inside that block only. But the lifetime of i is lifetime of the function so itContinue reading… Lifetime and visibility of a variable
General C points
Unary + is the only dummy operator in C, you can just ignore this operator. i = – -1
default function prototype
When the compiler sees a new function (ex. show) about which it doesn’t know anything. It by default assumes the function to be returning int. But when compiler sees the actual definition of function (show), if mismatch occurs (might beContinue reading… default function prototype
scope of variables
labels (ex. goto ) have functions as scope. Labels which are outside the functions are not visible to this statement and cause compilation error.