I've written this simple code so I can see what's wrong with a more complex program that I have written.
#include<stdio.h>
int main()
{
int n = 0, i = 1, a = 0;
scanf("%d", &n);
while (i <= n)
{
scanf(" %d", &a);
printf("%d", &a);
i++;
}
}
but when I run the program it goes like this:
4
1
6487620
what's wrong with it?
In your code
printf("%d", &a);
should be
printf("%d", a); // don;t print address....
FWIW, passing an address (a pointer type) as an argument to %d is a mismatch and invokes undefined behavior.
When you use
printf("%d", &a);
this means that it will print the address of a
and to print the value of a you have to wright
printf("%d", a);
and after making the changes compile the program and try to rerun :)
You pass the address of a instead of its value to printf. You should also output a linefeed to separate the numbers:
printf("%d\n", a);
Related
Currently I am writing a simple program in C that reads in values the user enters in a loop. For some reason, when I initialize the integer a I am given a random value as opposed to the value I specified. Any help would be greatly appreciated
#include <stdio.h>
int main()
{
char sName[10];
int sTime;
int a = 0;
printf("%d", &a);
printf("Please enter the name of your snail: ");
scanf("%s", &sName);
for(a = 10; a < 20; a = a + 1) {
printf("%d", &a);
printf("Please enter the %d time of your snail: ", &a + 1);
scanf(" %d ", &sTime);
}
return 0;
}
Change this:
printf("%d", &a);
to this:
printf("%d", a);
&a is the address of a (and it's of type int*, so %d is the wrong format). a gives you the value of a.
You still need the & in scanf(" %d ", &sTime);; scanf needs the address of sTime so it knows where to store the value.
You're printing the address of a. You don't want the & in there:
printf("%d", a);
You do want the & for scanf() because you need to tell that function where (at what address) to store the value.
int main()
{
double a = 1;
double b = 3;
int n = 128;
int answer = 0;
printf("select an option(1, 2) ");
scanf("%d", answer);
double y = calcIntegral (answer, a, b, n);
printf("%f \n", y);
system("pause");
return 0;
}
it gets to Scanf and then if accepts the answer but stalls completely and I have to force the task to end. What's going on? This is identical to other programs I've written, I think. I tried using %i as well, and using a char instead of a double for the variable "answer". It says it can't access the memory.
For scanf with modifier d, it matches an optionally signed decimal integer, and the next pointer must be a pointer to int. Says the standard. Also make sure always check scanf return value.
int ret = scanf("%d", &answer);
if (ret != 1) {
// failed to input the number
}
When using scanf(), the variable you read has to be a pointer . So your statement :
scanf("%d", answer);
should be :
scanf("%d", &answer);
as you have declared answer to be an int, so its memory address is a pointer to an int.
On the other hand, if you wanted to read a string and had declared :
char *str;
allocating some memory for it, then the statement would be :
scanf("%s", str);
as str is declared as a pointer to char.
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*'
after printf line program ends itself but i didnt get it why.
#include<stdio.h>
int main ()
{
int Sum,multiply,divide,difference,num1,num2;
char i;
scanf("%d", &s1);
scanf("%d", &s2);
printf("Type initial of your operation : ");
scanf("%c", &i);
return 0 ;
}
There is no way you could compile that, since s1 and s2 are undefined variables.
Thus, any information about what happened when you ran it is moot, since there is no way you could run it.
You meant:
if(scanf("%d %d", &num1, &num2) == 2)
{
printf("Operands are %d and %d, now type initial of desired operation:\n");
if(scanf("%c", &i) == 1)
{
}
}
It's important to check that scanf() has succeeded before relying on the return value.
use scanf(" %c",&i);
there is new line character is present in buffer,so it is not asking for any input and storing it in i.
s1 & s2 are not declared which you are trying to read into them.
I feel they should be num1 & num2 which you been declared as integers.
After printf you just read a char value into i using scanf and main ends doing nothing.