C is a general-purpose programming language. It has been closely associated
with the UNIX system where it was developed, since both the system and most of the programs that run
on it are written in C. The language, however, is not tied to any one operating system or machine;
and although it has been called "system programming language" because it is useful for writing
compilers and operating systems, it has been used equally well to write major programs in many
different domains.
With C, our aim is tho show the essential elements of the language in real
programs, but without getting bogged down in details, rules, and exceptions. We want to get you as
quickly as possible to the point where you can write useful programs, and to do that we have to
concentrate on the basics; variables and constants, data types, declarations, operators, statements,
and loops.
Hello World!
The only way to learn a new programming language is by writing programs in
it. The first program to write is the same for all languages:
Printing "hello world"
This is the big hurdle; to leap over it you have to be able to create
the program text somewhere, compile it successfully, load it, run it, and find out where your output
went. In C, the program to print "hello, world"
is:
Copy to clipboard
#include <stdio.h>
int main() {
printf("Hello World\n");
}
Just how to run this program depends on the sysyem you are using. As a specific example, on the UNIX
operating system you must create the program in a file whose name ends in ".c" such as hello.c, then
compile it with the command
cc hello.c
and execute with
a.out
On a recommended program called Dev C++, you
can just simply press F11. If you haven't
botched anything, such as omitting a character or mispelling something, the compilation will proceed
silently.
Variables
In C, all variables must be declared before they are used, usually at the
beginning of the function before any executable statements. A declaration announces, the
properties of variables; it consists of a type name and a list of variables such
as
int fahr, celcius;
int lower, upper, step;
Variable names are made up of letters and digits; the first character must
be a letter. The underscore "_" counts as a letter; it is sometimes useful for improving the
readability of long variable names. It's wise to choose variable names that are related to the
purpose of the variable, and that are unlikely to get mixed up typographically. We tend to use short
names for local variables, especially loop indices, and longer names for external variables.
Data Types
C provides several basic data types such as:
int
integer
char
character-a single byte
short
short integer
long
long integer
double
double-precision floating point
Declaration
All variables must be declared before use, although certain declarations can
be made implicitly by context. A declaration specifies a type, and contain a list of one or more
variables of that type, as in
int lower, upper, step;
char c, line[1000];
Variables can be distributed among declarations in any fashion; the list above could equally well be
written as
int lower;
int upper;
int step;
char c;
char line[1000];
This latter form takes more space, but is convenient for adding a comment to each declaration or for
subsequent modifications.
A Variable may also be initialized in its declaration. If the naem is
followed by an equals sign and an expression, the expression serves as an initializer, as in
char esc = '\\';
int i = 0;
int limit = MAXLINe + 1;
float eps = 1.0e-5;
Operators
The binary arithmetic operators are +, -, *, /, and the modulus operator %.
Integer division truncates any fractional part. The expression
x % y
produces the remainder when x is divided by y, and thus is zero when y
divides x
exactly. For example, a year is a leap year if it is divisible by 4 but not by 100, except that
years
divisible by 400 are leap years. Therefore
if((year % 4 == 0 && year % 100 != 0) || year & 400 == 0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
The % operator cannot be applied to float or double. The direction of truncation for / and the sign
of the result for % are machine-dependent for negative operands, as is the action taken on overflow
or underflow. The binary + and - operators have the same precedence, which is lower than the
precedence of *, /, and %, which is in turn lower than unary + and -.
The relational operators are
> >= < =
They all have the same precedence. Just below them
in precedence
are the equality operators:
== !=
If Else
The if-else statement is used to express decisions. Formally, the syntax is
if (expression)
statement1
else
statement2
where the else part is optional. The expression is evaluated; if it is true (that is, if
expression has a non-zero value), statement1 is executed. If it is false
(expression is zero) and if there is an else part, statement2 is executed
instead.
Loops
Take a look at this example.
while (expression)
statement
In the code, the expression is evaluated. If it is non-zero, statement is executed and
expression is re-evaluated. This cycle continues until expression becomes zero, at
which point execution resumes after statement.
Reference
This documentation is based on the book titled 'The C Programming Language' by the creators of C
programming language
themself: Brian W. Kernighan and Dennis M. Ritchie.