i'm cs student start second year. in preparation, i'm reading "an introduction c programming java programmers" mark handley.
in found code, decided try out myself:
#include <stdlib.h> main() { char *s1; char s2[80]; char *s3; s1 = "hello"; s3 = malloc(80); printf("enter first name:\n"); fgets(s2, 80, stdin); printf("enter last name:\n"); fgets(s3, 80, stdin); printf("%s %s %s\n", s1, s2, s3); free(s3); }
i following errors when trying compile 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 function) strings.c:12: error: (each undeclared identifier reported once strings.c:12: error: each function appears in.)
like said, code copied right text, i'm little frustrated doesn't work. figure must version of gcc. if me move in right direction understand going on here, appreciate it. thanks!
update: changed code per helpful suggestions below, , works. didn't explicitly try code in text, took upon myself that. think author intended show how strings can handled in c, , how different java. everyone, appreciate it!
your code relies on implicit declaration of printf
, fgets
, , stdin
, , old signature main()
. doesn't compile because it's not valid c (it's not valid c89, first c standard).
you need include stdio.h
these definitions located. gcc allows use signature main()
extension, 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 first name:\n"); fgets(s2, 80, stdin); printf("enter last name:\n"); fgets(s3, 80, stdin); printf("%s %s %s\n", s1, s2, s3); free(s3); return 0; /* not necessary, it's practice */ }
Comments
Post a Comment