yes.it works fine.bt would you plz explain how? Unknown - Feb 1, 2014yes.it works fine.bt would you plz explain how?
Its because printf() returns the characters it prints. So if we do printf (“Hello World\n”) … it returns 10 (for helloworld) + 1 (for space) + 1 (for \n)=12.
Now, coming to main part, if(12) , will be executed as in C language, if(0) is like if (false), but if(any other integer) is true, so if statement is executed, meanwhile printing Hello World.
[Read More]
Printing "Hello World" to screen without using a single semicolon in C/C++
When I first encountered this question, I thought it was impossible. However, there are ways to do so. I don’t know if the solutions are exploitation of c / c++. Anyway, here are the most common solutions:
int main() { if (printf("Hello World \\n")) { } } Or you can use the while loop like this:
int main() { while (printf("Hello World") > 20) { } } ```If you don't think these codes will work, try them out.
[Read More]
Squeeze all multiple spaces in a string into one space
#include <stdio.h>
void trimspace(char \*dst) {
const char \*src = dst;
int tocopy = 1;
char c;
while((c = \*src++)) {
if(tocopy)
\*dst++ = c;
tocopy = (c != ' ') || (\*src != ' ');
}
\*dst = '\\0';
}
int main() {
char s\[64\];
scanf("%\[^\\n\]c", s);
trimspace(s);
printf("%s\\n", s);
}
Leveling sectors in a circle
Problem : How to make the surface of the circular plot level in minimum number of moves
Detail:
The circular plot is divided into sectors. Each of these sectors has a level which is indicated by an integer. The sector can be either above the earth surface, in which case the integer is positive, or below the earth surface, when it will be negative. She can choose two consecutive sectors and increase the level of both the sectors by 1 at a time, which will be counted as one move.
[Read More]
What is BST or Binary Search tree?
A binary search tree (BST) is a tree which has following property :
Every node’s left sub-tree has a key less than the node’s key, and every right sub-tree has a key greater than the node’s key.
The major advantage of binary search trees is that the related sorting algorithms and search algorithms, such as in-order traversal, can be very efficient. This data structure is perfect for advanced interview questions. See the figure below for this tree:
[Read More]
Common naming convention : Coding Style
**Variable names must be in mixed case starting with lower case. **
Common practice in the Java development community and also the naming convention for variables used by Sun for the Java core packages. Makes variables easy to distinguish from types, and effectively resolves potential naming collision as in the declaration
eg.
int state;
Names representing constants (final variables) must be all uppercase using underscore to separate words.
MAX_ITERATIONS, COLOR_RED
[Read More]
Java and CPP - the differences and similarities
This list of similarities and differences is based heavily on The Java Language Environment, A White Paper by James Gosling and Henry McGilton http://java.sun.com/doc/language_environment/ and the soon-to-be published book, Thinking in Java by Bruce Eckel, http://www.EckelObjects.com/. At least these were the correct URLs at one point in time. Be aware, however, that the web is a dynamic environment and the URLs may change in the future.
Java does not support typedefs, defines, or a preprocessor.
[Read More]
Enum in c++
The problem: representing series of values It is very common to have a series of values that need to be represented. For example, to simulate a traffic light requires representing three values (red, yellow, and green), but there is no built-in C++ color datatype.
Use integer values to represent colors, for example red as 0, yellow as 1, and green as 2. There is nothing “green” about the value 2, and it could just as easily be represented by some other number.
[Read More]
How to bound check arrays in cpp / c
Bound checking in cpp /c is headache….
char \*strcpy(char \*dest, const char \*src) { char \*save \= dest; while(\*dest++ \= \*src++); return save; } //main func
char *src = “hello to c programming language”;
char dest[12];
strcpy(dest,src); //calling function
Here we have no bound check on dest size or src size. When we pass it to function it is perfectly alright but
problem is dest is array which is just 12 bytes long…but src is larger string…
[Read More]
File IO in c++ 1
Introduction
This tutorial will start with the very basis of File I/O (Input/Output) in C++. After that, I will look into aspects that are more advanced, showing you some tricks, and describing useful functions.
You need to have good understanding of C++, otherwise this tutorial will be unfamiliar and not useful to you!
Your Very First Program
I will first write the code, and after that, I will explain it line by line.
[Read More]