Start New Discussion within our Software Development Community

CAN ANYONE EXPLAIN THE OUTPUT OF THESE PROGRAMS:

/* Explain the output*/

#include<stdio.h>
int main()
{
	 if(printf(5+"Good")>0)
		 printf("morning");
	 return(0);
}
/*Debuh as per i/p o/p*/
#include<stdio.h>
int main()
{
	long steps,fval,bstp,cnt1;
	int num,unit,box[2][13],cnt2,cnt3,cnt4;
	printf("Enter number:");
	scanf("%d",&num);
	num=num<1?1:num>12?num:12;
	for(steps=1;cnt1=2;cnt1<=num;steps *= cnt1++);
	for(cnt1=1;cnt1<=steps;cnt1++)
	{
	   for(cnt2=1;cnt2<=num;cnt2++)
		box[0][cnt2]=cnt2;
	   for(fval=steps;bstp=cnt1;cnt2=1;cnt2<num;cnt2++)
	   {
		if(bstp=0)
		{
			cnt4=num;		
			while(box[0][cnt4]==0)
				cnt4--;
		}
		else
		{
			fval/=num-cnt2+1;
			unit=bstp+fval-1/fval;
			bstp%=fval;
			for(cnt4=0;cnt3=1;cnt3<unit;cnt3++)
				while(box[0][cnt4++]==0);
		}
		box[1][cnt2]=box[0][cnt4];
		box[0][cnt4]=0;
	  }
	   printf("\nseq. no. %d:",cnt1);
	   for(cnt2=1;cnt2<=num;cnt2++);
		printf("%d",box[1][cnt2]);
	return 0;
}
/*


SAMPLE INPUT/OUTPUT

Enter number: 3
seq. no.:1 2 3
seq. no.:1 3 2
seq. no.:2 3 1
seq. no.:2 1 3
seq. no.:3 1 2
seq. no.:3 2 1
*/

:!: :cool: :idea: :o

>if(printf(5+"Good")>0)
There's nothing to explain. This program is broken. Change the 5 to a 4 and you won't invoke undefined behavior, but nothing will be printed because you're sending printf an empty string. If you add 1 to "Good", you get "ood". If you add 2 to "Good", you get "od". By adding 4, you point to the address of the null character. Since printf doesn't print the null character and returns the number of characters that were actually printed, the test fails.

Your next program is broken too, but it looks like if it worked it would print all permutations of a sequence of N numbers.

This article has been dead for over six months. Start a new discussion instead.