Was ist am Programm verkehrt?
#include<stdio.h>
#include<stdlib.h>
void main()
{
short x,y,z;
printf("2 Ganzzahlen eingeben:
");
scanf("%d %d", &x, &y);
if(x<y)
z=y-x;
else if(x==y)
z=0;
else
z=x-y;
printf("Resultat: %d
", z);
exit(0);
}
Bei gleicher Zahl Eingabe kommt die eingegene Zahl heraus statt null bei der Anweisung nach else kommt die zu letzt eingegene Zahl heraus!!
Bis bald
Andreas
Anfaenger
Re: Anfaenger
> Was ist am Programm verkehrt?
>
> void main()
Falsch deklariert. Rückgabewert "int" muss angegeben werden.
> short x,y,z;
Ersetz mal das "short" durch "int", dann klappts auch.
Wenn du mit scanf() nach "%d" scannst, dann musst du auch als Variable eine int-Variable verwenden damit das ganze klappt.
Steht aber auch in der Manpage zu scanf(): "%d Matches an optionally signed decimal integer; the next pointer must be a pointer to int."
<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y, z;
printf("2 Ganzzahlen eingeben:\<!--no-->n");
scanf("%d %d", &x, &y);
if(x < y)
{
z = y - x;
}
else if(x == y)
{
z = 0;
}
else
{
z = x - y;
}
printf("Resultat: %d\<!--no-->n", z);
exit(0);
}
oder kürzer:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y, z;
printf("2 Ganzzahlen eingeben:\<!--no-->n");
scanf("%d %d", &x, &y);
z = x == y ? 0 : x < y ? y - x : x - y;
printf("Resultat: %d\<!--no-->n", z);
exit(0);
}
</font><hr></pre></blockquote>
>
> void main()
Falsch deklariert. Rückgabewert "int" muss angegeben werden.
> short x,y,z;
Ersetz mal das "short" durch "int", dann klappts auch.
Wenn du mit scanf() nach "%d" scannst, dann musst du auch als Variable eine int-Variable verwenden damit das ganze klappt.
Steht aber auch in der Manpage zu scanf(): "%d Matches an optionally signed decimal integer; the next pointer must be a pointer to int."
<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y, z;
printf("2 Ganzzahlen eingeben:\<!--no-->n");
scanf("%d %d", &x, &y);
if(x < y)
{
z = y - x;
}
else if(x == y)
{
z = 0;
}
else
{
z = x - y;
}
printf("Resultat: %d\<!--no-->n", z);
exit(0);
}
oder kürzer:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y, z;
printf("2 Ganzzahlen eingeben:\<!--no-->n");
scanf("%d %d", &x, &y);
z = x == y ? 0 : x < y ? y - x : x - y;
printf("Resultat: %d\<!--no-->n", z);
exit(0);
}
</font><hr></pre></blockquote>