article

DECLARING VARIABLES / CONSTANTS IN C

Email
Submitted on: 1/1/2015 1:44:00 PM
By: Indee (from psc cd)  
Level: Beginner
User Rating: By 15 Users
Compatibility: C
Views: 489
 
     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.

 
				
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.


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.