C language determine length of array depending on amount of digits entered by the userHow do I determine the size of my array in C?Improve INSERT-per-second performance of SQLite?Fastest sort of fixed length 6 int arrayC program: Breaking an int into an int array[]Restrict user input to 8 digit numberC program: Checking digits entered are 1s and 0s only using arraysUnexpected behaviour when scanf used inside while loop to read an arrayArray size different from what is entered - CNeed to write a program that asks the user to fill in a 2D array then checks how many times a digit between 0 and 9 was entered in those two rowsUser defined array elements and array size in C

Who must act to prevent Brexit on March 29th?

Can the electrostatic force be infinite in magnitude?

Invariance of results when scaling explanatory variables in logistic regression, is there a proof?

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

Word describing multiple paths to the same abstract outcome

No idea how to draw this using tikz

Freedom of speech and where it applies

Why is delta-v is the most useful quantity for planning space travel?

How can a jailer prevent the Forge Cleric's Artisan's Blessing from being used?

Is there an Impartial Brexit Deal comparison site?

How do I rename a LINUX host without needing to reboot for the rename to take effect?

Bob has never been a M before

Books on the History of math research at European universities

What will be the benefits of Brexit?

Reply ‘no position’ while the job posting is still there (‘HiWi’ position in Germany)

How to color a zone in Tikz

Giant Toughroad SLR 2 for 200 miles in two days, will it make it?

In Star Trek IV, why did the Bounty go back to a time when whales were already rare?

How can I raise concerns with a new DM about XP splitting?

Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?

Pronouncing Homer as in modern Greek

For airliners, what prevents wing strikes on landing in bad weather?

A workplace installs custom certificates on personal devices, can this be used to decrypt HTTPS traffic?

Proving by induction of n. Is this correct until this point?



C language determine length of array depending on amount of digits entered by the user


How do I determine the size of my array in C?Improve INSERT-per-second performance of SQLite?Fastest sort of fixed length 6 int arrayC program: Breaking an int into an int array[]Restrict user input to 8 digit numberC program: Checking digits entered are 1s and 0s only using arraysUnexpected behaviour when scanf used inside while loop to read an arrayArray size different from what is entered - CNeed to write a program that asks the user to fill in a 2D array then checks how many times a digit between 0 and 9 was entered in those two rowsUser defined array elements and array size in C













1















I have the below code in which a user enters their card number, with this each digit is stored as an element in the variable array digits[].



What I am trying to achieve is to have the for loop stop after it has stored all digits, however, I am struggling to set the loop as per the length of the entered card number.



I have tried the below code using sizeof(cardNo) stored in the variable length and setting length as the loop condition.



Let's say the user enters 1234 the result I am receiving with the below code being run is 43210000, when what I am looking for is just 4321.



if I set int digits to digits[4] and the loop condition to < 4 it will give me 4321 but obviously this restricts me to user only entering 4 digits when in fact I want this to be flexible i.e. if they enter 6 digits it will give me 123456 if they enter 8 digits it will give me 12345678 etc.



Any ideas or suggestions, please?



long cardNo = get_long("Enter Card Number: ");
int length = sizeof(cardNo);
int count = 0;
int digits[length];

for (int i = 0; i < length; i++)
digits[i] = cardNo % 10;
cardNo /= 10;
printf("%in", digits[i]);

printf("n");









share|improve this question



















  • 1





    Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.

    – Eugene Sh.
    Mar 7 at 15:31












  • First: Use malloc to this allocate dynamically memoery. Second: length should be size_t type. I think cardNo too unless you want negative number handle.

    – Igor Galczak
    Mar 7 at 15:33







  • 4





    sizeof(cardNo) is not what you think it is.

    – Jabberwocky
    Mar 7 at 15:36











  • @IgorGalczak It doesn't have to be size_t.

    – StaceyGirl
    Mar 7 at 15:38











  • @StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…

    – Igor Galczak
    Mar 7 at 15:44















1















