Calculate the cost of cement for a project I am a newbie in a programming, and I just randomly chose a task for training from some group on Facebook. The task is to calculate the cost of cement for a construction project. The input is the number of pounds of cement required (guaranteed not to be a multiple of 120). The store sells cement in 120-pound bags, each costing $45.

Example input: 295.8
Output: 135



#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define PRICE 45
#define CAPACITY 120
#define MAXDIGITS 5

int sum(int);

int main(int argc, char *argv[])
{
    int val = 0;
    char inp[MAXDIGITS];

    if ((argc &gt; 1) &amp;&amp; (argv[1] &gt; 0))
        val = strtol(argv[1], NULL, 0);
    else
    {
        do 
        {
            printf("Please input the value of cement: ");
            scanf("%s", inp);
            val = strtol(inp, NULL, 0);
        }
        while (!val);
    }

    if (val)
        printf("Money you need: %d\n", sum(val));

    return 0;
}

int sum(int need)
{
    int mon = PRICE;
    int n = CAPACITY;

    while (n &lt; need)
    {
        n += CAPACITY;
        mon += PRICE;
    }

    return mon;
}


I'm interested in code style, rational memory usage, etc.
Buffer overflow
Consider what entering &quot;295.8&quot; does.
#define MAXDIGITS 5
char inp[MAXDIGITS];
printf(&quot;Please input the value of cement: &quot;);
scanf(&quot;%s&quot;, inp);

Code used the dangerous scanf(&quot;%s&quot;, inp); with no width limits.  scanf(&quot;%s&quot;, inp); being ignorant of the size of inp[], stored the 5 read characters and the appended null character.
This results in undefined behavior (UB).  Code may work as expected today and fail in strange ways tomorrow.
I recommend to only use fgets() for user input.  After reading a line of user input, it is saved as a string.  Then parse the string.
No need for such a tight buffer size.  Recommend twice the expected max width needed.
#define BUFFER_SIZE (2*MAXDIGITS + 1)
char buffer[BUFFER_SIZE];

printf(&quot;Please input the value of cement: &quot;);
fflush(stdout);  // Insure output is seen before reading

fgets(buffer, sizeof buffer, stdin);
double val = strtod(buffer, NULL);