Some of C functions do not work in C++

Hello,
I have next code in main.cpp:

[/code]
#include <iostream>
#include <stdio.h>
void main(void)
{cout <<"XXX";
printf("YYY");
}
[/code]

I received:
error: invalid processing directive #include //*it's about the second directive #include <stdio.h>*//
error: "printf" was not declared in this scope. //*function printf is not recognised, I have known that functions and libraries in C can be used in C++, perhaps it is a Code::Blocks setting what I must do to work perfectly.
Please help me to understand this aspect.
Last edited on
You could pay attention to the formatting of your post.

1
2
3
4
5
6
7
8
#include <iostream>
#include <stdio.h>

void main(void)
{
  cout << "XXX";
  printf( "YYY" );
}

4:15: error: '::main' must return 'int'
 In function 'int main()':
6:3: error: 'cout' was not declared in this scope
6:3: note: suggested alternative:
In file included from 1:0:
/usr/include/c++/4.9/iostream:61:18: note:   'std::cout'
   extern ostream cout;  /// Linked to standard output

Another compiler dislikes quite different points in the program.

1
2
3
4
5
6
7
8
#include <iostream>
#include <stdio.h>

int main() // must return int. "()" means "no parameters"
{
  std::cout << "XXX"; // cout is declared in the std namespace
  printf("YYY");
}

XXXYYY

No warnings. However, it would be more proper to:
1
2
3
4
5
6
7
8
#include <iostream>
#include <cstdio> // C++ wrapper header for C header

int main() // must return int. "()" means "no parameters"
{
  std::cout << "XXX"; // cout is declared in the std namespace
  printf("YYY");
}


Code::Blocks is not a compiler. It uses a compiler. It probably includes a compiler for convenience, but is not tied to it.


These particular functions ... the printf() writes to stdio which may be distinct from cout. Mixing them can bite you.
You are much better off, if you use only one type of I/O, either C or C++.
I corrected my code

#include <iostream>
#include <stdio.h>

int main()
{cout <<"XXX";
printf("YYY");
return 0; }

I have the same errors, maybe somebody who knows code::blocks explains me how can I use a C function and a C directive into C++ project, I am beginner and I want to learn.
Keskiverto has already shown you how to do that. If there are things about his answer that you still don't understand, it would be helpful if you asked specific questions about those things.
Last edited on
Topic archived. No new replies allowed.