I have the below code in which a user enters their card number, with this each digit is stored as an element in the variable array digits[].



What I am trying to achieve is to have the for loop stop after it has stored all digits, however, I am struggling to set the loop as per the length of the entered card number.



I have tried the below code using sizeof(cardNo) stored in the variable length and setting length as the loop condition.



Let's say the user enters 1234 the result I am receiving with the below code being run is 43210000, when what I am looking for is just 4321.



if I set int digits to digits[4] and the loop condition to < 4 it will give me 4321 but obviously this restricts me to user only entering 4 digits when in fact I want this to be flexible i.e. if they enter 6 digits it will give me 123456 if they enter 8 digits it will give me 12345678 etc.



Any ideas or suggestions, please?



long cardNo = get_long("Enter Card Number: ");
int length = sizeof(cardNo);
int count = 0;
int digits[length];

for (int i = 0; i < length; i++)
digits[i] = cardNo % 10;
cardNo /= 10;
printf("%in", digits[i]);

printf("n");









share|improve this question



















  • 1





    Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.

    – Eugene Sh.
    Mar 7 at 15:31












  • First: Use malloc to this allocate dynamically memoery. Second: length should be size_t type. I think cardNo too unless you want negative number handle.

    – Igor Galczak
    Mar 7 at 15:33







  • 4





    sizeof(cardNo) is not what you think it is.

    – Jabberwocky
    Mar 7 at 15:36











  • @IgorGalczak It doesn't have to be size_t.

    – StaceyGirl
    Mar 7 at 15:38











  • @StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…

    – Igor Galczak
    Mar 7 at 15:44













1












1








1








I have the below code in which a user enters their card number, with this each digit is stored as an element in the variable array digits[].



What I am trying to achieve is to have the for loop stop after it has stored all digits, however, I am struggling to set the loop as per the length of the entered card number.



I have tried the below code using sizeof(cardNo) stored in the variable length and setting length as the loop condition.



Let's say the user enters 1234 the result I am receiving with the below code being run is 43210000, when what I am looking for is just 4321.



if I set int digits to digits[4] and the loop condition to < 4 it will give me 4321 but obviously this restricts me to user only entering 4 digits when in fact I want this to be flexible i.e. if they enter 6 digits it will give me 123456 if they enter 8 digits it will give me 12345678 etc.



Any ideas or suggestions, please?



long cardNo = get_long("Enter Card Number: ");
int length = sizeof(cardNo);
int count = 0;
int digits[length];

for (int i = 0; i < length; i++)
digits[i] = cardNo % 10;
cardNo /= 10;
printf("%in", digits[i]);

printf("n");









share|improve this question
















I have the below code in which a user enters their card number, with this each digit is stored as an element in the variable array digits[].



What I am trying to achieve is to have the for loop stop after it has stored all digits, however, I am struggling to set the loop as per the length of the entered card number.



I have tried the below code using sizeof(cardNo) stored in the variable length and setting length as the loop condition.



Let's say the user enters 1234 the result I am receiving with the below code being run is 43210000, when what I am looking for is just 4321.



if I set int digits to digits[4] and the loop condition to < 4 it will give me 4321 but obviously this restricts me to user only entering 4 digits when in fact I want this to be flexible i.e. if they enter 6 digits it will give me 123456 if they enter 8 digits it will give me 12345678 etc.



Any ideas or suggestions, please?



long cardNo = get_long("Enter Card Number: ");
int length = sizeof(cardNo);
int count = 0;
int digits[length];

for (int i = 0; i < length; i++)
digits[i] = cardNo % 10;
cardNo /= 10;
printf("%in", digits[i]);

printf("n");






c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 16:02









static_cast

1,07211521




1,07211521










asked Mar 7 at 15:29









CoderCoder

9911




