Home c How to Use Char Type with integers

How to Use Char Type with integers

Author

Date

Category

As far as I understand, the type char is the symbol type, you can write one character in it, for example, “f” . But it turns out, it can be used as an integer type, i.e. Record into it, for example, 5 and produce some operations with numbers. But I use Visual Studio and I have already had problems using scanf_s ("% C", & amp; CH, 1) , with what I did not point the size char through the comma, this time I also tried to use the type char , but this time according to the specifer % d , I think that this is a mistake, because it is the only thing that I did not do before, but I don’t know exactly, maybe somewhere stupid somewhere. But the point is that the program starts and ends with a nonzero code, and in my opinion even gives the wrong answer, for understanding I will give the full text of the program.

The essence of the program and the bitwise operations are not so important, it is important that Visual on the line with scanf_s () gives the Warning “WARNING C6328 Size Nonsetting:” Char “is transmitted as a parameter Param (2), although when calling “ScanF_s” requires “32 Bit Operand”. This indicates a possible serious error. Getting this message for functions similar to SCANF can lead to devastation or buffer overflow. “It seems to understand what I warns, but I do not understand how to fix it. Thanks in advance for the detailed explanation of my problem. If you have some other comments in my code, I will be grateful to hear them. I only learn and glad any remarks.

# include & lt; iostream & gt;
#Include & lt; stdio.h & gt;
#Include & lt; Windows.h & gt;
INT MAIN ()
{
  SetConsoleCP (1251);
  SetConsoleoutPutcp (1251);
  Char C;
  Char F;
  Char B;
  Unsigned Char N;
  Unsigned int Word;
  Printf ("Enter the status code (0 - 31) & gt;");
  scanf_s ("% d", & amp; c);
  PrintF ("Enter a sign of error (0/1) & gt;");
  scanf_s ("% d", & amp; f);
  PrintF ("Enter employment sign (0/1) & gt;");
  scanf_s ("% d", & amp; b);
  Printf ("Enter the number of bytes (0 - 255) & gt;");
  scanf_s ("% d", & amp; n);
  Word = ((unsigned char) C & amp; 0x1f) & lt; & lt; eleven;
  Word = ((unsigned char) f & amp; 1) & lt; & lt; nine;
  Word = ((unsigned char) b & amp; 1) & lt; & lt; eight;
  Word = (N & AMP; 0xFF);
  Printf ("\ NIGHT device device =% 04x \ n", Word);
  Return 0;
}

Answer 1, Authority 100%

For Printf Scanf, it is better to open MSDN and read. There is a link to formats, open it. I was looking for Printf , because It is better documented.

Read carefully Format Specifications Specification.

Since I came across the dimension – then I already know what you need to know the prefixes. We find paragraph size prefixes for printf and wprintf format-type specifiers .

to Specify Use Prefix with Type Specifier
Char.
Unsigned Char HH D, I, O, U, X, OR X
Short int
Short unsigned int h d, i, o, u, x, or x
...
Wide Character L (LowerCase L) OR W C or C
Single-BYTE CHARACTER STRING H S, S, OR Z
Wide-Character String L (LowerCase L) OR W S, S, OR Z

for type type, there is a HH prefix that can be applied to i (iconic number) or U (unsigned number).

Total answer

scanf_s ("% HHD", & amp; c);

About HC Prefix – HC – You need to use if WPRINTFW or WSCANFW must be forced to work with Char 8-bit instead of WCHAR_T 16-bit as a simir. For a byte – everything is necessary to use HHD, or also acceptable HHI, HHO, HHU (unsigned), HHX (Hex).

But this is not all, there are still features. For stack variables, the alignment is used, which varying the size, for the X86, the variable is still physically to occupy 4 bytes. For global variables – the same, but in the options you can turn off the alignment. For structures – you can turn off the alignment (you need to google packaging structures in C++), but it will be more difficult to write. The easiest way is to create an array of Char Data [4]; in this array – all memory will be clearly involved. But you have one of the numbers – unsigned – therefore … so-so idea with an array, I usually use an array where it is guaranteed to save memory. In terms of speed, this will have a winnings recent, the winning such packaging will give – if you have a deep recursion, or if you work with large data arrays – then you can confuse with the size if you need to achieve a smaller consumption of memory. A smaller consumption of memory for large volumes – allows you to increase the speed in some cases. For a specifically of your case, all this can be pricked.


Answer 2

scanf_s (and just in scanf ) the code>% d code requires a 4-byte variable. Type Char takes 1 byte.
It is better to use the type int (4 byte).

However, you can convert char * in int * :

scanf_s ("% d", (int *) (& amp; f), 4);

, but you must then ensure that the number entered is not more than 1 byte.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions