int main() { return 0; }
All C programs have a main
function.
int main ()
is the function header.
main
has a return type of int
, an integer.
()
defines function parameters.
{}
defines the beginning and end of the function.
{}
.
return 0;
returns an integer value of zero, meaning that all went well.
main()
returns an int
main()
does not return void
The ANSI standard says that main()
returns int
.
It doesn't matter if void
works for another compiler.
We teach ANSI C in this class, not Microsoft C or GNU C or any particular compiler.
int main() { /* There may be one or more lines in this type of comment. */ return 0; // Comment to the end of line }
Programs may have comments.
//
starts a comment, the rest of the line is ignored by the compiler
// something // else
/* ... */
is a block comment that can span lines
/*
is ignored within a comment. The first matching */
ends the comment.
#include <stdio.h> // Preprocessor Directive int main() { // Function // Print Hello, world // Comment printf("Hello, world!\n"); // Function Call return 0; // Function return value }
Declarations appear at the top of the program
#include <stdio.h>
#include
<stdio.h>
.h
= header file
#include <stdio.h> // Preprocessor Directive int main() { // Function // Print Hello, world // Comment printf("Hello, world!\n"); // Function Call return 0; // Function return value }
Functions defined by the header file are used in the program.
printf ("Hello, world!\n");
printf
= name of function
( )
are parameters
printf
prints (displays) the parameter values to the screen
\n
= new line character
pico
, vim
, or whatever editor you like.
c11
is just like using gcc -std=c11
.
#
.
c11
options you won’t need to
remember, but some are used often.
-o
file — make an executable program called file
instead of the default program a.out
. % c11 -o hello hello_world.c
-v
— give verbose output, show the commands being executed
--help
— give some basic usage information about the compiler
-Wall
— give tons of useful warnings
% cp ~cs156/pub/hello_world.c . % ls -l total 4 -r-------- 1 cs156 class 84 Dec 22 02:17 hello_world.c % cat hello_world.c #include <stdio.h> int main() { printf("Hello, world!\n"); return 0; } % c11 -Wall hello_world.c % ls -l total 24 -rwx------ 1 cs156 class 18104 Dec 22 02:17 a.out -r-------- 1 cs156 class 84 Dec 22 02:17 hello_world.c % ./a.out Hello, world!
% cp ~cs156/pub/hello_world.c . % ls -l total 4 -r-------- 1 cs156 class 84 Dec 22 02:17 hello_world.c % cat hello_world.c #include <stdio.h> int main() { printf("Hello, world!\n"); return 0; } % c11 -Wall -o hello hello_world.c % ls -l total 24 -rwx------ 1 cs156 class 18104 Dec 22 02:17 hello -r-------- 1 cs156 class 84 Dec 22 02:17 hello_world.c % ./hello Hello, world!
% cp ~cs156/pub/hello_world.c . % c11 -Wall -o hello hello_world.c % ls -l total 24 -rwx------ 1 cs156 class 18104 Dec 22 02:17 hello -r-------- 1 cs156 class 84 Dec 22 02:17 hello_world.c % c11 -Wall -o hello_world.c hello hello: In function `_fini': (.fini+0x0): multiple definition of `_fini' /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.fini+0x0): first defined here hello: In function `data_start': (.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.data+0x0): first defined here hello:(.rodata+0x8): multiple definition of `__dso_handle' /usr/lib/gcc/x86_64-redhat-linux/8/crtbegin.o:(.rodata+0x0): first defined here hello:(.rodata+0x0): multiple definition of `_IO_stdin_used' /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here hello: In function `_dl_relocate_static_pie': (.text+0x30): multiple definition of `_dl_relocate_static_pie' /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text[.text.group]+0x0): first defined here hello: In function `_start': (.text+0x0): multiple definition of `_start' /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text+0x0): first defined here hello: In function `_init': (.init+0x0): multiple definition of `_init' /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.init+0x0): first defined here /usr/lib/gcc/x86_64-redhat-linux/8/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__' hello:(.data+0x8): first defined here /bin/ld: error in hello(.eh_frame); no .eh_frame_hdr table will be created. collect2: error: ld returned 1 exit status % ls -l total 20 -rwx------ 1 cs156 class 18104 Dec 22 02:17 hello
Disaster results from -o
in the wrong order!