#include <stdio.h> // no semicolon after directive
int main()
{
printf("Hello, world!\n"); // 2 spaces indentation
// normal return value for main():
// return 0; // 0 signals no errors
}
/*
gcc -E hello.c // preprocessing
gcc hello.c // compiling
./a.out // running the program
a.out // running the program
Hello, world! // output
gcc hello.c -o hello // specify name of executable
./hello // run program
hello // run program
Hello, world!
*/
Notes: When running programs, do not write the comments in the terminal window, for instance, write
gcc hello.c
./a.out
"." is the current directory. Both
program
and
./program
run the program "program" from the current directory.
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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.
#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!
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
//#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>
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h
int main()
{
printf("Hello, world!\n");
}
/*
gcc hello3.c
hello3.c:1:18: error: missing terminating > character
1 | #include <stdio.h
| ^
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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>
| ^
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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.
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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 | }
| ~
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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 | }
| ~
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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 | }
| ~
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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");
| ^~~~~~
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h>
//int main()
{
printf("Hello, world!\n");
}
/*
gcc hello10.c
hello10.c:4:1: error: expected identifier or ‘(’ before ‘{’ token
4 | {
| ^
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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");
| ^~~~~~~~~~~~~~~~~
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 1-2. Experiment to find out what happens when printf()'s argument string contains \c, where c is some character not listed above.
#include <stdio.h> // for printf()
int main()
{
printf("Hello,\t\'world!\b\'\n");
}
/*
gcc helloc.c
./a.out
Hello, 'world'
*/
Notes: \t is tab, \b is backspace, \' produces ', !\b deletes !
You could also include \" for quotes within the quoted string argument of printf(), as in
printf("Hello, \"world!\"\n");
which produces the output
Hello, "world!"
You can also include \f (form feed), \v (vertical tab), \r (carriage return) or other escape_sequences.
See also quotes.c, Exercise_1-10, and Exercise_3-2.
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for putchar(), printf()
// single and double quotes
int main()
{ // equivalent chars: '"' and '\"', and strings: "'" and "\'"
printf("'\"': "); putchar('"'); printf("\t \"'\": "); printf("'\n");
printf("'\\\"': "); putchar('\"'); printf("\t \"\\'\": "); printf("\'\n");
return 0;
}
/*
gcc quotes.c -o quotes
./quotes
'"': " "'": ' // quotes as a char, apostrophe in a string
'\"': " "\'": ' // escaped quotes as a char, escaped apostrophe in a string
*/