Tutorial-Declaring Variables/Constants in C
DECLARING VARIABLES / CONSTANTS IN C
The tutorial is designed for the novice programmer and the programmer who has
never used structured language before. It will give an impression how C
works. Experts skip this articles.
C is Case-Senstive
If you are familiar with languages such as Pascal, one of the first diffrence
to notice in C is that it is case sensitive; uppercase and lowecase letters are
treated as separate characters. For example in C, the variable names
length, Length and LENGTH are three different variables. So
whenever entering the source code, be very careful to use the proper case.
Well I think the best way to learn is to write a simple C program and than I will
explain what all this jargon(source code) is about, so lets start by the following simple C program.
A Simple Example of C Program
#include
/* Sample Program */
void main
() { int age;
age = 31; printf("My age is %d\n",
age); }
|
Explanation
Now lets take a closer look at each line in sample program. The first
line,
#include
tells compiler to include the stdio.h
in the compilation. This file contains information needed by the program to
ensure the correct operation of standard library functions. In C there are
numbers of these types of files often referred to as header files.
Some programs will require more than one header file, so make sure that you
include these lines in your programs. (I will discuss these files in details in
my future tutorials.) The second line,
/* Sample Program */
is a
comment. In C, comments begin with the sequence /* and are
terminated by */. The C compiler ignores everything between these comment
symbols. This type of comment may extend over several lines. If the comment is
only one line long, then
use:
// Sample
Program
Did you noticed the blank lines following the comment line.
Well in C, blank lines are permitted and have no affect on the
program.
Ok now the
line,
void main()
specifies the name of a function. All C programs begin
by calling the main() function.(I will discuss functions in
details in my future tutorials.)Void ststement indicates that the programs does
not return a value. Void means empty, or none.
The next line
consists of a single curly brace, {, that signifies the start of the
main() function.
Ok! guys lets get to the real thing, the first
line of code inside the main() function is
int age;
This line declares a variable called age and the int informs the compiler that it is
an integer. In C, variables must be declared before they are used. The declaration
process involves specifying the variable's name as well as its type. In this case, age
is of type int, which is C's keyword for integer. Integers are whole numbers.
The next line is
age = 31;
which is assignment statement. It places the value 31 into the variable age. Notice
that C uses a single equal sign for assignment and also all the statements in C are
terminated by semicolon.
The next line which ouputs information to the screen, is
printf("My age is %d\n", age);
Well this statement is very important for two reasons. First, it is an example of a
function call and secondly, it illustrates the use of C's standard output function,
printf(). There are two parts in this statement: the function name, which is printf(),
and its two arguments, "My age is %d\n" and age.(An argument is a value passed to the
function when it is called.)
The first argument in the printf() function is a quoted string that may contain either
normal characters or format codes that begin with the percentage sign(%).A format code
informs printf() that a noncharacter item is to be displayed. In this case, the %d means
that an integer is to be output in decimal format. The value to be displayed is
found in the second argument, in this case age. The \n is a special code that tells
the printf() to issue a carriage-return-linefeed sequence, called a new line in C. This will
display "My age is 31".To understand concept lets change the line to read:
printf("%d is my age\n", age);
The message now displayed is "31 is my age". The point is that where the format command
occurs in the string determines where the second argument to printf() will be printed.
(Well printf() is substantially more powerful than illustrated by this example. I will
write tutorial on this in future.)
OK at last the last line of the program is a closing curly braces, }, which tells the
compilers that this is the end of the main() function. The program execution is terminated.
Phew!! I think that's enough for todays lesson. Well I will be writing the next tutorial
about C Data Types.
Bye! for now.
|