cancel
Showing results for 
Search instead for 
Did you mean: 

prinf and scanf will not work in c++ .please share with complete solution cout and cin usage in c++

MMARI.1
Senior
 
9 REPLIES 9
Pavel A.
Evangelist III

Why? printf and scanf work great in c++. # include <cstdio>

#include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ uint8_t display[] = "2555 \r\n"; volatile uint8_t flag =0; volatile int counter = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); /* USER CODE BEGIN PFP */ #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } no compile error .. #include but no ouput in serial monitor . #include it's working
i mean not working #include working with only #include

You cannot use a C++ include file in a plain C file. main.c is a plain C file.

hi pavel , good moring .
i made stdio.h in main.c and tested find that printf is working.
i made cstdio in main.cpp and tested find that prinf is not working .
i need to change the setting in properties -> c\c++ build-> setting -> g++compile>preprocessor is so please suggest an exact setting screen shot .

> i need to change the setting in properties -> c\c++ build-> setting ->

This works for me with libraries provided in CubeIDE and your PUTCHAR_PROTOTYPE above, without any changes in settings:

// File: testcpp.cpp
#include <cstdio>
 
// call this from your main.c
extern "C"
void test_cpp(void)
{
    printf("hello c++ %d\n", 42);
}

 Getting a bit fancier.... the following works too, with language standard ISO C++17:

#include <cstdio>
using std::printf;
 
extern "C"
void test_cpp(void)
{
    printf("hello c++ %d\n", 42);
}

Not applicable

Use printo("text", double, float, uint(8-16-32-64)_t, int(8-16-32-64)_t ). Any parameters without restrictions, in any order.

https://github.com/AVI-crak/Rtos_cortex/blob/master/sPrint.h

Before using, define the string output function - soft_print(char* txt)

Works very fast.

  1. hi , pavel , i am facing difficulty and error where i need keep this /

now kept in

/* USER CODE BEGIN PFP */

#ifdef __GNUC__

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif

PUTCHAR_PROTOTYPE

{

HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);

return ch;

}

void test_cpp(void);

/* USER CODE END PFP */

/* USER CODE BEGIN 4 */

  1.  
  2. extern "C"
  3. void test_cpp(void)
  4. {
  5. printf("hello c++ %d\n", 42);
  6. }
  7. /* USER CODE BEGIN 4 */
  8.  

while (1)

  1. and try call by test_cpp(); in loop .

hi pavel, i was try with many times . since it's working with you can share exact location i need keep and calling .