Home
Username:
Password:
Advanced C Programming Tutorials

Memory Allocation / Declarations & Definitions




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

C contains both declarations and definitions. The two are different things. These two are both definitions. A definition only creates a symbol with specific characteristics. It does not create space in the final program. It only defines a symbol which is remembered by the compiler. The symbol then can be used later in a declaration to allocate actual memory. These are two examples of declarations. A declaration creates a place in memory. A declaration is a definition but it also creates a place in memory. With these two examples, one creates space to hold an int value and the other creates space to hold a double. It's easy to get the two words confused. I keep them straight in my mind by remembering the define directive of the pre-processor. It's used to create definitions, not storage space. The extern keyword creates a definition, not a declaration. This defines the type of the variables named val as being a long. You can write code using it but the actual storage location does not exist locally. In another source module you will find a definition of val without the exter keyword. This is an example of a global name being assigned to a location in memory. The first usage of val here is a definition. The second is a declaration. Any declaration that is not inside a method and is not declared as static has its symbol supplied to the linker so it can be referred to from any module in the program. The linker does the job of connecting up all the references to the externally-declared symbol. You need to be aware of a couple of things here. If a declaration is outside of a function body and you do not specify that it is static, it becomes a global name. The entry keyword is no longer a part of the C language. Also, it's OK to have both the declaration and a definition of the symbol in the same source file. In other words, these two lines of code can appear in the same source file. This makes it easy to do something like define external references in a header file and include that header file in the module where they are defined. But there must be only one of each of these for a variable in any given source file and there must be only one declaration of it in all the source files that make up the program. One very common definition is the function prototype. This does nothing but define the name and type of the function to the compiler. Later when you declare a call to the function, the compiler looks at the definition it's holding and verifies that the name, return type and argument types all match. Also, when you declare the function body itself, the definition being held by the compiler is checked for the parameters being of the right types. By the way, you'll see the term formal argument used for parameters and actual argument used for arguments but they're referring to the same things.

Tutorial Information

Course: Advanced C Programming
Author: Arthur Griffith
SKU: 33965
ISBN: 1-935320-24-6
Release Date: 2009-01-30
Duration: 5.5 hrs / 82 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available