Thursday, December 13, 2007

Interesting C Program -21

My friend came to me with the following weird C puzzle.


#include "stdio.h"
int main()
{
int x = 5;
printf("%f",x);
return 0;
}

This program throws me the following warning:

double format, different type arg (arg 2)


and the output is 0.000000

What is this?
His arguement was like this. Though my compiler allocates 4 bytes for both integer and float datatypes, why am I not being able to retrieve the same value if I interchange the types?

Yes, very good question.

The explanation goes like this.

The data will be retrieved from a variable in the way it is stored. If you retrieve it in a way different from the way it is stored, you will not be able to get the same value you have stored.

Lets be a bit more clear.

I am going to show you how the variables are stored in the memory.

Observe the following program.

#include "stdio.h"
int main()
{
int x=800;
int *x_int_ptr = &x;
unsigned char *x_char_ptr = (unsigned char *)x_int_ptr;
printf("%x %x %x %x",*(x_char_ptr+3),*(x_char_ptr+2),*(x_char_ptr+1),*(x_char_ptr));
return 0;
}


The output is 0 0 3 20

What is this?
Yes, the integer will be stored in the local memory like this. The equivalent of Integer Decimal 800 is Integer Binary 0b0000 0000 0000 0000 0000 0011 0010 0000.
But a character can store only 8 bits. So the higher byte 0000 0011 is stored as 3 in the second LS byte of output and the lower byte 0010 0000 which is 20 is stored in the first LS byte.

An integer 800 is stored like this.

Lets see how a float is stored.


#include "stdio.h"
int main()
{
float x=800;
float *x_int_ptr = &x;
unsigned char *x_char_ptr = (unsigned char *)x_int_ptr;
printf("%x %x %x %x",*(x_char_ptr+3),*(x_char_ptr+2),*(x_char_ptr+1),*(x_char_ptr));
return 0;
}



The ouput is 44 48 0 0

If we retrieve this value like an integer, how can we expect a correct result?
Let me justify the output 44 48 0 0.
This storage in memory is based on IEEE754 standard.

Just go the following link.
http://babbage.cs.qc.edu/IEEE-754/32bit.html

There you will find a calculator which takes this hex value as input and computes the decimal value that is given as input.

Just type 44480000 in field adjacent to Hexadecimal Representation, and you will be seeing the 800 in the field adjacent to Decimal Value Entered:.

So you need to be very careful while storing and retrieving data.


Isnt this program weird?

Monday, December 10, 2007

Interesting C Program -20

Today, when I was studying about preprocessing stuff in C Programs, I thought of trying out something different. I wrote the following C program.


#define a #include"stdio.h"
#define b main()
#define c {
#define d printf("hello world\n");
#define e }
a
b
c
d
e


Will it work?

No. It didnt worked!!!

I got the following error.

syntax error at '#' token
syntax error before string constant


What exactly could be the reason?

Lets analyse it step by step, from the DOS prompt. The following steps will work only when your path variables are correct. In order to correct them you must tweak in the Environment variables.


First lets just preprocess it.

Type the following command in the DOS Prompt


gcc -E main.c>main.i

The output of preprocessing has been redirected to the main.i.

When I opened main.i, I found the following.
----------main.i file starts here---------------------

# 1 "src\\main.c"
# 1 ""
# 1 ""
# 1 "src\\main.c"
# 58 "src\\main.c"
#include"stdio.h"
main()
{
printf("hello world\n");
}


----------main.i file ends here-----------------------


Hurray!!! I found out the problem.
After preprocessing, all the #defines has been replaced by their respective counter parts. But the only #include remains as it is. It didnot get replaced by the #include "stdio.h" file. This is causing the error.

Now let me make some modifications.


#define b main()
#define c {
#define d printf("hello world\n");
#define e }
#include "stdio.h"
b
c
d
e

Now it is compiling.

Is it correct? Lets check it out.

Now run the same command

gcc -E main.c>main.i

Observe the main.i file
My god, I am getting a very big main.i file.

If you see that file, the stdio.h file has been pasted there.

The conclusion is that the input to a compiler should not have #'s in the code. All #'s should be removed at the preprocessing stage itself.

A compiler doesnot know #.

Isnt this program weird? ;)

Wednesday, December 5, 2007

Interesting C Program -19

Hey,
Its long since my last post. I am here, with a very unique program.

Observe the output of the following C program
CASE:1

void func(void);
int main()
{
if(0)
func();
return 0;
}

Does this compile? (Yes/No). The answer depends on the compiler you are using. In some compilers this will compile without any errors. How come? "func()" is not defined anywhere. How come it is running?

Some smart compilers have that intelligence to compile such codes. This program runs, because "if(0)" is never going to be true. So the statement that follows is will never be executed. So, it dont even bother to search where the "func" function has been defined.

