Exercise 1-1 (Hello - error messages)

Chapter_1     Hello Exercise_1-2







Exercise 1-1     K&R, p. 8


Exercise 1-1. Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get.




CONTENTS:     hello1.c     hello2.c     hello3.c     hello4.c     hello5.c     hello6.c     hello7.c     hello8.c     hello9.c     hello10.c     hello11.c




Semicolon after #include directive


hello1.c         download


#include <stdio.h>; // no semicolon after directive

int main()
{
printf("Hello, world!\n");
}
/*
gcc hello1.c // Compiler gives a warning, but creates the executable:
hello1.c:1:19: warning: extra tokens at end of #include directive
1 | #include <stdio.h>; // no semicolon after directive
| ^

./a.out
Hello, world!
*/









Missing (commented out)  #include <stdio.h>


hello2.c         download


//#include <stdio.h>

int main()
{
printf("Hello, world!\n");
}
/*
gcc hello2.c // compile
// Error message:
hello2.c: In function ‘main’:
hello2.c:5:3: warning: implicit declaration of function ‘printf’
[-Wimplicit-function-declaration] // line 5, column 3
5 | printf("Hello, world!\n");
| ^~~~~~
hello2.c:5:3: warning: incompatible implicit declaration of built-in function ‘printf’
hello2.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
+++ |+#include <stdio.h>
1 | //#include <stdio.h>
*/









Missing ending '>' after 'stdio.h'


hello3.c         download


#include <stdio.h

int main()
{
printf("Hello, world!\n");
}
/*
gcc hello3.c
hello3.c:1:18: error: missing terminating > character
1 | #include <stdio.h
| ^
*/









Missing '#' before 'include'


hello4.c         download


include <stdio.h>

int main()
{
printf("Hello, world!\n");
}
/*
gcc hello4.c
hello4.c:1:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
1 | include <stdio.h>
| ^
*/









<stdio>  not recognized, write  <stdio.h>


hello5.c         download


#include <stdio>

int main()
{
printf("Hello, world!\n");
}
/*
gcc hello5.c
hello5.c:1:10: fatal error: stdio: No such file or directory
1 | #include <stdio>
| ^~~~~~~
compilation terminated.
*/









Missing terminating  "  for  printf()  argument


hello6.c         download


#include <stdio.h>

int main()
{
printf("Hello, world!\n);
}
/*
gcc hello6.c
hello6.c: In function ‘main’:
hello6.c:5:10: warning: missing terminating " character
5 | printf("Hello, world!\n);
| ^
hello6.c:5:10: error: missing terminating " character
5 | printf("Hello, world!\n);
| ^~~~~~~~~~~~~~~~~~
hello6.c:6:1: error: expected expression before ‘}’ token
6 | }
| ^
hello6.c:5:10: error: expected ‘;’ before ‘}’ token
5 | printf("Hello, world!\n);
| ^
| ;
6 | }
| ~
*/









Missing ending ')' for  printf()


hello7.c         download


#include <stdio.h>

int main()
{
printf("Hello, world!\n";
}
/*
gcc hello7.c
hello7.c: In function ‘main’:
hello7.c:5:27: error: expected ‘)’ before ‘;’ token
5 | printf("Hello, world!\n";
| ^
| )
hello7.c:5:28: error: expected ‘;’ before ‘}’ token
5 | printf("Hello, world!\n";
| ^
| ;
6 | }
| ~
*/









Missing semicolon after  printf()


hello8.c         download


#include <stdio.h>

int main()
{
printf("Hello, world!\n")
}
/*
gcc hello8.c
hello8.c: In function ‘main’:
hello8.c:5:28: error: expected ‘;’ before ‘}’ token
5 | printf("Hello, world!\n")
| ^
| ;
6 | }
| ~
*/









Missing ending '}' for  main()


hello9.c         download


#include <stdio.h>

int main()
{
printf("Hello, world!\n");
//}
/*
gcc hello9.c
hello9.c: In function ‘main’:
hello9.c:5:3: error: expected declaration or statement at end of input
5 | printf("Hello, world!\n");
| ^~~~~~
*/









Missing  main()  function declaration


hello10.c         download


#include <stdio.h>

//int main()
{
printf("Hello, world!\n");
}
/*
gcc hello10.c
hello10.c:4:1: error: expected identifier or ‘(’ before ‘{’ token
4 | {
| ^
*/









Missing  main()  altogether


hello11.c         download


#include <stdio.h>

//int main()
//{
printf("Hello, world!\n");
//}
/*
gcc hello11.c
hello11.c:5:10: error: expected declaration specifiers or ‘...’ before string constant
5 | printf("Hello, world!\n");
| ^~~~~~~~~~~~~~~~~
*/









Chapter_1     Hello BACK_TO_TOP Exercise_1-2



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo