#include<stdlib.h>
#include<stdio.h>
#ifndef COMPARE_H
#define COMPARE_H
using namespace std;
int compare(const void* pfirst,const void *psecond);
#endif

int compare(const void* pfirst,const void *psecond)
{
  const int first=*((const int *) pfirst);
  const int second=*((const int *) psecond);
  return(first-second);
}

void main()
{
  int iarr[10]={9,83,100,1,645,-7654,4,23,543,9};
  int j=0;
  for(;j<10;j++)
    printf("element %d of iarr is:%d\n",j,iarr[j]);
    qsort(iarr,10,sizeof(int),compare);
    printf("sorted\n\n");
    j=0;
    for(;j<10;j++)
    printf("element %d of iarr is:%d\n",j,iarr[j]);
 
}

<< moderator edit: added [code][/code] tags >>

when i run this program,it states "std"does not exist or is not a namesapce.
then i delete "using namespace std" it's working!
why this happen?

it's a pure c code.
just a little practic of c standard library.
feeding c code to a c++complier.
thanks dave sinkula

It's not pure C, because C does not have namespaces.

So it's C++.

The C++ compiler chokes because the <stdlib.h> and <stdio.h> header files do not use the std namespace. There is no 'std' namespace as far as the compiler is aware, since none of the source files mention it. If you used <cstdlib> or <cstdio> then 'using namespace std;' would work properly.

rashakil fol
thank you!
more quesion , namespace is only related to header file?

more quesion , namespace is only related to header file?

No. std is to iostream.h (cstdio.h respectively) but namespaces are just logical units holding whatever belongs in a logical way together.

Ex.:

namespace simple_operations { 
		   double sum(double a, double b) { return a+b; };
		   double mul(double a, double b) { return a*b; };
		   // ....
}

>namespaces are just logical units holding whatever belongs in a logical way together.

That's about as good a description of namespaces as I've seen. However, cstdio.h isn't in namespace std either. cstdio is in namespace std (notice no .h) whereas stdio.h isn't in namespace std but has (just about) the same stuff in it that cstdio does. Likewise, stdlib.h, math.h, string.h, iostream.h, ctype.h, etc. are all outside namespace std, whereas cstdlib, cmath, cstring, iostream, cctype, etc. are all in namespace std.

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