Some Interesting Facts about C programming:
How to
print % sign via C program:
for printing %, use %%.
int perc;
perc = 25;
printf("The mark is: %d%%", perc);
output:
Will print The mark is 25%
another type:
printf("%c", '%');
for printing %, use %%.
int perc;
perc = 25;
printf("The mark is: %d%%", perc);
output:
Will print The mark is 25%
another type:
printf("%c", '%');
A C program to print 1 to 1000 without using
semicolon:
main(i){
while(printf("%d ", i), i < 1000) {}}
Note: Trick is that i is initialized with number of arguments, i.e. 1
//another one:
#define P1(i) if(printf("%d\n",i )){}#define P2(i) P1(i) P1(i)#define P4(i) P2(i) P2(i)#define P8(i) P4(i) P4(i)#define P16(i) P8(i) P8(i)#define P32(i) P16(i) P16(i)#define P64(i) P32(i) P32(i)#define P128(i) P64(i) P64(i)#define P256(i) P128(i) P128(i)#define P512(i) P256(i) P256(i)main(i){P512(i) P256(i) P128(i) P64(i) P32(i) P8(i)}
//another one:
#include <iostream>
void main(){
if (int i = 1) {
while (std::cout << i << std::endl, i < 1000) {}
}
//another one:
main(i) {
if(printf("%d\n", i) && i != 1000 && main(i)) { }}
//another one:
#include <iostream>
void main(int argc, char*argv[]) {
if( ! (argc ^= argc) )
while( argc <1000 && std::cout.operator<<(argc) ) {
}
}
//another one:
#include <stdio.h>
void main(int argc, char*argv[]) {
if(argc=1000)
while(argc-- && printf("%d ",1000-argc)) {}
}
//another one:
#include <iostream>
#define Please(x) if (x) {}int Number(int x){ if (x > 1) Please(Number(x-1)) Please(std::cout << " " << x)
}
int main(){ Please(Number(1000))
}
//another one:
main(i){if(printf("%d",i),i<10 00&&main(i
1)){}}
//another one:
#include <iostream>
void loop(int low, int high){
if ( low <= high && std::cout << low << std::endl && (loop(low 1, high), false) ) {}
}
int main(){
if (loop(1, 1000), false) {}
main(i){
while(printf("%d ", i), i < 1000) {}}
Note: Trick is that i is initialized with number of arguments, i.e. 1
//another one:
#define P1(i) if(printf("%d\n",i )){}#define P2(i) P1(i) P1(i)#define P4(i) P2(i) P2(i)#define P8(i) P4(i) P4(i)#define P16(i) P8(i) P8(i)#define P32(i) P16(i) P16(i)#define P64(i) P32(i) P32(i)#define P128(i) P64(i) P64(i)#define P256(i) P128(i) P128(i)#define P512(i) P256(i) P256(i)main(i){P512(i) P256(i) P128(i) P64(i) P32(i) P8(i)}
//another one:
#include <iostream>
void main(){
if (int i = 1) {
while (std::cout << i << std::endl, i < 1000) {}
}
//another one:
main(i) {
if(printf("%d\n", i) && i != 1000 && main(i)) { }}
//another one:
#include <iostream>
void main(int argc, char*argv[]) {
if( ! (argc ^= argc) )
while( argc <1000 && std::cout.operator<<(argc) ) {
}
}
//another one:
#include <stdio.h>
void main(int argc, char*argv[]) {
if(argc=1000)
while(argc-- && printf("%d ",1000-argc)) {}
}
//another one:
#include <iostream>
#define Please(x) if (x) {}int Number(int x){ if (x > 1) Please(Number(x-1)) Please(std::cout << " " << x)
}
int main(){ Please(Number(1000))
}
//another one:
main(i){if(printf("%d",i),i<10
//another one:
#include <iostream>
void loop(int low, int high){
if ( low <= high && std::cout << low << std::endl && (loop(low 1, high), false) ) {}
}
int main(){
if (loop(1, 1000), false) {}
}
Printing semicolon without using semicolon:
#include <stdio.h>
int main(int i) {
while(i-- && printf("%d",59)) {}
while(!printf("%d",59)) {}
if(printf("%d",59)){}
}
#include <stdio.h>
int main(int i) {
while(i-- && printf("%d",59)) {}
while(!printf("%d",59)) {}
if(printf("%d",59)){}
}
C program to shutdown or turn off computer:
#include <cstdio>
#include <cstdlib>
main(){
char ch;
printf("Do you want to shutdown your computer now (y/n)\n");
scanf("%c",&ch);
if (ch == 'y' || ch == 'Y')
system("C:\\WINDOWS\\System32\ \shutdown
-s"); // Windows XP
// system("C:\\WINDOWS\\System32\\ shutdown
/s"); //Windows 7
//system("shutdown -P now"); //Ubuntu Linux
return 0;
}
Note: To restart You can use /r instead of /s
#include <cstdio>
#include <cstdlib>
main(){
char ch;
printf("Do you want to shutdown your computer now (y/n)\n");
scanf("%c",&ch);
if (ch == 'y' || ch == 'Y')
system("C:\\WINDOWS\\System32\
//
//system("shutdown -P now"); //Ubuntu Linux
return 0;
}
Note: To restart You can use /r instead of /s
Recursive main:
#include<stdio.h>
main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
o/p:- 5 4 3 2 1
main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
o/p:- 5 4 3 2 1
Reverse the string :
#include<stdio. h>
#include<string .h>
void func(char*,int, int);
int main(){
char ch[100];
gets(ch);
func(ch, 0, strlen(ch)-1);
printf("%s\n",ch );
return 0;
}
void func(char *x, int beg, int end){
char a, b, c;
if ( beg >= end )
return;
c = *(x+beg);
*(x+beg) = *(x+end);
*(x+end) = c;
func(x, ++beg, --end);
}
#include<stdio. h>
#include<string .h>
void func(char*,int, int);
int main(){
char ch[100];
gets(ch);
func(ch, 0, strlen(ch)-1);
printf("%s\n",ch );
return 0;
}
void func(char *x, int beg, int end){
char a, b, c;
if ( beg >= end )
return;
c = *(x+beg);
*(x+beg) = *(x+end);
*(x+end) = c;
func(x, ++beg, --end);
}
Printer Connection in C:
#include<stdlib.h>
void main(void)
{
system("type text.txt>LPT1");
}
#include<stdlib.h>
void main(void)
{
system("type text.txt>LPT1");
}
Aulternative:
#include<stdlib.h>
void main(void)
{
system("notepad.exe /P text.txt");
}
/*this code is effective for both parallel and usb printers*/
void main(void)
{
system("notepad.exe /P text.txt");
}
/*this code is effective for both parallel and usb printers*/
it will print the content of text.txt file.
Here is a small Quine in ANSI C.
int main(void){
char str[]= "int main(void){ char str[]= %c%s%c; printf(str, 0x22, str, 0x22);}";
printf(str, 0x22, str, 0x22);
}
int main(void){
char str[]= "int main(void){ char str[]= %c%s%c; printf(str, 0x22, str, 0x22);}";
printf(str, 0x22, str, 0x22);
}
A dangerous program:
#include<stdlib.h>
main()
{
int i;
for(i=1;i<=1;)
system("md %random%");
}
note: don't try !!!
#include<stdlib.h>
main()
{
int i;
for(i=1;i<=1;)
system("md %random%");
}
note: don't try !!!
Something different with Pointer:
#include<stdio.h>
main()
{
int a[7]={1,2,3,4};
printf(“%d\n%d\n%d\n%d\n%d\n”, (*a),*(&*a),a[*a*0],*a,*(a+1)) ;
}
output:
1
1
1
1
2
#include<stdio.h>
main()
{
int a[7]={1,2,3,4};
printf(“%d\n%d\n%d\n%d\n%d\n”,
}
output:
1
1
1
1
2
What will be the output :-
ReplyDelete#include
main(){
printf("%d",sizeof(FILE));
printf("%d",sizeof(__FILE__));
}