Every time I build and run my project file, it crashes once I interact with it.
#include <stdio.h>
int main()
{
float complexnumber, a, b, r, j, theta;
j = -1;
complexnumber = a+b*j;
printf ("Please enter intput A and B in the form of a+bj\n");
printf ("Input A:");
scanf ("%f" , a);
printf ("Input B:");
scanf ("%f" , b);
theta = atan (a/b);
printf ("Theta=\n" , theta);
r = sqrt (pow(a, 2) + pow(b , 2));
printf ("R=\n" , r);
return 0;
}
Any help is much appreciated
scanf ("%f" , a);
scanf needs a pointer to the variable it shall fill, so that must be
scanf ("%f" , &a);
and similarly for b.
It might be a good idea to include the header file <math.h>.
Your printf() statement
printf ("Theta=\n" , theta);
looks incorrect,
it should be,
printf ("Theta=%f\n" , theta);
Similarly,
printf ("R=%f\n" , r);
Your scanf() statement is also wrong, it should be
scanf("%f",&a);
The line complexnumber = a+b*j; will assign garbage value to complexnumber as both a and b are uninitialized.
Related
using a simple code:
#include <stdio.h>
int main(int argc, int **argv)
{
int A, B, C, D, E, F;
printf ("input 1 : ");
scanf ("%d", &A);
printf ("input 2 : ");
scanf ("%d", &B);
C = A + B;
D = A - B;
E = A * B;
F = A / B;
printf ("sum : %d\n", C);
printf ("difference : %d\n", D);
printf ("product : %d\n", E);
printf ("quotient : %d\n", F);
return 0;
}
My question is as such, in the first scanf [p.s I know I can use other input methods its for a project] if you input a float/double number such as 1.3 or 20.5
the sum and difference are quite random for me,anyone can explain to me why the results are what they are?
Continuing from the comments, you must always validate all input (especially user input). All input functions provide a return. The scanf family returns the match count, the number of successful conversions processed based on the number of format specifiers in the format string. You use that to validate your input. E.g., at minimum:
#include <stdio.h>
int main(int argc, int **argv)
{
int A, B, C, D, E, F;
printf ("input 1 : ");
if (scanf ("%d", &A) != 1) {
fprintf (stderr, "error: invalid input - A.\n");
return 1;
}
printf ("input 2 : ");
if (scanf ("%d", &B) != 1 || B == 0) {
fprintf (stderr, "error: invalid input - B.\n");
return 1;
}
C = A + B;
D = A - B;
E = A * B;
F = A / B;
printf ("sum : %d\n", C);
printf ("difference : %d\n", D);
printf ("product : %d\n", E);
printf ("quotient : %d\n", F);
return 0;
}
note: your "quotient" will always be the result of integer division and truncated accordingly.
The first scanf(), if the input has a decimal point will stop at the decimal point and leave it to be read on the next operation. For input of 1.3, this means A will get the value 1, and the .3 will remain in the input stream to be read. This is because of the %d format - which tells scanf() to expect an integral value, and to stop on any characters that do appear in any representation of an integral value. A decimal point is one such character that is not used in the representation of an integral value.
The next operation (scanf("%d", &B)) immediately encounters the decimal point and returns, without changing B.
Since B is not initialised at all (before, during, or after the scanf("%d", &B)) in the program, any subsequent attempt to access its value gives undefined behaviour. Among other things, this can mean the value of B is indeterminate. From what you describe, for your setup, that results in "random" input.
If you're expecting to read input that looks like floating point values (e.g. that contains a decimal point) either read as floating point (e.g. %f format, and variables of a floating point type) or read a whole line (e.g. using fgets()) and check the contents of the line BEFORE trying to read integral values from it.
If you insist on using scanf(), check its return value. On the second call of scanf() in your scenario, scanf() will return 0 rather than 1 (since it has read no values, rather than the one value the format string specified). Which is an indication that something has gone wrong reading the input.
I'm running some simple code in my CodeBlocks and I wonder why scanf function cannot work with shorts correctly!
The code below is an example. The code takes from the user three int numbers and then prints them again, that simple — but the values printed don't match the values entered.
#include <stdio.h>
int main()
{
short x, y, z;
printf("Please enter three integers! ");
scanf("%d %d %d", &x, &y, &z);
printf("\n num1 = %d , num2 = %d , num3 = %d ", x, y, z);
return 0;
}
The specifier %d is only used for int variables, but in case of short you must use the %hi specifier instead of %d.
So your code must be :
#include <stdio.h>
int main() {
short x , y , z ;
printf("Please Enter three int Numbers ! ");
scanf("%hi %hi %hi",&x,&y,&z);
printf("\n num1 = %hi , num2 = %hi , num3 = %hi ",x,y,z);
return 0;
}
You can find more information about the C data types and their Specifiers here :
https://en.wikipedia.org/wiki/C_data_types
short != int
you pass the pointer to the (usually 2 byte) data, and scanf expects and writes 4 bytes
change short x , y , z ; to int x , y , z ;
as always scanf is not buggy, but the coder is :)
PS Forgot to add. you can also use h format modifier. There is hh as well if you want to scan char sized variables
Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.
I use Bloodshed DevC++ to compile this code.
But I get problem with scanf float number.
And I don't want get decimal type, but the result must in float type,
But the result always be 0.000000.
This is the code:
#include <stdio.h>
main()
{
float A, B, C;
B = 3.14;
C = (4*B*A*A);
printf (" Enter value of A : "); scanf("%f", &A);
printf ("\n");
printf (" So, the result is : %f", C);
printf ("\n");
}
And result is:
Float.jpg
Enter value of A : 22.45
So, the result is : 0.000000
--------------------------------
Process exited after 7.235 seconds with return value 0
Press any key to continue . . .
It would be better if anyone help me, Thanks :)
When you do C = (4*B*A*A);, this doesn't mean that C will always contains what that expression evaluates to. It means that you're modifying C based on the value of that expression at that moment.
So changing A after this point has no effect on the value of C. And since A is uninitialized, this results in undefined behavior, meaning the results are not necessarily predictable.
You need to first read in the value of A, then modify C based on the current values of A and B:
printf (" Enter value of A : "); scanf("%f", &A);
C = (4*B*A*A);
printf ("\n");
printf (" So, the result is : %f", C);
printf ("\n");
To do what you though your original code would do, you'd need to define a function that will calculate the value of C when you call it.
#include <stdio.h>
int C(int A, int B)
{
return 4*B*A*A;
}
int main()
{
float A, B;
B = 3.14;
printf (" Enter value of A : "); scanf("%f", &A);
printf ("\n");
printf (" So, the result is : %f", C(A,B));
printf ("\n");
}
You are calculating the "result" before you ask for input:
float A, B, C;
B = 3.14;
C = (4*B*A*A); // What value has A here? Probably 0 !!
printf (" Enter value of A : "); scanf("%f", &A); // This has no influence
// whatsoever on the
// value of C
printf ("\n");
printf (" So, the result is : %f", C);
printf ("\n");
I am not sure about C (question was tagged C++ before), but I guess A gets initialized to 0, thus the result is indeed exactly 0.
PS: a kind compiler would issue a warning, something like "Warning: variable A is used uninitialized."
I have an error code I do not understand:
format %d expects type int, but argument 2 has type int *
I do not know the difference between int and int *. I did not know there were different types of int, and cannot find any note of it on webpages about printf and scanf key letters.
The code is as follows:
#include <stdio.h>
#include <math.h>
int main(void)
{
int X = 0, Y = 0, A = 0, D = 0;
printf("This program computes the area of a rectangle ");
printf("and its diagonal length.");
printf("Enter Side 1 dimentions: ");
scanf("%d", &X);
printf("Enter Side 2 dimentions: ");
scanf("%d", &Y);
/* Calc */
A = X * Y;
D = pow(X,2) + pow(Y,2);
D = pow(D, 1 / 2);
/* Output */
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
return 0;
}
The error references the last two printf's:
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
Additionally, this program was originally written using floats (declaring X,Y,A, and D as float and using %f). But that gave an even stranger error code:
format %f expects type double, but argument 2 has type float *
I knew that %f is used for doubles and floats, so I could not understand why I had this error. After I got the error code about floats/doubles I tried changing everything to int (as shown in the above code), just to check. But that delivered the error code at the top of this post, which I do not understand either.
I've been using the gcc compiler.
Would someone explain what's being done wrong?
The problem is that you're trying to pass pointers to the printf function. Here's what your code looks like:
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
A is the int variable, but &A is a pointer to the int variable. What you want is this:
printf("Rectangle Area is %d sq. units.", A);
printf(" Diagonal length is %d.", D);
int* means a pointer to an int object. this is what you get because you use & before the variable name (i.e &A in your code)
You can read this to understand more about pointers and references, but basically if you omit the & before the variable names, it will work fine.
Why passing pointers to printf("...%d...", &D)?
Take a look to pointers explanation:
http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers
And to simplified printf() manual:
http://www.cplusplus.com/reference/cstdio/printf/
int d = 1;
printf("I'm an integer: %d", 42); // OK, prints "...42"
printf("I'm an integer too: %d", d); // OK, prints "...1"
printf("I'm a pointer, I have no idea why you printing me: %p", (void*)&d); // OK, prints "...<address of d>", probably not what you want
printf("I'm compile-time error: %d", &d); // Fail, prints comiper error: 'int' reqired, but &d is 'int*'