Saturday, October 30, 2010

C & C++ Questions & Answers

Q) what operations can be performed on pointers ?

I said- normal referencing, dereferencing, & normal arithmetic operations like addition, subtraction, increment, decrement.The i said- using dereferencing operator one can perform all the operations allowed on referenced variables, using pointers. But he said- can u divide pointers?I said- no. He said- then why did you say so? You are contradicting urself. I don't know when did I...

Q) do you know something about call by value & call by reference?
I said yes.He asked me to tell the differnce. i said- If you use call by value, you create a copy of original object/var. & all the operations performed modifies only the copy. Original object remains unchanged.But, If you use call by reference, a reference is created instead of a copy, & all the operations are reflected in original object.

Q) so if you want to change the value, would you use a function or a procedure?
I said - a procedure. He asked me- why? I said- convetionally, function is used when we wan't to return a value such as a status info. or result of any operation. Then I said- since a procedure does not return any value to point of reference (where it was called) only the referenced values are manipulated. He said- Now, you are saying that procedure doesn't return a value so u r contradicting urself for ur using procedure to change a value.I said- no, a function is used when we want to return a value to point of reference, It can return only a single value. & original vals. are not modified unless they use the return value of the function (i.e., by appearing on the L.H.S. of an assignment including a function call.)

Q) what does a function returns?
I said- any return value, that we want it return. Like result of an operation. He asked- explain. Like if you want to add two values, then what would the function return? I said- ok, if you are using a function sum like (i wrote it on a paper) int sum (int a, int b) { return a+b; } & calling the function like c = sum(a,b); then the function sum will return the value of operation a+b to c.

Q) int i=0; while(i=0) printf("%d",++i); Guess the output *****
infinite loop

Q) int x=2, y=6, z=6; x=y==z; printf("%d",x); Guess the output *****
prints 1.

Q. What is a configuration file?
A. A configuration file tells Turbo C++ what options to default to and where to look for its library and header files. TC.EXE looks for a configuration file named TCCONFIG.TC, and TCC.EXE looks for a file named TURBOC.CFG.

When linking C or Assembly language modules with C++ modules I get undefined symbol errors at link time. It appears that none of the C or Assembly public symbols can be found.
A. C++ is a strongly typed language. In order to support the language to its fullest, Turbo C++ must attach information to the symbols generated for function names and variables. When this is done, the symbol will no longer match the standard C style function name. In order to link correctly, the compiler must be notified that the symbol is declared in an external module without type information tacked on to the symbol. This is done by prototyping the function as type extern "C". Here is a quick example: extern "C" int normal_c_func( float, int, char ); // name not altered void cplusplus_function( int ); // name altered See related comments under Linker Errors and in the Paradox Engine question in this section.

Classes with static data members are getting linker errors ("undefined").
A. This code is built into Turbo C++ 1.0 but not in version 3.0. In the 1.0 compiler, static members without definitions were given a default value of 0. This default definition will no longer be made in the compiler. The programmer must now give an explicit definition for each static member. Here is a quick example: class A { static int i; }; A linker error saying that A::i is not defined will result unless the source also contains a line such as: int A::i = 1;

What potential problems can arise from typecasting a base class pointer into a derived class pointer so that the derived class's member functions can be called?
A. Syntactically this is allowable. There is always the possibility of a base pointer actually pointing to a base class. If this is typecast to a derived type, the method being called may not exist in the base class. Therefore, you would be grabbing the address of a function that does not exist.

Q: What's the difference between the keywords STRUCT and CLASS?
A: The members of a STRUCT are PUBLIC by default, while in CLASS, they default to PRIVATE. They are otherwise functionally equivalent.

Q: I have declared a derived class from a base class, but I can't access any of the base class members with the derived class function.
A: Derived classes DO NOT get access to private members of a base class. In order to access members of a base class, the base class members must be declared as either public or protected. If they are public, then any portion of the program can access them. If they are protected, they are accessible by the class members, friends, and any derived classes.

Q: How can I use the Paradox Engine 1.0 with C++?,
A: Because the Paradox Engine functions are all compiled as C functions, you will have to assure that the names of the functions do not get "mangled" by the C++ compiler. To do this you need to prototype the Engine functions as extern "C". In the pxengine.h header file insert the following code at the lines indicated. /* inserted at line # 268 */ #ifdef __cplusplus extern "C" { #endif /* inserted at line # 732, just before the final #endif */ #ifdef __cplusplus } #endif Paradox Engine version 2.0 is "aware" of C++ and thus does not require any modifications to its header file.

