Mentor for C self study wanted
cpghost
cpghost at cordula.ws
Tue Oct 23 19:28:36 PDT 2007
On Tue, 23 Oct 2007 23:24:09 +0200
Harald Schmalzbauer <h.schmalzbauer at omnisec.de> wrote:
> #include <stdio.h>
>
> void main()
> {
> short nnote;
>
> // Numerischen Notenwert einlesen
> printf("Bitte numerischen Schulnotenwert eingeben: ");
> scanf("%d",&nnote);
>
> switch (nnote)
> {
> case 1: printf("Die Note %d entspricht sehr gut.",nnote);
> break;
> case 2: printf("Die Note %d entspricht gut.",nnote);
> break;
> case 3: printf("Die Note %d entspricht befriedigend.",nnote);
> break;
> case 4: printf("Die Note %d entspricht ausreichend.",nnote);
> break;
> case 5: printf("Die Note %d entspricht mangelhaft.",nnote);
> break;
> case 6: printf("Die Note %d entspricht ungenügend.",nnote);
> break;
> default: printf("%d ist keine zulässige Schulnote!");
^^^^^ ^^^^^
No matching int for "%d" here. It'll print garbage. Change to:
default: printf("%hd ist keine...!", nnote);
^^^^^^^^
> }
> printf("\n");
> }
>
> P.S.:
> I found that declaring nnote as int soleves my problem, but I
> couldnÄt understand why.
> Another one was the result of default: nnote was -1077942208 instead
> of 9 for example.
The reason for this is that the number of arguments after printf's
format string MUST match the number of %-place holders (unless
you're using exotic stuff like %n, of course). If printf misses
some arguments, it will fetch them from a place that is
implementation dependant (and that almost always means:
you'll get garbage).
Sorry for overlooking your second question... ;)
-cpghost.
--
Cordula's Web. http://www.cordula.ws/
More information about the freebsd-questions
mailing list