2

I just started using OpenGL. This is my first code:

// OpenGL hello program
#include<iostream>
#include <GL/glut.h>
#include <cstring>

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    char message[] = "Hello, world!";
    glRasterPos2d(0, 0);
    for (int i = 0; i < sizeof(message) / sizeof(message[0]); i++)
    {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, message[i]);
    }
}

int main(int argc, char *argv[]) {
    glutInit(&argc, argv);
    glutInitWindowSize(500, 500);
    glutCreateWindow("OpenGL hello program");
    glutDisplayFunc(display);
    glutMainLoop();
}

The error I am getting is: Warning: comparison between signed and unsigned integer expressions (line 9). I also tried writing a new code then to see whats causing the problem:

#include<iostream>
#include <cstring>

void display1() {
    char message[] = "Hello, world!";

    for (int i = 0; i < sizeof(message) / sizeof(message[0]); i++)
        std::cout<<message[i];
}

int main() {
    display1();
}

This code works perfectly fine. Why is the first code not working fine?

EDIT: Following up on Cyber's annswer, I changed the loop to:

for (unsigned int i = 0; i < sizeof(message) / sizeof(message[0]); i++)

But the OpenGL code does not do the expected i.e. show "Hello, world!" message in the window. It just creates a window with "OpenGL hello program" written at the top and nothing else.

Michael IV
  • 11,016
  • 12
  • 92
  • 223
Bosnia
  • 333
  • 2
  • 3
  • 10
  • 1
    `sizeof` operator returns `size_t` which is unsigned integral type. In the for loop's conditional you are comparing the result of a division between two `sizeof`s which is an unsigned integral value with `i` that is an `int`. – 101010 Aug 21 '14 at 15:07
  • Why is the second code working fine then? – Bosnia Aug 21 '14 at 15:09
  • The second piece of code issues the same warning. – 101010 Aug 21 '14 at 15:19
  • @Cyber I mean for the first code (OpenGL), not the test code (second one) – Bosnia Aug 21 '14 at 15:20
  • Note that `sizeof(message)` returns the number of entries only in C++ since there `sizeof(char)==1`. – Benjamin Bannier Aug 21 '14 at 15:31
  • Your code is working fine, because this is a warning, not an error. The problem is that the compiler writers observed that whenever a compiler sees code like yours, there is only a 50% chance that the code behaves in the way the programmer intended. So the compiler is *warning* you that you might not have written what you intended to write. Nice guys, the compiler people, aren't they? – cmaster - reinstate monica Aug 21 '14 at 16:30
  • Possible duplicate of [C: how can I fix warnings like: "comparison between signed and unsigned"](http://stackoverflow.com/questions/859943/c-how-can-i-fix-warnings-like-comparison-between-signed-and-unsigned) – phuclv Mar 24 '17 at 10:55

2 Answers2

13

This line is the problem

for (int i = 0; i < sizeof(message) / sizeof(message[0]); i++)

The operator sizeof has the return type of std::size_t which is therefore what you should use for the type of your variable i. std::size_t is an unsigned type, so the compiler is warning you that you are comparing a signed type (int) to an unsigned type, because the comparison is potentially unsafe if the value of one variable is not in the representable range of in the other type.

for (std::size_t i = 0; i < sizeof(message) / sizeof(message[0]); ++i)

Or simply use a range-based for loop.

for (auto i : message)
{
    std::cout << i;    // i is a char from the message array
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • @Bosnia: Your question seems focused on the signed/unsigned warning, which has been answered. If you have a different question, you should probably start a new post. If you intended to ask a different question here, you should probably correct the title and the tags and focus the text on the other issue. – Adrian McCarthy Aug 21 '14 at 17:36
-1

for (int i =0,i<sizeof ....) That line is the problem. The sizeof function returns an unsigned integer thus causing error when compared to the signed int. Try creating an int variable for the sizeof like this:

int size=sizeof()...

Then replace in your line as follows:

for (int i=0,i<size...)

stateMachine
  • 5,227
  • 4
  • 13
  • 29