Thread: to assign a double and output it

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    50

    to assign a double and output it

    hi, so at the moment i have this code

    Code:
        char cint2[10] = "xyz";
        char cint3[10] = "lol";
    
    
        char my_string[] = {""};
        char my_string2[] = {""};
    
    
        printf("\n%s", cint2);
    
    
    
    
        strcpy (cint2,my_string);
        strcpy (cint3,my_string2);
    
    
    
    
    
    
        double double1[2][2] = {{my_string,my_string2,my_string},{my_string2,my_string,my_string2}};
    
        printf("\n%f", double1[2][2]);
    it outputs: 0.000000
    anyone know the correct syntax?
    Last edited by WaterSerpentM; 10-18-2023 at 10:59 AM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Describe in detail in English what you are trying to accomplish. NO Source code!

    Your code presented does not make any sense!

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    Describe in detail in English what you are trying to accomplish. NO Source code!

    Your code presented does not make any sense!
    ok,

    so, in my current exercise, i am starting off with a char,

    then, converting the char into an array,
    then converting the array into a double,
    then outputting the double

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by WaterSerpentM View Post
    ok,

    so, in my current exercise, i am starting off with a char,

    then, converting the char into an array,
    then converting the array into a double,
    then outputting the double
    One character, or several characters together?

    '3' or "3.14"?

    Was this exercise from a book or from a tutorial, or from somewhere else?

    What is the full text of the "exercise"?

  5. #5
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    One character, or several characters together?

    '3' or "3.14"?

    Was this exercise from a book or from a tutorial, or from somewhere else?

    What is the full text of the "exercise"?
    the characters are "xyz" and "lol"

    its my own exercise practicing calculations, variable conversions and outputs
    Last edited by WaterSerpentM; 10-18-2023 at 11:18 AM.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by WaterSerpentM View Post
    the characters are "xyz" and "lol"

    its my own exercise practicing calculations and outputs
    Neither if these strings would ever convert to a double! You need to learn more aut C data types, and a whole lot more first!

  7. #7
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    Neither if these strings would ever convert to a double! You need to learn more aut C data types, and a whole lot more first!
    so, only INT can be a double?

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by WaterSerpentM View Post
    so, only INT can be a double?
    Ints and doubles are two different things!

    123 is an int.
    123.45 is a float or a double depending on the data type of the variable.

    "ABC" is a string of three chars, not an int or a double, and never will be!

    Again, you need to study the language!!!

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    An int can be converted to a float or a double and vice versa, with some data loss.

    There are many more data types than ints and doubles!

    Char, int, short, long, long long, unsigned versions of all int types, etc....

    Study the language!!!

  10. #10
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    Ints and doubles are two different things!

    123 is an int.
    123.45 is a float or a double depending on the data type of the variable.

    "ABC" is a string of three chars, not an int or a double, and never will be!

    Again, you need to study the language!!!
    ye I kind of got muddled up a bit, I mistook double for an array,

    so, all I want to do is,

    create an array with a sequence of chars, then outputting the array,

    is this possible?

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Yes, but away from my computer. Will respond in a couple of hours.

  12. #12
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Look at the following code and the output:
    Code:
    #include <stdio.h>  // For printf() and scanf() functions
    #include <stdlib.h> // For atoi() function
    
    int main(void)
    {
      char ary[64] = "123"; // Initialize all local variables
      int val = 0;
      double dval = 0.0;
      
      printf("The initial string in the array: %s\n\n", ary);
    
      val = atoi(ary);
      printf("The converted string to an int: %d\n\n", val); 
    
      printf("Please enter a double: "); 
      scanf("%lf", &dval);  // The '&' is needed here.  scanf() needs an address
      printf("The double value entered is: %lf\n\n", dval);
    	 
      printf("Please enter a name: ");
      scanf("%s", ary);  // But not here.  ary is an address
      printf("The name entered was: %s\n", ary);
      
      return 0;
    }
    Output:
    Code:
    The initial string in the array: 123
    
    The converted string to an int: 123
    
    Please enter a double: 3.14
    The double value entered is: 3.140000
    
    Please enter a name: John
    The name entered was: John
    Is this what you are trying to do?

    Normally, I would use fgets() to enter strings, but start with scanf().

    I can't teach you the entire language here. You need to get a book and STUDY the language yourself!!!

  13. #13
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    Look at the following code and the output:
    Code:
    #include <stdio.h>  // For printf() and scanf() functions
    #include <stdlib.h> // For atoi() function
    
    int main(void)
    {
      char ary[64] = "123"; // Initialize all local variables
      int val = 0;
      double dval = 0.0;
      
      printf("The initial string in the array: %s\n\n", ary);
    
      val = atoi(ary);
      printf("The converted string to an int: %d\n\n", val); 
    
      printf("Please enter a double: "); 
      scanf("%lf", &dval);  // The '&' is needed here.  scanf() needs an address
      printf("The double value entered is: %lf\n\n", dval);
         
      printf("Please enter a name: ");
      scanf("%s", ary);  // But not here.  ary is an address
      printf("The name entered was: %s\n", ary);
      
      return 0;
    }
    Output:
    Code:
    The initial string in the array: 123
    
    The converted string to an int: 123
    
    Please enter a double: 3.14
    The double value entered is: 3.140000
    
    Please enter a name: John
    The name entered was: John
    Is this what you are trying to do?

    Normally, I would use fgets() to enter strings, but start with scanf().

    I can't teach you the entire language here. You need to get a book and STUDY the language yourself!!!
    that is not what I am trying to do

    as I said before

    I kind of got muddled up a bit, I mistook double for an array,
    I am simply trying to output an array which is a set of chars


    Code:
        char dint1 = "poi";
        char dint2 = "ghj";
        char dint3 = "ggg";
    
    
    
    
    
    
        char array123[3] = {dint1,dint2,dint3};
    
    
        printf("\n%s"), array123[0];
    This not correct, but can kind of show what I am trying to do

  14. #14
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    I would also recommend studying the language from a good book. Asking question after question is a really slow way to learn it (it's a good way to become a Help Vampire though). After all, your questions (and questions you don't know to ask) have already been answered by any good C programming book.

    See also How to Ask Questions the Smart Way.

  15. #15
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by christop View Post
    I would also recommend studying the language from a good book. Asking question after question is a really slow way to learn it (it's a good way to become a Help Vampire though). After all, your questions (and questions you don't know to ask) have already been answered by any good C programming book.

    See also How to Ask Questions the Smart Way.
    thanks,

    I actually have been going through tutorials, so I am able to get a rough insight of how syntax work, its when I can't get it to run ill ask someone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cant get a double type output ??
    By hummingdev in forum C Programming
    Replies: 3
    Last Post: 04-01-2014, 04:13 PM
  2. double/float output is inaccurate.
    By slee8251 in forum C Programming
    Replies: 8
    Last Post: 03-13-2012, 01:27 AM
  3. Comma instead of period in Double output
    By trillykins in forum C Programming
    Replies: 1
    Last Post: 11-19-2010, 09:29 AM
  4. Double output!
    By Spurnout in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2002, 03:35 AM
  5. Floating points and Double output
    By bman1176 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2001, 12:24 AM

Tags for this Thread