Ok, lets play around with the same program. Try out the following program.
CASE:2
void func(void);
int x = 0;
int main()
{
if(x)
func();
return 0;
}

Alas! this is throws the following errors.


Compiling source file(s)...
file_2.c
Linking...
C:\Documents and Settings\Administrator\Desktop\CTIPS\my_proj\Debug\file_2.o(.text+0x28): In function `main':
C:\Documents and Settings\Administrator\Desktop\CTIPS\my_proj\file_2.c:65: undefined reference to `func'



Why the hell it is throwing linker errors?

Lets do some more modifications and try.

Try out the following program.
CASE:3

void func(void);
const int x = 0;
int main()
{
if(x)
func();
return 0;
}


Again this throws the same error.
Is there any way of running this program?
Yes, there are ways.

There is a small tweaking you need to do in the compiler settings.

Lets try out that.

I am using MingWStudio. Try out similar things in your compiler or the Makefile.

1. Go to Project->Settings.
2. Go to Compile tab
3. By default the Optimization level will be None.
4. Change the Optimization to Level 1 or Level 2 or Level 3 or Minimum Size.
5. Give OK.
CASE:4
Now Clean the program to remove some previous configuration and then build.

This will definitely run.

The explanation goes like this.
In CASE:1, since mine is a smart compiler, this will be compiled without any errors.
I am working on CASE:2, CASE:3 and CASE:4.

Meanwhile if somebody knows why CASE:2 and CASE:3 didnt work and CASE:4 worked, please do comment me.

Friday, November 2, 2007

Sorting Algorithm - 2 (Shaker Sort)

A slightly improved version of BUBBLE SORTING.


void shaker_sort(char *str,int count)
{
int i,exchange=0;
do{
exchange = 0;
for(i=1;i<count-1;i++)
{
if(str[i]>str[i+1])
{
exchange = 1;
swap_items(&str[i],&str[i+1]);
}
}
for(i=count-1;i>0;i--)
{
if(str[i-1]>str[i])
{
exchange = 1;
swap_items(&str[i-1],&str[i]);
}
}
}while(exchange);
}
void swap_items(char *a,char *b)
{
(*a)^=(*b)^=(*a)^=(*b);
}

Sorting Algorithm - 1 (BUBBLE SORT)

A very primitive, simple and Well known Sorting algorithm.


void bubble_sort(char *str, int count)
{
int i,j;
for(i=0;i<count;i++)
{
for(j=count-1;j>i;j++)
{
if(str[j-1]>str[j])
{
swap_terms(&str[i],&str[j]);
}
}
}
}

void swap_terms(char *a,char *b)
{
(*a)^=(*b)^=(*a)^=(*b);
}

Monday, October 29, 2007

Interesting C Program -18

Hi all,
Observe the output of the following program.


#include "stdio.h"
#define DPRINTI(X) printf("\n%s = %d \n",#X,X)
#define DPRINTF(X) printf("\n%s = %f\n",#X,X)
#define DPRINTC(X) printf("\n%s = %c\n",#X,X)
int main()
{
double x = 100.1;
int y = 5;
char z = 'a';
void *p;

p = &z;
DPRINTI(sizeof(*(char *)p));
DPRINTC(*(char *)p);
DPRINTI(p);

p = &y;
DPRINTI(sizeof(*(int *)p));
DPRINTI(*(int *)p);
DPRINTI(p);

p = &x;
DPRINTI(sizeof(*(double *)p));
DPRINTF(*(double *)p);
DPRINTI(p);

return 0;
}


The output is something like the following:


sizeof(*(char *)p) = 1

*(char *)p = a

p = 2293635

sizeof(*(int *)p) = 4

*(int *)p = 5

p = 2293636

sizeof(*(float *)p) = 8

*(double *)p = 100.100000

p = 2293640



Explanation goes like this.

Initially a pointer has been initialized of type void * (generic pointer). We are type casting that pointer p to point a location in which a character is stored. Then we are typecasting that pointer p to point a location in which an integer is stored. Later, we are again type casting that pointer p to point a location in which a float is stored. This program runs without any errors.
The character variable is stored in 2293635 location (Size is 1 byte), ie 2293635.
The Integer variable is stored in 2293636 location (Size is 4 bytes), ie 2293636, 2293637, 2293638 and 2293639.
The Float variable is stored in 2293640 location (Size is 8 bytes), ie 2293640, 2293641, 2293642, 2293643, 2293644, 2293645, 2293646 and 2293647.
Every thing seems simple.

Hey wait wait. How can Life be so simple?

Lets see what happens when the following code is executed?


