Thread: What's the difference from passing a int to char and a L L int to char?

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    What's the difference from passing a int to char and a L L int to char?

    I had some Int that i had to turn into Long Long Int.

    But now all my conversions are mazed!


    I dont know the difference between long long int to char and int to char..

    i tried this example to understand but it doesnt work either..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      long long int b;
      char lol[9999];
      b=534534654;
      
      sprintf(lol,"&#37;lli oi?",(long long int)b);
      printf("lol: %s", lol);
      system("PAUSE");	
      return 0;
    }
    How can i solve it?
    I used sprintf to convert ints into chars, now with long long ints its different..

    Thanks.
    Last edited by Milhas; 04-01-2008 at 02:29 AM.

  2. #2
    Banned
    Join Date
    Mar 2008
    Posts
    78
    And if i wanna use unsigned long long int?

    (i really need this answer, its for an exam )

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    That's not how you use sprintf at all. Your parameters are out of order. Why don't you look at the documentation for sprintf?

  4. #4
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by arpsmack View Post
    That's not how you use sprintf at all. Your parameters are out of order. Why don't you look at the documentation for sprintf?
    right, i forgot that.. lol

    Corrected!

  5. #5
    Banned
    Join Date
    Mar 2008
    Posts
    78
    I'm getting errors. OMG i'm doomed :'(

    I need to turn int into unsigned long long int for this to work but this functions give me errors when area_camada,boxes_camada,wires_camada are unsigned long long int:

    Code:
        void escreveDados() { //This one writes in char camadas_dados[], some data in this order/syntax: "&#37;i-%i-%i]"
         char result[2000];
         sprintf(result,"%i-%i-%i]",area_camada,boxes_camada,wires_camada);
         printf("%i,%i,%i\n",area_camada,boxes_camada,wires_camada);
         strncpy(camadas_dados+p4,result,strlen(result));
         printf("camada_dados+p4: %s\n",&camadas_dados[p4]);
         p4 += strlen(result); //Deslocar p4 &#224; direita
         area_camada = boxes_camada = wires_camada = 0;
         printf("p4: %i\n", p4);
        }
    
    
        void dadosCamada () { //This one reads what the above function wrote in char camadas_dados[]
             int s; //0 = area_camada, 1 = boxes_camada, 2 = wires_camada
             bk=pX;
             area_camada = boxes_camada = wires_camada = 0;
             s=0;
             while(camadas_dados[bk] != ']') {
             printf("**BK: %c\n",camadas_dados[bk]);
              if(isdigit(camadas_dados[bk])) {
               area_camada = (s == 0) ? area_camada*10 + (int)(camadas_dados[bk] - '0') : area_camada;
               boxes_camada = (s == 1) ? boxes_camada*10 + (int)(camadas_dados[bk] - '0') : boxes_camada;
               wires_camada = (s == 2) ? wires_camada*10 + (int)(camadas_dados[bk] - '0') : wires_camada;
               printf("c[bk]: %c \t c+bk: %i\n",camadas_dados[bk],wires_camada);
              }
              else s++;
              bk++;
             }
             printf("!!!!!!!!!!!!!!!!!!!%i,%i,%i\n",area_camada,boxes_camada,wires_camada);
             }
    They work well if area_camada,boxes_camada,wires_camada are only int.

  6. #6
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Please someone.. :'(

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Milhas View Post
    Please someone.. :'(
    You quite carefully did not tell us what the errors actually are. My Hat of Guessing would like to remind you that it is not possible to print a long long int using %i, and that you must use %lli instead. (That's lowercase L lowercase L lowercase I.)

  8. #8
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by tabstop View Post
    You quite carefully did not tell us what the errors actually are. My Hat of Guessing would like to remind you that it is not possible to print a long long int using %i, and that you must use %lli instead. (That's lowercase L lowercase L lowercase I.)
    I'm trying to implement unsigned long long int.


    But i'm getting this errors (after replacing every %i into &lli or %llu):

    http://xs126.xs.to/xs126/08142/pic183.jpg

    The green line is given by this command:

    Code:
    char result[2000];
    sprintf(result,"%llu-%llu-%llu]",area_camada,boxes_camada,wires_camada);
    What am i doing wrong? :''''(

  9. #9
    Banned
    Join Date
    Mar 2008
    Posts
    78
    The source code is here:
    http://pastebin.com/m658c4c0b

    I placed a "//////HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" marking the lines or functions where the error begins


    If you dont want to see the whole code, the lines are this ones:

    Code:
        int layers,boxes,wires,bk,s; //Dados Globais
        int camadas[1000],p1,p4,pX; //o pX serve como temporario para qualquer opera&#231;&#227;o posterior
        unsigned long long int area_camada,boxes_camada,wires_camada; ///HERE!!!!!!
        int temp,camada_actual;
        float xmin,xmax,ymin,ymax;
        char c,tmp[3],camadas_dados[5000],*ptr; //ptr usado para receber um digito em char de cada vez
        
        int camadaExisteQ (int cId);
        void dadosCamada (); //Mete os dados da camada no "cache"
        void escreveDados(); //Grava o "cache" da camada actual
        void readNext(); //Procura o proximo valor e guarda-o no argumento
    
    
    (...)
    
    
        void escreveDados() { //Writes the sprintf XXXXXX-XXXXXXX-XXXXXX] for the vars area_camada,boxes_camada,wires_camada
         char result[2000];
         sprintf(result,"&#37;llu-%llu-%llu]",area_camada,boxes_camada,wires_camada);
         printf("!*!*!*!*!*!*!%llu-%llu-%llu]",area_camada,boxes_camada,wires_camada);
         strncpy(camadas_dados+p4,result,strlen(result));
         printf("camada_dados+p4: %s\n",&camadas_dados[p4]);
         p4 += strlen(result); //Deslocar p4 &#224; direita
         area_camada = boxes_camada = wires_camada = 0;
         printf("p4: %i\n", p4);
        }
    
        void dadosCamada () { //Reads the data back to area_camada,boxes_camada,wires_camada
             int s; //0 = area_camada, 1 = boxes_camada, 2 = wires_camada
             bk=pX;
             area_camada = boxes_camada = wires_camada = 0;
             s=0;
             while(camadas_dados[bk] != ']') {
             printf("**BK: %c\n",camadas_dados[bk]);
              if(isdigit(camadas_dados[bk])) {
               area_camada = (s == 0) ? area_camada*10 + (unsigned long long int)(camadas_dados[bk] - '0') : area_camada;
               boxes_camada = (s == 1) ? boxes_camada*10 + (unsigned long long int)(camadas_dados[bk] - '0') : boxes_camada;
               wires_camada = (s == 2) ? wires_camada*10 + (unsigned long long int)(camadas_dados[bk] - '0') : wires_camada;
               printf("c[bk]: %c \t c+bk: %i\n",camadas_dados[bk],wires_camada);
              }
              else s++;
              bk++;
             }
             printf("!!!!!!!!!!!!!!!!!!!%i,%i,%i\n",area_camada,boxes_camada,wires_camada);
             }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you post the variable declarations for:
    area_camada,boxes_camada,wires_camada

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by matsp View Post
    Can you post the variable declarations for:
    area_camada,boxes_camada,wires_camada

    --
    Mats
    Its there, is this one:

    unsigned long long int area_camada,boxes_camada,wires_camada; ///HERE!!!!!!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is dadosCamada the thing that's supposed to read "B 5 5 5 5"? If so, I get that you should get -156565655 out of that. (Since ' '-'0' is negative sixteen, and your "number" is " 5 5 5 5".) I don't think that's quite what you see, but I would expect the problem is in the computation, not the printing.

    Edit: No, I'm wrong, I didn't see the isdigit() thing. So you should get 5555.
    Last edited by tabstop; 04-01-2008 at 09:09 AM.

  13. #13
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by tabstop View Post
    Is dadosCamada the thing that's supposed to read "B 5 5 5 5"? If so, I get that you should get -156565655 out of that. (Since ' '-'0' is negative sixteen, and your "number" is " 5 5 5 5".) I don't think that's quite what you see, but I would expect the problem is in the computation, not the printing.

    Edit: No, I'm wrong, I didn't see the isdigit() thing. So you should get 5555.
    Yes, it must be the computation..

    When i'm using unsigned long long int, what cares should i take manipulating them?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Alright, let's look at this:
    Code:
    box_data[temp] = (box_data[temp] < 0) ? atoi(ptr) : box_data[temp] * 10 + atoi(ptr);
    If box_data is -999 it's supposed to be the first thing, so we do atoi to get the value, BUT: ptr does not point at a legitimate character array -- it starts with c, but then who knows what comes next? (Well, your compiler does, but the compiler is not forced to lay out your variables in any particular way.) So who knows what you get back from atoi?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > But i'm getting this errors (after replacing every &#37;i into &lli or %llu):
    To print a long long int using a MinGW compiler (you didn't say, so I'm making a guess), or one of the IDE's wrapped around it (say Dev-C++ or code::blocks), then you need to use the Microsoft specific format extension to format a long long int, as opposed to the ANSI %ll format.

    The MinGW compiler unfortunately inherits all the bugs/features of the Microsoft runtime, and not an ANSI compliant one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed