char a[5][6] = {
{0xe,0x6,0x8,0x6,0x7,0xe},
{0x9,0x9,0x8,0x9,0x2,0x4},
{0xe,0xf,0x8,0xf,0x2,0x4},
{0x9,0x9,0x8,0x9,0xa,0x4},
{0xe,0x9,0xe,0x9,0x4,0xe}
};
m[4]={0x8,0x4,0x2,0x1};
int main()
{
int i,j,k;
do
{
j=0;
do
{
k=0;
do
{
(a[i][j]&m[k])?printf("#"):printf(" ");
}while(++k<4);
printf(" ");
}while(++j<6);
printf("\n");
}while(++i<5);
}
Friday, July 13, 2007
Interesting C Program -11
Want to know my name?
Thursday, July 12, 2007
Interesting C Program -10
Quine Program: (A C Program that prints itself)
But Can we call this a Quine Program?
The answer is NO.
Why?
Because, after compilation if you change the source code, new source file will be printed. Then how do you write a quine program?
Heres an implementation of Quine:
char *s="char *s=%c%s%c;%cmain(){printf(s,34,s,34,10,10);}%c";
main() {printf(s,34,s,34,10,10);}
/*quine.c*/
#include"stdio.h"
int main()
{
FILE *fp1;
char x[100][100]={0};
int i;
fp1 = fopen("quine.c","r");
for(i=0;i<50;i++)
fgets(x[i],50,fp1);
for(i=0;i<50;i++)
printf("%s",x[i]);
}
But Can we call this a Quine Program?
The answer is NO.
Why?
Because, after compilation if you change the source code, new source file will be printed. Then how do you write a quine program?
Heres an implementation of Quine:
char *s="char *s=%c%s%c;%cmain(){printf(s,34,s,34,10,10);}%c";
main() {printf(s,34,s,34,10,10);}
Interesting C Program -9
Something weird in the output of the following program:
The output will be:
10
4
10
#include "stdio.h"
void main(void)
{
int c = 10;
printf("%d\n",c);
printf("%d\n",sizeof(++c));
printf("%d\n",c);
}
The output will be:
10
4
10
++c inside sizeof function is ignored.
Why?
The answer goes like this:
The input argument to sizeof function is datatype and not data. So the datatype of ++i will be considered instead of data ++i. So it will not be incremented.
Monday, July 9, 2007
Interesting C Program -8
Just C the potential of the space in the following C Program:
(Try removing the space before %c in scanf)
(Try removing the space before %c in scanf)
#include
int main()
{
char c;
scanf("%c",&c);
printf("%c\n",c);
scanf(" %c",&c);
printf("%c\n",c);
return 0;
}
Interesting C Program -7
Guess the Output of the following C Program:
The answer is 4321.
Explanation:
printf returns the number of letters printed.
#include "stdio.h"
int main()
{
int i=43;
printf("%d\n",printf("%d",printf("%d",i)));
return 0;
}
The answer is 4321.
Explanation:
printf returns the number of letters printed.
Saturday, July 7, 2007
Interesting C Program -6
One more Interesting Program
#include "stdio.h"
#include "stdlib.h"
#define DPRINTF(x) printf("%s:%d\n",#x,x)
int main()
{
int Y=5;
DPRINTF(Y);
system("pause");
}
Interesting C Program -5
Guess the output of the following C program!!!
If your guess is 40, it is definitely not!!!
Explanation:
The input to the function is not the entire array. Only the address of the first element of the array will be passed into the function.
If your guess is 40, it is definitely not!!!
#define SIZE 10
void size(int arr[SIZE])
{
printf("size of array is:%d\n",sizeof(arr));
}
int main()
{
int arr[SIZE];
size(arr);
return 0;
}
Explanation:
The input to the function is not the entire array. Only the address of the first element of the array will be passed into the function.
Interesting C Program -4
A Program that counts the number of bits set in a number:
The following is another excellent implementation of the same program:
#include "stdio.h"
#include "stdlib.h"
int Count_Bits(int a)
{
int i=0;
int b[10]={0,0,0,0,0,0,0,0,0,0};
int c=0;
while(a!=0)
{
b[i] = a%2;
a = a/2;
i++;
}
for(i=0;i<10;i++)
{
c+=b[i];
}
return c;
}
int main()
{
int x,y;
printf("ENTER A VALUE LESS THAN 1023:");
scanf("%d",&y);
x=Count_Bits(y);
printf("%d",x);
system("pause");
}
The following is another excellent implementation of the same program:
int CountBits(unsigned int x)
{
int count=0;
while(x)
{
count++;
x = x&(x-1);
}
return count;
}
Tuesday, July 3, 2007
Interesting C Program -3
Can you write a C program to print the String in the center of the Screen...
Heres a little demonstration of that.
Heres a little demonstration of that.
/*
This program will move the string to the center of the screen.
Center in this context is a relative thing. So I have taken my screen size as 80
and did the program. The user has to change according to his size of the screen.
*/
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void mystring(char* s);
int main()
{
char name[10];
mystring("ENTER YOUR NAME\n");
mystring("");
scanf("%s",&name);
mystring(name);
mystring("\n");
system("pause");
}
void mystring(char* s)
{
int screen_size = 80;
int l=strlen(s);
int pos=(int)((screen_size-l)/2);
for(int i=0;i < pos;i++ )
printf(" ");
printf("%s",s);
}
Monday, July 2, 2007
Interesting C Program -2
I thought the following program was a perfect C program. But on compiling, I found a silly mistake. Can you find it out (without compiling the program :-) ?
The bug is in "
Try the following code. The correct version.
#include "stdio.h"
#include "stdlib.h"
void OS_Solaris_print()
{
printf("Solaris - Sun Microsystems\n");
}
void OS_Windows_print()
{
printf("Windows - Microsoft\n");
}
void OS_HP-UX_print()
{
printf("HP-UX - Hewlett Packard\n");
}
int main()
{
int num;
printf("Enter the number (1-3):\n");
scanf("%d",&num);
switch(num)
{
case 1:
OS_Solaris_print();
break;
case 2:
OS_Windows_print();
break;
case 3:
OS_HP-UX_print();
break;
default:
printf("Hmm! only 1-3 :-)\n");
break;
}
return 0;
}
The bug is in "
OS_HP-UX_print();
". The function should not use '-' symbol. It will be considered as a minus. So the compiler will show error as OS_HP has not yet been defined and UX_print has not yet been defined.Try the following code. The correct version.
#include "stdio.h"
#include "stdlib.h"
void OS_Solaris_print()
{
printf("Solaris - Sun Microsystems\n");
}
void OS_Windows_print()
{
printf("Windows - Microsoft\n");
}
void OS_HP_UX_print()
{
printf("HP-UX - Hewlett Packard\n");
}
int main()
{
int num;
printf("Enter the number (1-3):\n");
scanf("%d",&num);
switch(num)
{
case 1:
OS_Solaris_print();
break;
case 2:
OS_Windows_print();
break;
case 3:
OS_HP_UX_print();
break;
default:
printf("Hmm! only 1-3 :-)\n");
break;
}
return 0;
}
Interesting C Program -1
The expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.
The culprit is the initialization in the for loop. The initial value should be a positive integer.
Try this program. This program will do the intended task.
A Contribution from Mr. SasiKumar:
The code can be replaced as follows:
#include"stdio.h"
#include"stdlib.h"
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
system("pause");
return 0;
}
The culprit is the initialization in the for loop. The initial value should be a positive integer.
Try this program. This program will do the intended task.
#include "stdio.h"
#include "stdlib.h"
#define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))
int array[] = {1,2,3,4,5};
int main()
{
unsigned int d;
for(d=0;d {
printf("%d",array[d]);
system("pause");
}
system("pause");
return 0;
}
A Contribution from Mr. SasiKumar:
The code can be replaced as follows:
#include"stdio.h"
#include"stdlib.h"
int array[] = {23,34,12,17,204,99,16};
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int main()
{
int d;
for(d=-1;d < = (signed)(TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
system("pause");
return 0;
}
Subscribe to:
Posts (Atom)
Search Google
Books that I refer to...
- The Complete Reference C, Fourth Edition
- The C Programming Language