#include <stdio.h>
/* This is a comment
* it can extend through several lines
*/
int
main (int argc, char *argv[])
{
puts ("Hello, world!"); /* Another comment*/
puts ("--------------------\n"
"We can concatenate several strings\n"
"to define long texts\n"
"----------------------------\n");
return 0;
}The C language is one of those languages that requires us to write
some things before we can actually start writing our program. As we can
see, we need to define at least the main function (more on
this in a later chapter) which will be the entry point of the program.
For the moment, we just need to know that this function is declared in
the way you see in the code above. We will also need to use some
#include directives.
These includes or header files contain the definitions of the functions that we use in our programs and which are implemented elsewhere, such as in the standard C library. These definitions are what in C jargon are known as prototypes, and their main function is to indicate to the compiler the types of the parameters and the type of the value that a particular function returns. This information is necessary to compile the program correctly and to detect errors during compilation.
In our small program, the header file stdio.h is the one
that contains the definition of the puts function. This
function allows us to write strings to the screen. If you have doubts
about which header file to use, simply consult the manual page for the
function in question (man 3 puts in this case).
I’M A NEWBIE
In this case where we only use one function and no data types, we could replace the
includeat the beginning of the program with the prototype of the function we want to use,putsin this case. You can get the prototype from the.hfile or directly from the manual page. The program would look like this:/* Este es un comentario * que puede extenderse varias lineas */ int puts (const char*); int main (int argc, char *argv[]) { ...}
The puts function from the standard library allows us to
write strings to the console as we have already seen. This function
always adds a carriage return at the end of the string it writes. The
carriage return is represented in almost all languages with the ASCII
control character \n. In the multiline example, we can see
how \n characters are interspersed in the string to
separate the string into different lines.
DID YOU KNOW…
Control characters in the ASCII code (code below
0x20+0x7f) were initially introduced to control the first terminals. These control characters allowed you to do things like move to the next line (LFLine Feed) or delete the previous character (BSBackspace). You’ll rarely use most of them unless you program a terminal emulator, but there are at least three that you’ll use more than once:
HTor\tor0x09. This code is used for horizontal tabbing.
LFor\nor0x0a. This code is used to move to the next line.
CRor\ror0x0d. This code is used to move to the beginning of the line.Windows/DOS systems use the character combination
\r\nto move the cursor to the beginning of the next line, while most other systems only use the character\n. If you’re working on Windows, you may need to modify your programs slightly :).
As you can see, strings are represented between double quotes. We will talk more about C’s type system in later chapters. A constant string like the one we use in our program is known as a Literal. It’s simply a constant that will be stored in a special area of memory.
Comments begin with the characters /* and end with
*/. Everything between these two markers is considered a
comment and is ignored by the compiler. This type of comment cannot be
nested, so be careful when commenting large blocks of code that may
contain comments.
DID YOU KNOW…
Most modern C compilers accept C++ comments, which are single-line comments that only require indicating the beginning of the comment with the characters
//.However, older compilers (like those found on ancient UNIX machines that no one knows where they physically are, but everyone uses) don’t like seeing this symbol very much and produce quite cryptic compilation errors.
C programs are typically stored in files with the extension
.c. Header files have the extension .h.
As in many other languages, C marks the end of instructions with the
character ; (semicolon) and allows you to group
instructions with braces {}.
You can compile the previous program by copying the text into a file
that you could, in a display of originality, call hola.c
and then executing the following command:
cc -o hello hello.c
This generates a file called hello that you can execute
directly.
DID YOU KNOW
The program
ccis nothing more than the Compiler of C (C compiler in English). In most modern systems,ccis usually a symbolic link togcc, which is nothing more than the GNU C Compiler (GNU C Compiler in English).
$ make hola
cc hola.c -o hola
$
The C language was developed by Denis Ritchie at Bell Labs between
1972 and 1973, as a successor to the B language (obviously). The
language was used to develop UNIX utilities as well as the kernel
itself. The version of C before standardization organizations got
involved is known as K&R. K&R are the initials of Brian
Kerninghan (who by the way is the K of
awk) and Denis Ritchie, who published the book “The C
Programming Language” that, for many years, was the informal
specification of the language.
Summary
- Compiled language
- Extension:
c - Line terminator:
; - Groups instructions with:
{...} - Comments: between characters
/* */ - String delimiter:
"string" - You can print texts with
puts(Adds carriage return) - Does not natively support multiline strings
- Does not support
HERE-DOCS - Entry point function
main - Requires prior definition of all elements (include files to declare functions)