1

I'm a CS student about to start my second year. In preparation, I'm reading "An Introduction to C Programming for Java Programmers" by Mark Handley.

In it I found this code, that I decided to try out for myself:

#include <stdlib.h>
main()
{
char *s1;
char s2[80];
char *s3;
s1 = "Hello";
s3 = malloc(80);
printf("Enter your first name:\n");
fgets(s2, 80, stdin);
printf("Enter your last name:\n");
fgets(s3, 80, stdin);
printf("%s %s %s\n", s1, s2, s3);
free(s3);
}

I get the following errors when trying to compile with GCC:

strings.c: In function ‘main’:
strings.c:11: warning: incompatible implicit declaration of built-in     `
`function ‘printf’
strings.c:12: error: ‘stdin’ undeclared (first use in this function)
strings.c:12: error: (Each undeclared identifier is reported only once
strings.c:12: error: for each function it appears in.)

Like I said, this is code copied right from the text, so I'm a little frustrated it doesn't work. I figure this must do with the version of GCC. If anyone could help me move in the right direction to understand what is going on here, I would appreciate it. Thanks!

Update: Changed the code per the helpful suggestions below, and it works. It didn't explicitly say to try this code in the text, I took it upon myself to do that. I think the author just intended to show how strings can be handled in C, and how that is different from Java. Thanks everyone, I really appreciate it!

He-Man
  • 75
  • 1
  • 6
  • 3
    Is that a book? Amazon doesn't seem to sell it. What's the publication date? That code hasn't been valid C since.... well, about 5 years before Java was invented, so it was never good code for a Java programmer to learn, and I would throw away that tutorial/e-book/whatever it is. – Ben Voigt Sep 26 '15 at 05:30
  • 2
    Get one of the books off this list instead: http://stackoverflow.com/q/562303/103167 – Ben Voigt Sep 26 '15 at 05:31
  • '#include "stdio.h"' and it will be fine. – A.S.H Sep 26 '15 at 05:33
  • 2
    @A.S.H: No, it won't. `main()` is missing a return type. "implicit `int`" was a thing before 1989 when the first ANSI C standard was ratified. Like I said, there's no reason for any text in the Java era to have that style. – Ben Voigt Sep 26 '15 at 05:34
  • 2
    it is apparently taking implicit declarations, as it did with printf... assume int returned. It is a bad habit though, not good for teaching. I agree with the advices above... throw this book in the bin – A.S.H Sep 26 '15 at 05:36
  • @BenVoigt I think they mean "fine" as in "it compiles with gcc". – PC Luddite Sep 26 '15 at 05:36
  • @PCLuddite: Not cleanly, you'll get a diagnostic – Ben Voigt Sep 26 '15 at 05:38
  • Compile with `gcc -Wall -Wextra -g -std=c99` – Basile Starynkevitch Sep 26 '15 at 05:40
  • @BenVoigt True, I was just acting as a translator. – PC Luddite Sep 26 '15 at 05:45
  • of course I meant it will compile. plz see my second note :) – A.S.H Sep 26 '15 at 05:47
  • Hey, thanks for the responses. @Ben Voigt there doesn't seem to be a publication date. It is a handout from my teacher. It doesn't explicitly say in the text to try to run the code. It's meant to be an example of the ways strings can be handled, and how this is different from Java. After I included stdio.h and changed it to int main(void) and returned 0 at the end, it compiled just fine. Still, it would have been nice if it worked as is. Thanks again for all your responses I really appreciate it! I feel so much better now that I've seen it work! – He-Man Sep 26 '15 at 06:22
  • @He-Man : Had it been intended as a fragment rather than a working example why include stdlib (for malloc/free) but not stdio, or for that matter wrap it within main? This example appears to be about I/O and memory management rather than string handling - it does not use string.h. There is much else to criticise with this code such as magic numbers and lack of conventional indentation. – Clifford Sep 26 '15 at 06:53

2 Answers2

3

1. Include header <stdio.h> (in which printf , fgets are defined)

2. main() -> int main(void) or int main(int argc, char **argv)

3. add return 0; (indicating success and a good practice).

ameyCU
  • 16,489
  • 2
  • 26
  • 41
2

Your code relies on an implicit declaration of printf, fgets, and stdin, and an old signature for main(). It doesn't compile because it's not currently valid C (it's not even valid C89, the first C standard).

You need to include stdio.h where these definitions are located. GCC allows you to use that signature for main() as an extension, but it's still not technically valid C.

#include <stdlib.h>
#include <stdio.h> 

int main(void)
{
    char *s1;
    char s2[80];
    char *s3;
    s1 = "Hello";
    s3 = malloc(80);
    printf("Enter your first name:\n");
    fgets(s2, 80, stdin);
    printf("Enter your last name:\n");
    fgets(s3, 80, stdin);
    printf("%s %s %s\n", s1, s2, s3);
    free(s3);
    return 0; /* not necessary, but it's good practice */
}
PC Luddite
  • 5,883
  • 6
  • 23
  • 39