9911







  • 1





    Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.

    – Eugene Sh.
    Mar 7 at 15:31












  • First: Use malloc to this allocate dynamically memoery. Second: length should be size_t type. I think cardNo too unless you want negative number handle.

    – Igor Galczak
    Mar 7 at 15:33







  • 4





    sizeof(cardNo) is not what you think it is.

    – Jabberwocky
    Mar 7 at 15:36











  • @IgorGalczak It doesn't have to be size_t.

    – StaceyGirl
    Mar 7 at 15:38











  • @StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…

    – Igor Galczak
    Mar 7 at 15:44












  • 1





    Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.

    – Eugene Sh.
    Mar 7 at 15:31












  • First: Use malloc to this allocate dynamically memoery. Second: length should be size_t type. I think cardNo too unless you want negative number handle.

    – Igor Galczak
    Mar 7 at 15:33







  • 4





    sizeof(cardNo) is not what you think it is.

    – Jabberwocky
    Mar 7 at 15:36











  • @IgorGalczak It doesn't have to be size_t.

    – StaceyGirl
    Mar 7 at 15:38











  • @StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…

    – Igor Galczak
    Mar 7 at 15:44







1




1





Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.

– Eugene Sh.
Mar 7 at 15:31






Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.

– Eugene Sh.
Mar 7 at 15:31














First: Use malloc to this allocate dynamically memoery. Second: length should be size_t type. I think cardNo too unless you want negative number handle.

– Igor Galczak
Mar 7 at 15:33






First: Use malloc to this allocate dynamically memoery. Second: length should be size_t type. I think cardNo too unless you want negative number handle.

– Igor Galczak
Mar 7 at 15:33





4




4





sizeof(cardNo) is not what you think it is.

– Jabberwocky
Mar 7 at 15:36





sizeof(cardNo) is not what you think it is.

– Jabberwocky
Mar 7 at 15:36













@IgorGalczak It doesn't have to be size_t.

– StaceyGirl
Mar 7 at 15:38





@IgorGalczak It doesn't have to be size_t.

– StaceyGirl
Mar 7 at 15:38













@StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…

– Igor Galczak
Mar 7 at 15:44





@StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…

– Igor Galczak
Mar 7 at 15:44












3 Answers
3






active

oldest

votes


















3














The length of a number is based on its logarithm base 10:



#include <math.h> //log10
....
int len = log10(cardNo) + 1;
...


Don't forget to link with the math library (gcc ... -lm)






share|improve this answer

























  • It's a long not an int. FPU roundoff burns man.

    – Joshua
    Mar 7 at 15:39











  • @Joshua: the argument cardNo is automatically converted to double

    – pmg
    Mar 7 at 15:42












  • @pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)

    – Coder
    Mar 7 at 15:44






  • 3





    This is what logarithms do. log10(x) is the number you have to raise 10 to to obtain x. 10^3 (power, not xor) is 1000, so log10(1000) is 3; I add 1 to get 4. log10(4863) is 3.<something> and it needs 4 digits. The assigment to an integer discards anything after the decimal point.

    – pmg
    Mar 7 at 15:49












  • @Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999

    – Joshua
    Mar 7 at 16:13


















0














Idiom: measure-allocate-generate.



size_t n =0;
for (; i < cardno; )
cardNo /= 10;
n++;

if(!n)n++;//0
int*digits=malloc(n*sizeof(int));





share|improve this answer

























  • sizeof(int) is not a good idea, modifications may yield errors.

    – Jose
    Mar 7 at 16:34











  • @Jose: Calls to malloc without *sizeof() are really bad ideas unless char.

    – Joshua
    Mar 7 at 16:45






  • 2





    I think @Jose meant that int *digits = malloc(n * sizeof *digits); will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;). @Jose: excuse me for butting in :)

    – pmg
    Mar 7 at 20:53












  • Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.

    – Jose
    Mar 8 at 8:11


















0














The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):



#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define STRINGIFY(x) STRINGIFY2(x)
#define STRINGIFY2(x) #x

#define MAX_NUMBERS 16

int main()
char cardNum[MAX_NUMBERS + 1];
size_t sizeCard;

