tutorial,designed,novice,programmerand,progra
Quick Search for:  in language:    
tutorial,designed,novice,programmerand,progra
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 451,578 lines
 Jobs: 558 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for C/ C++.
Url Test
By Richard Creary on 8/27


Nice Console Calc
By Andrew Carter on 8/26


Visual Pi Hex 2
By jo122321323 on 8/26


Cascade Clone v1.0
By Paulo Jorente - aka JungleBoy on 8/26


Bubble Sort Algo
By d1rtyw0rm on 8/26


Copy File I/O
By Olivier Bastien on 8/25


Visual Pi Hex
By jo122321323 on 8/25


Click here to see a screenshot of this code!ShortCutSample
By Massimiliano Tomassetti on 8/25

(Screen Shot)

AnPMoneyManager beta
By Anthony Tristan on 8/24


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

DECLARING VARIABLES / CONSTANTS IN C

Print
Email
 

Submitted on: 11/1/2000 10:06:57 PM
By: Indee  
Level: Beginner
User Rating: By 15 Users
Compatibility:C

Users have accessed this article 6711 times.
 

(About the author)
 
     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.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
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 4 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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
11/2/2000 10:14:05 AM:Kevin
Great. Just what I need. I havnt even touched C++ for the fear of jumping in the deep end. This is a excellent way for me to learn it. Thanks..
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/3/2000 2:19:02 AM:InderMohanSingh
Thanks Kevin. I will be writing tutorial about C every week so keep coming back to this site.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/19/2000 12:30:13 AM:Biofreak
Hey, Thanx a lot for this, please continue writing these tutorials. I am a little further than this, but not too far. Please email me if you could ever have the time to help me personaly. non@phreaker.net
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/23/2000 11:32:52 PM:Manas Mukherjee, Ph.D.
Inder, Teaching is an art and you got it. C++ is not a man-eater, let people know that. Please keep it up. You are humble too.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/27/2000 3:51:16 AM:Paul
Cool! carry on these great tutorials Please...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.