Problem in C

Pages: 12
when I put #include<stdio> as header file in my program then it show error that there is no such file exist
Can any one fix this problem ???
should be #include <cstdio> or #include <stdio.h>
I have also tried this but unable to overcome that error

please check out my code

#include<cstdio>
using namespace std;
void swap(int *,int *)
int main()
{
int a=10,int b=20;
swap(&a,&b);
printf("a=%d,b=%d",a,b);
return 0;
}
void swap(int *x,int *y){
t=*x;
*x=*y;
*y=t;
}
It is very unlikely it is not there, but can yourself find the file stdio.h on your computer?
If you work in C you really want #include <stdio.h>. Why do you have the line using namespace std?
I cannot find a reason why was that post reported.

Also, you are using std with cstdio.

If you don't include iostream, you should not 'using namespace std'.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>

void swap(int *, int *);
int main()
{
    int a = 10;
    int b = 20;
    swap(&a,&b);
    printf("a=%d,b=%d",a,b);
    return 0;
}
void swap(int * x, int * y) {
   int t = *x; // You need to declare 't' as int.
   *x = *y;
   *y = t;
}


OR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>

#include <iostream>
// You need to include iostream, to be able to use namespace std
using namespace std;

void swapint(int *, int *);
// The combination of iostream and using namespace std create a new swap function.
// You need to change swap to another name. In this example, swapint.
int main()
{
    int a = 10;
    int b = 20;
    swapint(&a,&b);
    printf("a=%d,b=%d",a,b);
    return 0;
}
void swapint(int * x, int * y) {
   int t = *x; // You need to declare 't' as int.
   *x = *y;
   *y = t;
}
Last edited on
Remember he's using C, not C++.
If you are using C and not C++ (which is likely the case since <cstdio> isn't present) this should work for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>

void swap(int *,int *);
int main()
{
  int a=10
  int b=20;
  swap(&a,&b);
  printf("a=%d,b=%d",a,b);
  return 0;
}
void swap(int *x,int *y){
  int t=*x;
  *x=*y;
  *y=t;
}


Edit: Actually that's ALMOST the same as EssGeEich above except for the <stdio.h>. Should have read that first
Last edited on
1
2
3
4
5
6
7
template <class BN>
inline void _swap(BN &X, BN &Y)
{
  BN Z(X);
  X = Y;
  Y = Z;
}


1
2
3
4
5
6
7
void _swap(char &X, char &Y)
{
  char Z;
  Z = X;
  X = Y;
  Y = Z;
}
Last edited on
Templates and references in C?
> If you don't include iostream, you should not 'using namespace std'.
> You need to include iostream, to be able to use namespace std
¿wtf?
gautamreloaded,

I bet you din't think it would be this hard to get a straightforward question answered. :0)

Can you give us a bit more info? Can you confirm that you want C and not C++ (or not)? What compiler/IDE you are using.

Well in his original post to this thread he attempted:

 
#include <stdio> 


That is why he was getting the error when in fact (if as we presume he is using C ) it should be:

 
#include <stdio.h> 


Note the .h at the end of the included file
ajh32 wrote:
Well in his original post to this thread he attempted ... we presume he is using C

Yes, he also said 'I have also tried this but unable to overcome that error' so it is time to stop presuming and get some facts. ;0)
... yes, but that was then with:

 
#include <cstdio> 


But, if he's compiling for C and not C++ that wouldn't work either!

Agreed, wait for him to come back fith further info...
ne555 wrote:
> If you don't include iostream, you should not 'using namespace std'.
> You need to include iostream, to be able to use namespace std
¿wtf?

Uhm? What's wrong?

@Smac89: Identifiers beginning with an underscore are implementation-only.
using namespace std; will simply put all that's in the std namespace, into the global namespace.
So you can access `std::foo', `foo' or `::foo'

There is nothing special in `iostream' that enables using namespace std;
- I am actually using Linux terminal for the C program.
- and I am new to C so I don't know where to use using namespace std; or not
- anyway can you tell me which IDE is better for C programing on the window platform
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
void swap(int *,int *);
int main()
{
   int a=10, b=20;
   swap(&a,&b);
   printf("a=%d,b=%d",a,b);
   return 0;
}
void swap(int *x,int *y){
   int t=*x;
   *x=*y;
   *y=t;
}


> I am actually using Linux terminal for the C program.
print your build command
Last edited on
- One thing more is that I have compile this code using <cstdio> and neglecting the using namespace std; but I am still getting same i.e
- error: cstdio: No such file or directory

- code is

#include<cstdio>
void swap(int *,int *);
int main()
{
int a=10;
int b=20;
swap(&a,&b);
printf("a=%d,b=%d",a,b);
return 0;
}
void swap(int *x,int *y)
{
int t=*x;
*x=*y;
*y=t;
}
C doesn't have an include file, <cstdio>, nor does it recognise namespaces. All that stuf is C++. You really should be clear on what you want help with.
Last edited on
Pages: 12