12 Replies - 435 Views - Last Post: 01 December 2017 - 12:26 PM Rate Topic: -----

#1 sharkattack   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 29-November 17

How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 03:33 PM

I'm showing the value of an Xlib Window in a GTK label. However, when I try to convert its value to a char string, gcc complains. I understand that "%u" is the format specifier for an "unsigned int", but I'm not aware of a format specifier for an "unsigned long int". I looked up the definition for type "Window" in the header file, and it's typedeffed as "XID", which is typedeffed as "unsigned long int". It's the closest I could find, and it seems to run ok, but I want to get it 100% right with no warnings.

[email protected]:~/Documents/programming/GUIs/gtk+$ gcc -Wall `pkg-config --cflags gtk+-3.0` -o inputfocus2 inputfocus2.c `pkg-config --libs gtk+-3.0` -lX11
inputfocus2.c: In function ‘activate’:
inputfocus2.c:26:17: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘Window * {aka long unsigned int *}’ [-Wformat=]
   sprintf(text, "%u", &xwindow);           
                 ^


In case C is easier to parse than English :turned: here we go:

  Display       *dpy = GDK_DISPLAY_XDISPLAY(gdkdisplay);
  Window         xwindow = GDK_WINDOW_XID(gdkwin);
  char          *text = (char *)malloc(sizeof(Window) +1);
  int            revert_to = 0;
  
  // Get the active window ID and display it in the label
  XGetInputFocus(dpy, &xwindow, &revert_to); 
  sprintf(text, "%u", &xwindow);           
  gtk_label_set_text(GTK_LABEL(glblLabel), text);


So that's what's going on. What are my options?

Is This A Good Question/Topic? 0
  • +

Replies To: How should I specify the format of an Xlib window in sprintf?

#2 jimblumberg   User is online

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 03:53 PM

What exactly are you trying to "print", it appears that xwindow is a pointer?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 sharkattack   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 29-November 17

Re: How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 04:00 PM

I'm trying to display the unsigned long int value of the ID atom of xwindow. Even if I would have declared a Window focus; and then set xwindow to contain the address of focus, the problem would still remain, which I tried.
Was This Post Helpful? 0
  • +
  • -

#4 sharkattack   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 29-November 17

Re: How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 04:08 PM

Is there an edit option somewhere?

Anyway, what I meant to say in my previous message was that originally, I had a pointer "Window *focus" and a non-pointer variable "Window xwindow". I originally set focus = &xwindow, but like I said, it's an unsigned long int, and "%u" expects an unsigned int.

Is there a way to supply an unsigned long int to the printf functions without angering gcc?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6290
  • View blog
  • Posts: 21,613
  • Joined: 05-May 12

Re: How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 04:20 PM

Read the docs regarding "length modifiers".

Note that when you are doing this:
sprintf(text, "%u", &xwindow);   



The parameter &xwindow is not an integer. It is a pointer. In C, the compiler will try to "help" you out by trying to make that pointer into an integer. If you are compiling for 64-bit, then pointers will be 64-bits and so it will make it a long integer.
Was This Post Helpful? 0
  • +
  • -

#6 sharkattack   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 29-November 17

Re: How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 04:41 PM

View PostSkydiver, on 30 November 2017 - 05:20 PM, said:

Read the docs regarding "length modifiers".

Note that when you are doing this:
sprintf(text, "%u", &xwindow);   



The parameter &xwindow is not an integer. It is a pointer. In C, the compiler will try to "help" you out by trying to make that pointer into an integer. If you are compiling for 64-bit, then pointers will be 64-bits and so it will make it a long integer.

Let me make this clear:
#include <stdio.h>
#include <stdlib.h>

int main (int  argc, char **argv) {
  unsigned long int number = 5;
  char *text = (char *)malloc(sizeof(long int) +1);

  sprintf(text, "%u", number);

  return 0;
}


Causes
[email protected]:~/Documents/programming/GUIs/gtk+$ gcc -Wall -o printunlongint printunlongint.c 
printunlongint.c: In function ‘main’:
printunlongint.c:8:17: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘long unsigned int’ [-Wformat=]
   sprintf(text, "%u", number);
                 ^


So back to my original question, how do you convert an unsigned long int to a character string with sprintf?
Was This Post Helpful? 0
  • +
  • -

#7 sharkattack   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 29-November 17

Re: How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 04:56 PM

I figured it out. I was passing the specifier wrong. Should've been %ld. Thanks for pointing me to the answer. I had read my man page for sprintf, but didn't catch it before.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6290
  • View blog
  • Posts: 21,613
  • Joined: 05-May 12

Re: How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 05:16 PM

Read the docs closer. "%ld" is for signed long integers.

Also your malloc() call above does not allocate enough space.
Was This Post Helpful? 0
  • +
  • -

#9 sharkattack   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 29-November 17

Re: How should I specify the format of an Xlib window in sprintf?

Posted 30 November 2017 - 09:14 PM

View PostSkydiver, on 30 November 2017 - 06:16 PM, said:

Read the docs closer. "%ld" is for signed long integers.

Also your malloc() call above does not allocate enough space.

Thanks, fixed it.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6290
  • View blog
  • Posts: 21,613
  • Joined: 05-May 12

Re: How should I specify the format of an Xlib window in sprintf?

Posted 01 December 2017 - 04:34 AM

No need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

And one last thing. In C there is no need to cast the return value of malloc().
Was This Post Helpful? 0
  • +
  • -

#11 sharkattack   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 29-November 17

Re: How should I specify the format of an Xlib window in sprintf?

Posted 01 December 2017 - 11:44 AM

Fine, I won't quote you anymore. But you seem to love pointing out mistakes without being helpful and offering better alternatives.
Was This Post Helpful? -1
  • +
  • -

#12 sharkattack   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 29-November 17

Re: How should I specify the format of an Xlib window in sprintf?

Posted 01 December 2017 - 12:20 PM

Just delete my account. There are plenty of forums on which the "leaders" don't condescend. Demoting me for pointing out your condescension? Don't even bother replying. No need to return here.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14164
  • View blog
  • Posts: 56,756
  • Joined: 12-June 08

Re: How should I specify the format of an Xlib window in sprintf?

Posted 01 December 2017 - 12:26 PM

Chill, brah. Looks like Skydiver was helping you quite a bit AND helping you with the particular niche mores of the community. Nothing but helpful.

Side note - if you click the rep point you can see who gave it to you. Hint - not Skydiver.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1