Quick Navigation Bar introduction :: basic c :: functions, preprocessor [ toc | forums ] |
Note: If the document URL does not begin with https://randu.org/tutorials/c/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
In the introduction, we discussed very simple C, now it is time for us to move ahead and explore the basics of C programming. If you do not understand the concepts explained in the Introduction, do not proceed. Make sure you understand completely the topics covered in the introduction before you dive into C.
a = b
is different from
a == b
. Most C compilers will allow both statements to
be used in conditionals like if, but they have two completely different
meanings. Make sure your assignment operators are where you want them
to be and your relationals where you want relational comparisons!x != 0 && y != 0
x != 0
evaluates
to false, the whole statement is false regardless of the outcome
of y != 0
. This can be a good thing or a bad thing
depending on the context. (See Weiss pg. 51-52).++
and --
.
You can place these in front or on the back of variables.
++
specifies increment, the --
specifies
decrement. If the operator is placed in front, it is prefix
if it is placed behind, it is postfix. Prefix means,
increment before any operations are performed, postfix is increment
afterwards. These are important considerations when using these
operators.*= += /= -=
operators. For example:
int i = 5; i *= 5;The int i would have the value 25 after the operation.
if
used with the above relational and logical
operators allows for conditional statements. You can start
blocks of code using {
and }
. if's can
be coupled with else
keyword to handle alternative
outcomes.? :
operator can be a shorthand method for
signifying (if expression) ? (evaluate if true) : (else evaluate
this). For example, you can use this in a return statement
or a printf statement for conciseness. Beware! This reduces
the readability of the program... see Introduction. This
does not in any way speed up execution time.switch
statement allows for quick if-else
checking. For example, if you wanted to determine what the
char x
was and have different outcomes for certain
values of x, you could simply switch
x and run
cases. Some sample code:
switch ( x ) { case 'a': /* Do stuff when x is 'a' */ break; case 'b': case 'c': case 'd': /* Fallthrough technique... cases b,c,d all use this code */ break; default: /* Handle cases when x is not a,b,c or d. ALWAYS have a default */ /* case!!! */ break; }
while
, for
, and do
while
.while
loops until the expression specified is
false. For example while (x < 4)
will loop while x is
less than 4.for
is different. Here's an example:
for (i = 0; i < n; i++, z++)
. This code will loop until
i is equal to n. The first argument specifies initializing conditions,
the second argument is like the while expression: continue the for
loop until this expression no longer evaulates to true. The third
argument allows for adjustment of loop control variables or other
variables. These statements can be null, e.g. for (;
i < n; i++)
does not specify initializing code.do while
is like a "repeat-until" in Pascal. This
is useful for loops that must be executed at least once. Some
sample code would be:
do { /* do stuff */ } while (statement);
int, char, float, double
are the fundamental
data types in C.short, long, unsigned, signed
.
Not all combinations of types and modifiers are availble.
const
and
volatile
. The const
qualifier places
the assigned variable in the constant data area of memory which makes
the particular variable unmodifiable (technically it still is though).
volatile
is used less frequently and tells the compiler
that this value can be modified outside the control of the program.
auto, extern, register,
static
.
auto
keyword places the specified variable
into the stack area of memory. This is usually implicit
in most variable declarations, e.g. int i;
extern
keyword makes the specified variable
access the variable of the same name from some other file. This
is very useful for sharing variables in modular programs.register
keyword suggests to the
compiler to place the particular variable in the fast register
memory located directly on the CPU. Most compilers these days
(like gcc) are so smart that suggesting registers could actually
make your program slower.static
keyword is useful for extending the
lifetime of a particular variable. If you declare a static variable
inside a function, the variable remains even after the function
call is long gone (the variable is placed in the alterable area of
memory). The static
keyword is overloaded. It
is also used to declare variables to be private to a certain file
only when declared with global variables. static
can
also be used with functions, making those functions visible only
to the file itself.if, else, switch
.
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.