Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 24 Oct 2007 04:28:28 +0200
From:      cpghost <cpghost@cordula.ws>
To:        Harald Schmalzbauer <h.schmalzbauer@omnisec.de>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Mentor for C self study wanted
Message-ID:  <20071024042828.6194532e@epia-2.farid-hajji.net>
In-Reply-To: <200710232324.09851.h.schmalzbauer@omnisec.de>
References:  <200710232044.53240.h.schmalzbauer@omnisec.de> <20071023220134.3abd635e@epia-2.farid-hajji.net> <20071023162454.93851854.wmoran@potentialtech.com> <200710232324.09851.h.schmalzbauer@omnisec.de>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 23 Oct 2007 23:24:09 +0200
Harald Schmalzbauer <h.schmalzbauer@omnisec.de> wrote:

> #include <stdio.h>
>=20
> void main()
> {
>   short nnote;
>=20
>   // Numerischen Notenwert einlesen
>   printf("Bitte numerischen Schulnotenwert eingeben: ");
>   scanf("%d",&nnote);
>=20
>   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=FCgend.",nnote);
>     break;
>     default: printf("%d ist keine zul=E4ssige Schulnote!");
                      ^^^^^                             ^^^^^

No matching int for "%d" here. It'll print garbage. Change to:
default: printf("%hd ist keine...!", nnote);
                                   ^^^^^^^^

>   }
>   printf("\n");
> }
>=20
> P.S.:
> I found that declaring nnote as int soleves my problem, but I
> couldn=C4t 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.

--=20
Cordula's Web. http://www.cordula.ws/



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20071024042828.6194532e>