#include "stdio.h"
#define DPRINTI(X) printf("\n%s = %d \n",#X,X)
#define DPRINTF(X) printf("\n%s = %f\n",#X,X)
#define DPRINTC(X) printf("\n%s = %c\n",#X,X)
int main()
{
double x = 100.1;
int y = 5;
char z = 'a';
void *p;

p = &x;
DPRINTI(sizeof(*(double *)p));
DPRINTF(*(double *)p);
DPRINTI(p);

p = &y;
DPRINTI(sizeof(*(int *)p));
DPRINTI(*(int *)p);
DPRINTI(p);

p = &z;
DPRINTI(sizeof(*(char *)p));
DPRINTC(*(char *)p);
DPRINTI(p);

return 0;
}

The output is something like this:


The output is something like the following:


sizeof(*(float *)p) = 8

*(double *)p = 100.100000

p = 2293640

sizeof(*(int *)p) = 4

*(int *)p = 5

p = 2293636

sizeof(*(char *)p) = 1

*(char *)p = a

p = 2293635



Dont you see something weird in the output?

The addresses of float, int, and char should be respectively,2293635, 2293643 and 2293647. ie. the starting address should be 2293635. From there float occupies 8 bytes. The next 4 bytes is occupied by Integer (2293643, 2293644,2293645 and 2293646 and the next 1 byte is occupied by character variable, which is 2293647.
But that is not happening.

Can anybody comment me with the reason for that?

Wednesday, October 24, 2007

Interesting C Program -17

Hey!!!
This code converts the decimal number into binary, decimal, octal and hexadecimal number systems. Here "pow()" function is used. Since pow() function returns float, the values are typecasted.
Note: Never use a^b for "a raised to the power of b". This will do the EXOR operation.
Try using pow(base,power) function.


#include "stdio.h"
#include "math.h"
/*----------#defines starts here----------*/
#define DECIMAL 10
#define BINARY 2
#define OCTAL 8
#define HEXADECIMAL 16
#define DIGITS_DECIMAL 5
#define DIGITS_BINARY 16
#define DIGITS_OCTAL 3
#define DIGITS_HEXADECIMAL 3
/*-----------#defines ends here-----------*/
/* FUNCTION DECLARATIONS STARTS HERE */
char *tobinary(int x);
char *todecimal(int x);
char *tooctal(int x);
char *tohexadecimal(int x);
/* FUNCTION DECLARATIONS ENDS HERE */
int main()
{
int x;
char *y;
printf("ENTER THE VALUE TO CONVERT:\t\t");
scanf("%d",&x);
y=tobinary(x);
printf("\nBINARY:\t\t%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c"\
,y[0],y[1],y[2],y[3],y[4],y[5],y[6],y[7],y[8],y[9],y[10],\
y[11],y[12],y[13],y[14],y[15]);
y=todecimal(x);
printf("\nDECIMAL:\t%c %c %c %c %c",y[0],y[1],y[2],y[3],y[4]);
y=tooctal(x);
printf("\nOCTAL:\t\t%c %c %c",y[0],y[1],y[2]);
y=tohexadecimal(x);
printf("\nHEXADECIMAL:\t%c %c %c",y[0],y[1],y[2]);

return 0;
}
/* FUNCTION DEFINITIONS STARTS HERE */
char *todecimal(int x)
{
char y[3];
int i;
for(i=0;i<DIGITS_DECIMAL;i++)
{
y[i] = ((x/(int)pow(DECIMAL,(DIGITS_DECIMAL-i-1)))%DECIMAL)+0x30;
}
return y;
}

char *tobinary(int x)
{
char y[8];
int i;
for(i=0;i<DIGITS_BINARY;i++)
{
y[i] = ((x/(int)pow(BINARY,(DIGITS_BINARY-i-1)))%BINARY)+0x30;
}
return y;
}
char *tooctal(int x)
{
char y[3];
int i;
for(i=0;i<DIGITS_OCTAL;i++)
{
y[i] = ((x/(int)pow(OCTAL,(DIGITS_OCTAL-i-1)))%OCTAL)+0x30;
}
return y;
}
char *tohexadecimal(int x)
{
char y[3];
int i;
for(i=0;i<DIGITS_HEXADECIMAL;i++)
{
y[i] = ((x/(int)pow(HEXADECIMAL,(DIGITS_HEXADECIMAL-i-1)))%HEXADECIMAL)+0x30;
if(y[i]==0x3A) y[i] =0x41;
if(y[i]==0x3B) y[i] =0x42;
if(y[i]==0x3C) y[i] =0x43;
if(y[i]==0x3D) y[i] =0x44;
if(y[i]==0x3E) y[i] =0x45;
if(y[i]==0x3F) y[i] =0x46;


}
return y;
}
/* FUNCTION DEFINITIONS ENDS HERE */

Monday, August 20, 2007

Interesting C Program -16

Can u write a C program to swap two integers that does not use a temporary variable (or that uses only two variables to be swapped)?


Here goes an implementation:


#include "stdio.h"
int main()
{
int a=1,b=3;
printf("BEFORE SWAPPING : a=%d & b=%d\n",a,b);
a^=b^=a^=b;
printf("AFTER SWAPPING : a=%d & b=%d\n",a,b);
}


The output looks something like this:

BEFORE SWAPPING : a=1 & b = 3
AFTER SWAPPING : a=3 & b = 1

Looks good isn't it?

Sunday, August 19, 2007

Interesting C Program -15

Atlast, Here is my implementation of Pascal Triangle!!!

/*PASCAL TRIANGLE*/
#include"stdio.h"
int factorial(int);
int combination(int,int);
int main()
{
int rows,i,j;
printf("ENTER THE NUMBER OF ROWS:\t");
scanf("%d",&rows);
printf("\n---------------------------------------------------------\
------------------\n");
printf(" PASCAL TRIANGLE WITH %d ROWS \ ",rows);
printf("\n-----------------------------------------------------------\
----------------\n");
for(i=1;i<=rows;i++)
{
for(j=0;j<=i;j++)
{
printf("%d\t",combination(i,j));
}
//printf("\n");
printf("\n---------------------------------------------------------------\
------------\n");
}
}
int factorial(int x)
{
if(x<2)
return 1;
else
{
return x*factorial(x-1);
}
}
int combination(int n,int r)
{
int y;
y=factorial(n)/(factorial(r)*factorial(n-r));
return y;
}


The Output looks like this

ENTER THE NUMBER OF ROWS: 5

---------------------------------------------------------------------------
PASCAL TRIANGLE WITH 5 ROWS
---------------------------------------------------------------------------
1 1
---------------------------------------------------------------------------
1 2 1
---------------------------------------------------------------------------
1 3 3 1
---------------------------------------------------------------------------
1 4 6 4 1
---------------------------------------------------------------------------
1 5 10 10 5 1
---------------------------------------------------------------------------

Thursday, August 2, 2007

Interesting C Program -14

Just see the output of the following C program. The output looks bit weird. Isnt it?


#include "stdio.h"
#define DPRINTF(x) printf("%s = %d\n",#x,x);

int main()
{
int x[20];
int *p,*q;
p=&x[5];
q=&x[9];
DPRINTF(p);
DPRINTF(q);
DPRINTF(q-p);
}



The output is


p = 2293580
q = 2293596
q-p = 4

q-p should be 16. But it gives 4. why?



The explanation goes like this.


Lets take a real life example.

Say you took 9 dozens of apples. You returned 5 dozens of apples. Then how many dozens of apples do you have? 4 dozens isnt it? You will not say 48 apples.

Similar is the case here. x[9] is stored in 2293596 address location and x[5] is stored in 2293580. p and q are integer pointers, each occupying 4 bytes. Now when you display the value of p and q, they will be 2293580 and 2293596 respectively. But if you ask for difference, it will be 4.
Inshort, 9 integer pointers - 5 integer pointers = 4 integer pointers.


Now just try this program:



#include "stdio.h"
#define DPRINTF(x) printf("%s = %d\n",#x,x);

int main()
{
int x[20],y,z;
int *p,*q;
p=&x[5];
q=&x[9];
DPRINTF(p);
DPRINTF(q);
DPRINTF(q-p);
y = p;
z = q;
DPRINTF(y);
DPRINTF(z);
DPRINTF(z-y);
}

The output is like this:

p = 2293580
q = 2293596
q-p = 4
y = 2293580
z = 2293596
z-y = 16


If you see the difference between z and y, you will get 16.

I think now no explanation is required now.

Interesting C Program -13

Hey!!! Now u can change the color of the output window (DOS window). How?

Just try the following program:


#include "stdio.h"
#include "string.h"
#include "stdlib.h"
main()
{
int i=0;
for(i = 0;i<3;i++)
{
char x[] = "color ";
(i==0)?strcat(x,"0F"):((i==1)?strcat(x,"1E"):((i==2)?strcat(x,"2D")\
:strcat(x,"3C")));
system(x);
system("pause");
}
}


Your output window looks colorful right?

Interesting C Program -12

Can you write a simple "Hello World" Program that doesnot use semicolon?
Here is an implementation!!!


#include "stdio.h"
int main()
{
if(printf("Hello World")){}
}

Friday, July 13, 2007

Interesting C Program -11

Want to know my name?



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);

}

Thursday, July 12, 2007

Interesting C Program -10

Quine Program: (A C Program that prints itself)


/*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:


#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)

#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:


#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!!!

#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:


#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.

/*
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 :-) ?

#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.

#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;
}

Search Google

Books that I refer to...

  • The Complete Reference C, Fourth Edition
  • The C Programming Language