I have a class that is derived from three base classes. Can I insure that one base class constructor will be called before all other constructors?
A: If you declare the base class as a virtual base class, its constructor will be called before any non-virtual base class constructors. Otherwise the constructors are called in left-to-right order on the declaration line for the class. Q: Are the standard library I/O functions still available for use with the C++ iostreams library? A: Yes, using #include functions such as printf() and scanf() will continue to be available. However, using them in conjunction with stream oriented functions can lead to unpredictable behaviour.

Q: In C++, given two variables of the same name, one local and one global, how do I access the global instance within the local scope?
A. Use the scope (::) operator. int x = 10; for(int x=0; x < ::x; x++) { cout << "Loop # " << x << "\n"; // This will loop 10 times }

Q: Will the following two functions be overloaded by the compiler, or will the compiler flag it as an error? Why?

void test( int x, double y); & int test( int a, double b); A. The compiler will flag this as a redeclaration error because neither return types nor argument names are considered when determining unique signatures for overloading functions. Only number and type of arguments are considered.

Q: If I pass a character to a function which only accepts an int, what will the compiler do? Will it flag it as an error?
A. No. The compiler will promote the char to an int and use the integer representation in the function instead of the character itself.

Q: I was trying to allocate an array of function pointers using the new operator but I keep getting declaration syntax errors using the following syntax: new int(*[10])(); What's wrong?
A. The new operator is a unary operator and binds first to the int keyword producing the following: (new int) (*[10])(); You need to put parentheses around the expression to produce the expected results: new (int (*[10]());

Q: What are inline functions? What are their advantages? How are they declared?
A. An inline function is a function which gets textually inserted by the compiler, much like macros. The advantage is that execution time is shortened because linker overhead is minimized. They are declared by using the inline keyword when the function is declared: inline void func(void) { cout << "printing inline function \n"; } or by including the function declaration and code body within a class: class test { public: void func(void) { cout << "inline function within a class.\n"} };

Q: If I don't specify either public or private sections in a class, what is the default? A. In a class, all members are private by default if neither public nor private sections are declared.

Q: What is a friend member function?
A. Declaring a friend gives non-members of a class access to the non-public members of a class.

Q: Why do I get a "Type name expected" error on my definition of a friend class in my new class?
A You need to let the compiler know that the label you use for your friend class is another class. If you do not want to define your entire class, you can simply have "class xxx", where xxx is your label.

Q: What is the "this" pointer?
A. "this" is a local variable in the body of a non-static member function. It is a pointer to the object for which the function was invoked. It cannot be used outside of a class member function body.

Q: Why does a binary member function only accept a single argument?
A. The first argument is defined implicitly.

Q: Looking through the class libraries there are definitions in classes which look like: class test { int funct( void ) const; }; What is the const keyword doing here?
A. There is a pointer to the object for which a function is called known as the 'this' pointer. By default the type of 'this' is X *const ( a constant pointer). The const keyword changes the type to const X *const ( a constant pointer to constant data ).

I would like to use C++ fstreams on a file opened in binary mode, how is this done?
A: Use ios::binary as the open mode for the file: #include ifstream binfile; binfile.open("myfile.bin", ios::binary);

Q: How can I find out where my "null pointer assignment" is occurring?
A. Set a watch on the following expressions: *(char *)0,4m (char *)4 Step through the program. When the values change, the just-executed line is the one that is causing the problem. Null pointer is known not to point to any object; an uninitialized pointer might point anywhere. there is a null pointer for each pointer type, and the internal values of null pointers for different types may be different. And is implicitly defined as : preprocessor macro NULL is #defined (by or ), with value 0

Q: When I try to load a new file after editing a file, the first file remains on the screen. How do I close the first file?
A. Use Alt-F3 to close the current file. Also, use F6 to move from one file to the next, if there is more than one file open at a time.

Q: I'm doing a search and replace operation, and the editor prompts me for each replacement. I've selected "Change All", but it still does it.
A. To disable the prompting, you must unselect the "Prompt on replace" option on the left side of the dialog box.

Q: When I try to use the any of the pseudo registers, like _AX, I get the error message "Undefined symbol '_AX' in function..." when I compile. Why?
A. You are only allowed to use the pseudo registers in the Turbo C++ and ANSI modes of the compiler. You can change this setting in the Options | Compiler | Source menu.

Q: Since I don't have a mouse, can I still copy blocks of code from one file to another?
A. Yes. You can mark the beginning and end of a block by moving to the appropriate area and pressing Ctrl-K-B (mark beginning) and Ctrl-K-K (mark end). You can then use the copy and paste commands in the Edit menu.

How do I stop all of the files I have ever edited from constantly being open when I bring up Turbo C++?
A: BY default, Turbo C++ saves what is called the desktop configuration. This configuration is saved in a file with a .DSK extension. By deleting any files of this type, then entering Options/Environment/Preferences and removing the check from 'auto save desktop', you will begin with a clean desktop each time you invoke Turbo C++.

Q: The '\n' in cprintf() does not return the cursor to the beginning of the line. It only moves the cursor down one line.
A. cprintf() interprets '\n' as a Line Feed. To force the cursor to the beginning of the line, manually insert a Carriage Return: cprintf("\n\r");
Q: How do I print to the printer from a Turbo C++ program?
A. Turbo C++ uses a FILE pointer (stdprn) defined in the STDIO.H file. You do not need to open stdprn before using it: #include int main(void) { fprintf(stdprn, "Hello, printer!\n"); } Note that if your printer is line-buffered, the output is flushed only after a '\n' is sent.

Q: Why don't printf() and puts() print text in color?
A. Use the console I/O functions cprintf() and cputs() for color output. #include int main(void) { textcolor(BLUE); cprintf("I'm blue."); }

Q: How do I print a long integer?
A. Use the "%ld" format: long int l = 70000L; printf("%ld", l);

Q: How do I print a long double?
A. Use the "%Lf" format. long double ldbl = 1E500; printf("%Lf", ldbl);

Q: How do I compile the BGIDEMO program?
A. 1. Make sure that the following Turbo C++ files are in your current directory: BGIDEMO.C *.BGI *.CHR 2. Run Turbo C++. 3. Load BGIDEMO.C into the Editor by pressing F3, then typing BGIDEMO 3. Go to the Run menu and choose the Run item.

Q: How do I create a COM file?
A. DOS versions 3.2 and earlier include an EXE2BIN utility that converts EXE files to COM files. Users who do not have EXE2BIN can use TLINK, the Turbo C++ command-line linker, to create a COM file instead of an EXE file. Use the /t option. For example: TCC -mt -lt tiny will create TINY.COM instead of TINY.EXE. The -l switch passes the /t argument to the linker in this case.

Q: Why do I get incorrect results from all the math library functions like cos(), tan() and atof()?
A. You must #include before you call any of the standard Turbo C++ math functions. In general, Turbo C++ assumes that a function that is not declared returns an int. In the case of math functions, they usually return a double. For example /* WRONG */ /* RIGHT */ #include int main(void) int main(void) { { printf("%f", cos(0)); printf("%f", cos(0)); } }

Q: How do I change the stack size?
A. The size of the stack of a Turbo C++ program is determined at run time by the global variable _stklen. To change the size to, for example, 10,000 bytes, include the following line in your program: extern unsigned _stklen = 10000; This statement must not be inside any function definition. The default stack size is 4,096 bytes (4K).

Q: My program comes up with the message 'Null pointer assignment' after it terminates. What does this mean?
A. Before a small-data model Turbo C++ program returns to DOS, it will check to see if the beginning of its data segment has been corrupted. This message is to warn you that you have used uninitialized pointers or that your program has corrupted memory in some other way.

Q: Why are .EXE files generated by TC.EXE larger than those generated by TCC.EXE?
A. In the default configuration, TC.EXE includes debugging information in the .EXE files that it creates, and TCC.EXE does not. If you don't want to produce this debugging information, you can shut it off in the Integrated Development Environment by selecting Alt-O|B|N.

Q: I have a working program that dynamically allocates memory using malloc() or calloc() in small data models (tiny, small, and medium). When I compile this program in large data models (compact, large, and huge), my program hangs.
A. Make sure that you have #include in your program.

Q) I had definition char x[6] in one source file, and another I declared extern char *x. Why didn't it work?
Ans) The declaration extern char *x simply does not match the actual definition. The type "pointer-to-type-T" is not the same as "array-of-type-T." Use extern char x[]. But I heard that char x[] was identical to char *x. But it is wrong

No comments: