C Program Tricky Programs
C Program Tricky Programs
Custom Search
COURSES Login
HIRE WITH US
C/C++ Tricky Programs
We may come across various tricky programs in our day to day life. May be in
technical interviews, coding tests, or in C/C++ classrooms.
Here is a list of such programs :-
int main()
{
std::cout << "\"geeksforgeeks\"";
return 0;
}
Output:
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 1/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks
"geeksforgeeks"
int main()
{
int x = 10;
int y = 10;
if ( !(x ^ y) )
printf(" x is equal to y ");
else
printf(" x is not equal to y ");
return 0;
}
Output:
x is equal to y
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 2/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks
int main()
{
static int x = 1;
if (cout << x << " " && x++ < N && main())
{ }
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
4. Program to nd Maximum and minimum of two numbers without using any loop
or condition.
The simplest trick is-
int main ()
{
int a = 15, b = 20;
printf("max = %d\n", ((a + b) + abs(a - b)) / 2);
printf("min = %d", ((a + b) - abs(a - b)) / 2);
return 0;
}
Output:
max = 20
min = 15
5. Print the maximum value of an unsigned int using One’s Compliment (~)
Operator in C.
Here is a trick to nd maximum value of an unsigned int using one’s compliment
operator:
int main()
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 3/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks
{
unsigned int max;
max = 0;
max = ~max;
printf("Max value : %u ", max);
return 0;
}
Output:
10
return 0;
}
Output:
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 4/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks
geeksforgeeks
Output:
int main()
{
unsigned int n = 1;
char *c = (char*)&n;
if (*c)
printf("LITTLE ENDIAN");
else
printf("BIG ENDIAN");
return 0;
}
This article is contributed by Smitha Dinesh Semwal. If you like GeeksforGeeks and
would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to [email protected].
See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you nd anything incorrect, or you want to share more
information about the topic discussed above.
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 5/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks
Recommended Posts:
Output of C++ programs | Set 36
Output of C++ programs | Set 37
Output of C programs | Set 63
Output of C++ programs | Set 22
Error Handling in C programs
Output of C++ programs | Set 29 (Strings)
Memory Layout of C Programs
Output of C++ programs | Set 25 (Macros)
How to Compile and Run C/C++/Java Programs in Linux
Programs to print Interesting Patterns
Programs for printing pyramid patterns in C++
Output of C programs | Set 30 (Switch Case)
Common Memory/Pointer Related bug in C Programs
Facts and Question related to Style of writing programs in C/C++
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 6/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks
11
2.9
To-do Done
Based on 14 vote(s)
Please write to us at [email protected] to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 7/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks
Comments Community
1 Login
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Thanks
△ ▽ • Reply • Share ›
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 8/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks
COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials
PRACTICE CONTRIBUTE
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 9/9