0

I was trying c code to add but my program doesn't execute, codeblocks unfortunately closes. What is the error?

void main()
{
    float  a,b;

    printf("%30sAddition Of Numbers\n");
    printf("\nEnter Number 1: ");
    scanf("%f",&a);
    printf("\nEnter Number 2: ");
    scanf("%f",&b);

    printf("\nThe addition of %0.3f and %0.3f is %0.3f",a,b,(a+b));

}

I want to put the result of addition directly in printf statement with float inputs but I am not getting it working.

I also tried putting the result in variable a but it didn't work either.

    void main()
{
    float  a,b;

    printf("%30sAddition Of Numbers\n");
    printf("\nEnter Number 1: ");
    scanf("%f",&a);
    printf("\nEnter Number 2: ");
    scanf("%f",&b);
    a=a+b;
    printf("\nThe addition of %0.3f and %0.3f is %0.3f",a,b,a);

}

where am I going wrong?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
pratik watwani
  • 117
  • 1
  • 2
  • 11
  • what's the error your getting? – Brian Riley Jan 18 '16 at 19:20
  • In what way does it not work? – Amit Jan 18 '16 at 19:21
  • codeblocks doesnt execute. windows error is displayed.. codeblocks unfortunately stopped working – pratik watwani Jan 18 '16 at 19:22
  • The only issue I see directly is that you are not including the header files... but it could be you just excluded it from your example? – Brian Riley Jan 18 '16 at 19:22
  • I have included the header files in my program – pratik watwani Jan 18 '16 at 19:24
  • I just realized now that codeblocks is a compiler. Unfortunately I'm unfamiliar with it - though I believe that this should compile and run just fine in a standard C compiler such as GCC - aside from the issue with the second test - noted by @pFFed – Brian Riley Jan 18 '16 at 19:26
  • 3
    @BrianRiley Codeblocks is *not* a compiler, it's an IDE. On Windows it's usually used with mingw (so gcc, more or less). Sometimes beginners miss the distinction between their IDE and the tools behind it, though. – Dmitri Jan 18 '16 at 19:39
  • You should not use void main, but either int main(void) or int main() – Claudio Cortese Jan 18 '16 at 19:49
  • @Dmitri - Thanks for clearing that up for me :) I had never heard of Codeblocks - my epiphany was that he was talking about software. Without doing more research I should have left it at that rather than saying compiler ;-) – Brian Riley Jan 18 '16 at 20:04
  • @pratikwatwani - _[Here is a good explanation of how to format output](https://www.le.ac.uk/users/rjm1/cotter/page_31.htm)_, including leading spaces before your numeric output. – ryyker Jan 18 '16 at 20:20
  • 1
    a=a+b; printf("\nThe addition of %0.3f and %0.3f is %0.3f",a,b,a); You need another variable c so that c=a+b; printf("\nThe addition of %0.3f and %0.3f is %0.3f",a,b,c); – Bing Bang Jan 18 '16 at 21:02
  • *Code::Blocks* stops working and closes? Not your program? That shouldn't happen even if your program crashes. – user253751 Jan 18 '16 at 21:48

4 Answers4

5

The problem is in the following statement

 printf("%30sAddition Of Numbers\n");

here, the format string supplied to the printf() conatins %30s (or, %s, in general) which is a format specifier (conversion specifier), and you did not supply any argument to it. It invokes undefined behavior.

To quote C11 standard, chapter §7.21.6.1

[...] If there are insufficient arguments for the format, the behavior is undefined. [...]

You can also check the man page to find out more about the format specifiers.


EDIT:

As discussed in the below comments, if you want some spaces to appear before the output, change

printf("\t\tAddition Of Numbers\n");  

That said,

  • void main() should be int main (void), at least, to conform to the standards.
  • You should always check the return value of scanf() to ensure the successful scanning.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

The "%30sAddition Of Numbers\n" issue in your post has been addressed by two good answers (at the time of this post). But you asked a question in comments that may not have been answered completely:

works with %30s when i use all integer numbers and not float! how do i make it work with floats.

A generic answer to that question:
The format specifier you use in scanf(): "%f",&a could result in undesirable results if scanning in unexpected newlines, spaces or other white space. This can be addressed by modifying the format specifier string to suppress these characters. Here is a suggestion:

char* fmt = "%[^\n]%*c";//This generic format specifier, can be used for both integer
                        //and floating point inputs when used in conjuction
                        //with strtod() or strtol() (see below)
scanf(fmt, input);

Explanation of "%[^\n]%*c".

When a user is asked to enter a generic number, it might be a float or an integer. You can accommodate that by creating methods for both, and being specific about what kind of value you would like to process:

float get_float(void)
{
    char input[80];
    char **dummy={0};
    char* fmt = "%[^\n]%*c";
    printf("Enter floating point number and hit return:\n");
    scanf(fmt, input);
    return strtod(input, dummy);
}

long get_int(void)
{
    char input[80];
    char **dummy={0};
    char* fmt = "%[^\n]%*c";
    printf("Enter integer number and hit return:\n");
    scanf(fmt, input);
    return strtol(input, dummy, 10);
}

Called like this:

int main(void)
{
    long integer_var = get_int();
    float float_var = get_float();
    float sum = (float)integer_var + float_var;

    return 0;
}
Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
1
  1. Try adding getch(); function at the bottom before closing curly brackets,

like this

void main()
 {
  
  float  a,b;
  printf("%30sAddition Of Numbers\n");
  printf("\nEnter Number 1: ");
  scanf("%f",&a);
  printf("\nEnter Number 2: ");
  scanf("%f",&b);
  a=a+b;
  printf("\nThe addition of %0.3f and %0.3f is %0.3f",a,b,a);
  getch();//it will hold your output screen so you can see the output 
}
urosc
  • 1,938
  • 3
  • 24
  • 33
0

In this line

printf("%30sAddition Of Numbers\n");

you did not supply a string argument for the %s format. This causes undefined behaviour.

If you want the output spaced, you could try a small modification

printf("%30s\n", "Addition Of Numbers");

in this case you are supplying a string literal to satisfy the %s format.

Additionally you must always check the return value from scanf to see that it did convert the number of arguments it was supposed to. It's a basic newbie error not to, and the root cause of hundreds of SO questions.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56