printf("Enter card numbersn");
scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
sizeCard = strlen(cardNum);
for (int i = 0; i < sizeCard; i++)
printf("%cn", cardNum[i]);

printf("n");






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047405%2fc-language-determine-length-of-array-depending-on-amount-of-digits-entered-by-th%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    The length of a number is based on its logarithm base 10:



    #include <math.h> //log10
    ....
    int len = log10(cardNo) + 1;
    ...


    Don't forget to link with the math library (gcc ... -lm)






    share|improve this answer

























    • It's a long not an int. FPU roundoff burns man.

      – Joshua
      Mar 7 at 15:39











    • @Joshua: the argument cardNo is automatically converted to double

      – pmg
      Mar 7 at 15:42












    • @pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)

      – Coder
      Mar 7 at 15:44






    • 3





      This is what logarithms do. log10(x) is the number you have to raise 10 to to obtain x. 10^3 (power, not xor) is 1000, so log10(1000) is 3; I add 1 to get 4. log10(4863) is 3.<something> and it needs 4 digits. The assigment to an integer discards anything after the decimal point.

      – pmg
      Mar 7 at 15:49












    • @Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999

      – Joshua
      Mar 7 at 16:13















    3














    The length of a number is based on its logarithm base 10:



    #include <math.h> //log10
    ....
    int len = log10(cardNo) + 1;
    ...


    Don't forget to link with the math library (gcc ... -lm)






    share|improve this answer

























    • It's a long not an int. FPU roundoff burns man.

      – Joshua
      Mar 7 at 15:39











    • @Joshua: the argument cardNo is automatically converted to double

      – pmg
      Mar 7 at 15:42












    • @pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)

      – Coder
      Mar 7 at 15:44






    • 3





      This is what logarithms do. log10(x) is the number you have to raise 10 to to obtain x. 10^3 (power, not xor) is 1000, so log10(1000) is 3; I add 1 to get 4. log10(4863) is 3.<something> and it needs 4 digits. The assigment to an integer discards anything after the decimal point.

      – pmg
      Mar 7 at 15:49












    • @Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999

      – Joshua
      Mar 7 at 16:13













    3












    3








    3







    The length of a number is based on its logarithm base 10:



    #include <math.h> //log10
    ....
    int len = log10(cardNo) + 1;
    ...


    Don't forget to link with the math library (gcc ... -lm)






    share|improve this answer















    The length of a number is based on its logarithm base 10:



    #include <math.h> //log10
    ....
    int len = log10(cardNo) + 1;
    ...


    Don't forget to link with the math library (gcc ... -lm)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 7 at 15:40

























    answered Mar 7 at 15:35









    pmgpmg

    84.6k999170




    84.6k999170












    • It's a long not an int. FPU roundoff burns man.

      – Joshua
      Mar 7 at 15:39











    • @Joshua: the argument cardNo is automatically converted to double

      – pmg
      Mar 7 at 15:42












    • @pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)

      – Coder
      Mar 7 at 15:44






    • 3





      This is what logarithms do. log10(x) is the number you have to raise 10 to to obtain x. 10^3 (power, not xor) is 1000, so log10(1000) is 3; I add 1 to get 4. log10(4863) is 3.<something> and it needs 4 digits. The assigment to an integer discards anything after the decimal point.

      – pmg
      Mar 7 at 15:49












    • @Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999

      – Joshua
      Mar 7 at 16:13

















    • It's a long not an int. FPU roundoff burns man.

      – Joshua
      Mar 7 at 15:39











    • @Joshua: the argument cardNo is automatically converted to double

      – pmg
      Mar 7 at 15:42












    • @pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)

      – Coder
      Mar 7 at 15:44






    • 3





      This is what logarithms do. log10(x) is the number you have to raise 10 to to obtain x. 10^3 (power, not xor) is 1000, so log10(1000) is 3; I add 1 to get 4. log10(4863) is 3.<something> and it needs 4 digits. The assigment to an integer discards anything after the decimal point.

      – pmg
      Mar 7 at 15:49












    • @Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999

      – Joshua
      Mar 7 at 16:13
















    It's a long not an int. FPU roundoff burns man.

    – Joshua
    Mar 7 at 15:39





    It's a long not an int. FPU roundoff burns man.

    – Joshua
    Mar 7 at 15:39













    @Joshua: the argument cardNo is automatically converted to double

    – pmg
    Mar 7 at 15:42






    @Joshua: the argument cardNo is automatically converted to double

    – pmg
    Mar 7 at 15:42














    @pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)

    – Coder
    Mar 7 at 15:44





    @pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)

    – Coder
    Mar 7 at 15:44




    3




    3





    This is what logarithms do. log10(x) is the number you have to raise 10 to to obtain x. 10^3 (power, not xor) is 1000, so log10(1000) is 3; I add 1 to get 4. log10(4863) is 3.<something> and it needs 4 digits. The assigment to an integer discards anything after the decimal point.

    – pmg
    Mar 7 at 15:49






    This is what logarithms do. log10(x) is the number you have to raise 10 to to obtain x. 10^3 (power, not xor) is 1000, so log10(1000) is 3; I add 1 to get 4. log10(4863) is 3.<something> and it needs 4 digits. The assigment to an integer discards anything after the decimal point.

    – pmg
    Mar 7 at 15:49














    @Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999

    – Joshua
    Mar 7 at 16:13





    @Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999

    – Joshua
    Mar 7 at 16:13













    0














    Idiom: measure-allocate-generate.



    size_t n =0;
    for (; i < cardno; )
    cardNo /= 10;
    n++;

    if(!n)n++;//0
    int*digits=malloc(n*sizeof(int));





    share|improve this answer

























    • sizeof(int) is not a good idea, modifications may yield errors.

      – Jose
      Mar 7 at 16:34











    • @Jose: Calls to malloc without *sizeof() are really bad ideas unless char.

      – Joshua
      Mar 7 at 16:45






    • 2





      I think @Jose meant that int *digits = malloc(n * sizeof *digits); will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;). @Jose: excuse me for butting in :)

      – pmg
      Mar 7 at 20:53












    • Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.

      – Jose
      Mar 8 at 8:11















    0














    Idiom: measure-allocate-generate.



    size_t n =0;
    for (; i < cardno; )
    cardNo /= 10;
    n++;

    if(!n)n++;//0
    int*digits=malloc(n*sizeof(int));





    share|improve this answer

























    • sizeof(int) is not a good idea, modifications may yield errors.

      – Jose
      Mar 7 at 16:34











    • @Jose: Calls to malloc without *sizeof() are really bad ideas unless char.

      – Joshua
      Mar 7 at 16:45






    • 2





      I think @Jose meant that int *digits = malloc(n * sizeof *digits); will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;). @Jose: excuse me for butting in :)

      – pmg
      Mar 7 at 20:53












    • Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.

      – Jose
      Mar 8 at 8:11













    0












    0








    0







    Idiom: measure-allocate-generate.



    size_t n =0;
    for (; i < cardno; )
    cardNo /= 10;
    n++;

    if(!n)n++;//0
    int*digits=malloc(n*sizeof(int));





    share|improve this answer















    Idiom: measure-allocate-generate.



    size_t n =0;
    for (; i < cardno; )
    cardNo /= 10;
    n++;

    if(!n)n++;//0
    int*digits=malloc(n*sizeof(int));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 7 at 16:16

























    answered Mar 7 at 15:38









    JoshuaJoshua

    24.2k550103




    24.2k550103












    • sizeof(int) is not a good idea, modifications may yield errors.

      – Jose
      Mar 7 at 16:34











    • @Jose: Calls to malloc without *sizeof() are really bad ideas unless char.

      – Joshua
      Mar 7 at 16:45






    • 2





      I think @Jose meant that int *digits = malloc(n * sizeof *digits); will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;). @Jose: excuse me for butting in :)

      – pmg
      Mar 7 at 20:53












    • Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.

      – Jose
      Mar 8 at 8:11

















    • sizeof(int) is not a good idea, modifications may yield errors.

      – Jose
      Mar 7 at 16:34











    • @Jose: Calls to malloc without *sizeof() are really bad ideas unless char.

      – Joshua
      Mar 7 at 16:45






    • 2





      I think @Jose meant that int *digits = malloc(n * sizeof *digits); will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;). @Jose: excuse me for butting in :)

      – pmg
      Mar 7 at 20:53












    • Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.

      – Jose
      Mar 8 at 8:11
















    sizeof(int) is not a good idea, modifications may yield errors.

    – Jose
    Mar 7 at 16:34





    sizeof(int) is not a good idea, modifications may yield errors.

    – Jose
    Mar 7 at 16:34













    @Jose: Calls to malloc without *sizeof() are really bad ideas unless char.

    – Joshua
    Mar 7 at 16:45





    @Jose: Calls to malloc without *sizeof() are really bad ideas unless char.

    – Joshua
    Mar 7 at 16:45




    2




    2





    I think @Jose meant that int *digits = malloc(n * sizeof *digits); will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;). @Jose: excuse me for butting in :)

    – pmg
    Mar 7 at 20:53






    I think @Jose meant that int *digits = malloc(n * sizeof *digits); will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;). @Jose: excuse me for butting in :)

    – pmg
    Mar 7 at 20:53














    Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.

    – Jose
    Mar 8 at 8:11





    Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.

    – Jose
    Mar 8 at 8:11











    0














    The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):



    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

    #define STRINGIFY(x) STRINGIFY2(x)
    #define STRINGIFY2(x) #x

    #define MAX_NUMBERS 16

    int main()
    char cardNum[MAX_NUMBERS + 1];
    size_t sizeCard;

    printf("Enter card numbersn");
    scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
    sizeCard = strlen(cardNum);
    for (int i = 0; i < sizeCard; i++)
    printf("%cn", cardNum[i]);

    printf("n");






    share|improve this answer





























      0














      The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):



      #include <stdlib.h>
      #include <stdio.h>
      #include <string.h>

      #define STRINGIFY(x) STRINGIFY2(x)
      #define STRINGIFY2(x) #x

      #define MAX_NUMBERS 16

      int main()
      char cardNum[MAX_NUMBERS + 1];
      size_t sizeCard;

      printf("Enter card numbersn");
      scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
      sizeCard = strlen(cardNum);
      for (int i = 0; i < sizeCard; i++)
      printf("%cn", cardNum[i]);

      printf("n");






      share|improve this answer



























        0












        0








        0







        The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):



        #include <stdlib.h>
        #include <stdio.h>
        #include <string.h>

        #define STRINGIFY(x) STRINGIFY2(x)
        #define STRINGIFY2(x) #x

        #define MAX_NUMBERS 16

        int main()
        char cardNum[MAX_NUMBERS + 1];
        size_t sizeCard;

        printf("Enter card numbersn");
        scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
        sizeCard = strlen(cardNum);
        for (int i = 0; i < sizeCard; i++)
        printf("%cn", cardNum[i]);

        printf("n");






        share|improve this answer















        The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):



        #include <stdlib.h>
        #include <stdio.h>
        #include <string.h>

        #define STRINGIFY(x) STRINGIFY2(x)
        #define STRINGIFY2(x) #x

        #define MAX_NUMBERS 16

        int main()
        char cardNum[MAX_NUMBERS + 1];
        size_t sizeCard;

        printf("Enter card numbersn");
        scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
        sizeCard = strlen(cardNum);
        for (int i = 0; i < sizeCard; i++)
        printf("%cn", cardNum[i]);

        printf("n");







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 8:20

























        answered Mar 7 at 15:53









        JoseJose

        1,291516




        1,291516



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047405%2fc-language-determine-length-of-array-depending-on-amount-of-digits-entered-by-th%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme