Answer the question
In order to leave comments, you need to log in
Why does %c output me and %s throws an error?
I am writing code, and for some reason, if %s is replaced with %c, it outputs the rules, but if %s gives this error:
An exception was thrown: read access violation.
it was 0x4F.
Please help me decide, xs what it is, most likely something elementary, but I xs
#include <stdio.h>
#include <conio.h> //підключення бібліотеки текстового інтерфейсу
#include <locale>
void main()
{
setlocale(LC_ALL, "Russian");
char b = '|';
char n = '#';
char x = 'FIO';
char y = 'kurs';
char z = 'grup';
char f;
int k;
char g;
printf_s("\n%c%c%c%10s%10c", b,n,b,x,b);
}
Answer the question
In order to leave comments, you need to log in
char x = 'FIO';
Mistake.
Multi-character constants are implementation defined, used for heavy optimization, and are extremely unreliable in Visual C++. In GCC it's kind of ba-me, but in any case, converting to char 'FIO', which should take three bytes, will give 'F' or 'O'.
You need not a character constant, but a string one:
And, accordingly, %s.
And how does printf and any other function with a variable number of arguments work? These arguments are dumped in bulk into an area of memory called the “call stack”, and printf starts parsing this bulk. To shift the pointer by one byte and say: this is char - and the %c format is used. Of course, an incorrect format will result in data type misinterpretation and failure on one or all platforms.
char x[] = "FIO";
%c - there is one byte in memory, interpret it as a single character
%s - there is a pointer to a zero-terminated string in memory, 4/8 bytes. (All arrays in native C are passed as pointers anywhere.)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question