Date: Tue, 23 Oct 2007 23:32:53 +0100 From: Bruce Cran <bruce@cran.org.uk> To: Derek Ragona <derek@computinginnovations.com> Cc: freebsd-questions@freebsd.org, Harald Schmalzbauer <h.schmalzbauer@omnisec.de> Subject: Re: Mentor for C self study wanted Message-ID: <471E7695.1050702@cran.org.uk> In-Reply-To: <6.0.0.22.2.20071023171535.0244b420@mail.computinginnovations.com> 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> <6.0.0.22.2.20071023171535.0244b420@mail.computinginnovations.com>
next in thread | previous in thread | raw e-mail | index | archive | help
Derek Ragona wrote: > At 04:24 PM 10/23/2007, Harald Schmalzbauer wrote: >> Am Dienstag, 23. Oktober 2007 22:24:54 schrieb Bill Moran: >> > In response to cpghost <cpghost@cordula.ws>: >> > > On Tue, 23 Oct 2007 20:44:52 +0200 >> > > >> > > Harald Schmalzbauer <h.schmalzbauer@omnisec.de> wrote: >> > > > The first one was for example the attached code: Why does it >> segfault? >> > > >> > > Mailman ate the attachment... Can't see it here. >> > >> > I may be out of line, but I think if you're using FreeBSD as your >> > learning platform, that it wouldn't be a problem to ask this list. >> >> Harald Schmalzbauer wrote: >> > > Am Dienstag, 23. Oktober 2007 21:43:52 schrieben Sie: >> > >>> Hello Aryeh, >> > >>> >> > >>> I'm willing to pay fair fees, but are you interested in "micro >> payment" >> > >>> ;) ? >> > >> >> > >> For other reasons I love micro payments. >> > >> >> > >>> Serious, I'll have the one or other short question per week (I'm >> > >>> usually busy, just making spare time lessons from my book (UTC-1 >> spare >> > >>> time)). >> > >> >> > >> Just so I know what level to present on what is your background >> in CS >> > >> and programming? >> > > >> > > CS?? >> > > I'm able to solve problems analytically, but I don't know any >> language >> > > really well. >> > > I know bourne shell, csh, pascal, and basic. And a tiny bit asm, but >> > > that's been on ZX81. >> > >> > Although, you'll have to include your code inline to get past the >> > sanitizers. >> >> Thanks all, >> >> here was my example, just for completeness, I found mentors for my needs. >> >> Thanks a lot to all! >> >> >> #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!"); >> } >> 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. > > if you check the man page on scanf: > d Matches an optionally signed decimal integer; the next pointer must > be a pointer to int. > > You shouldn't try to put a short into an int. Always declare the > correct size for variables. Your segv is because scanf was trying to > put an int where it won't fit. > > You will get the same result if you go off the end of an array. > > -Derek > It's well worth increasing the number of warnings enabled when writing C code, to catch any errors early on. -Wall catches this sort of mistake. Compiling your code gives the following output: > gcc -Wall test.c -o test test.c:4: warning: return type of 'main' is not 'int' test.c: In function 'main': test.c:9: warning: format '%d' expects type 'int *', but argument 2 has type 'short int *' -- Bruce Cran
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?471E7695.1050702>