If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

To log in and use all the features of Khan Academy, please enable JavaScript in your browser.

AP®︎/College Computer Science Principles

Course: ap®︎/college computer science principles   >   unit 3, storing data in variables, assigning variables, displaying variables, re-assigning variables, pseudocode for variables.

  • (Choice A)   x ← 200 A x ← 200
  • (Choice B)   var x = 200 B var x = 200
  • (Choice C)   x = 200 C x = 200
  • (Choice D)   var x ← 200 D var x ← 200

Want to join the conversation?

  • Upvote Button opens signup modal
  • Downvote Button opens signup modal
  • Flag Button opens signup modal
  • Stack Overflow Public questions & answers
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Talent Build your employer brand
  • Advertising Reach developers & technologists worldwide
  • Labs The future of collective knowledge sharing
  • About the company

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

What exactly are C++ definitions, declarations and assignments?

I tend to use the words define, declare and assign interchangeably but this seems to cause offense to some people. Is this justified? Should I only use the word declare for the first time I assign to a variable? Or is there more to it than that?

  • variable-assignment
  • terminology

Brian Tompsett - 汤莱恩's user avatar

8 Answers 8

A definition is where a value or function is described, i.e. the compiler or programmer is told precisely what it is, e.g.

A declaration tells the compiler, or programmer that the function or variable exists. e.g.

An assignment is when a variable has its value set, usually with the = operator. e.g.

jheriko's user avatar

  • 3 Actually I would consider "int var;" to be definition, "int var = 5;" is a combined def/ass. Basically, in C, definition is anything that creates space for an object. I'll upvote anyway, maybe you'll change the answer, maybe not, but it's the best to date. –  paxdiablo Mar 23, 2009 at 1:09

Define and declare are similar but assign is very different.

Here I am declaring (or defining) a variable:

Here I am assigning a value to that variable:

Here I am doing both in one statement:

Not all languages support declaration and assignment in one statement:

Some languages require that you assign a value to a variable upon declaration. This requirement allows the compiler or interpreter of the language to infer a type for the variable:

Andrew Hare's user avatar

  • I'll split a hair -- Python has no "declaration" of any kind. Classes and functions are "defined", variables are created when they assigned. You don't really "declare" variables, since they're just names assigned to objects. –  S.Lott Mar 23, 2009 at 0:58
  • That is a good point - from one pedant to another I appreciate the distinction :) –  Andrew Hare Mar 23, 2009 at 1:02
  • More nitpicking: Python does not really do any type inference, it's dynamically typed. Type inference is only required when the variable has a type. This is valid Python and illustrates the effects of my point: x = 5 ; x = "str" –  Magnus Hoff Mar 23, 2009 at 13:24
  • I suggest either using a different language as an example (Haskell or C# with var), or just remove the "inference"-sentence. Anyway, this is just nitpicking :) –  Magnus Hoff Mar 23, 2009 at 13:24
  • 2 -1 for being the accepted answer yet being wrong: declare and define are not at all similar (even if in some languages the distinction is not made). See jheriko's answer below. –  Ian Goldby Nov 4, 2011 at 15:05

It is important to use the correct terminology, otherwise people will not know what you are talking about, or incorrectly assume that you don't know what you are talking about.

1800 INFORMATION's user avatar

These terms often have precise meanings in the standards for various languages. When that is the case they should not be conflated.

In c for instance:

a function may be defined only once (when you say what it does), but it may also be declared before that (when you say what arguments it takes and what type it returns).

likewise a variable is declared when you say what type it is, and this happens only once for each scope. But you may assign a value repeatedly. (Some languages also differentiate between initialization (giving a variable a value at declaration time) and assignment (changing the value later).)

dmckee --- ex-moderator kitten's user avatar

General Role: Definition = declaration + reserved space.

Definition, declaration, and assignment have two cases:

  • for Variables.
  • for Functions.

For Variables :

-- Definition: To tell the compiler to reserve memory for the variable.

-- Declaration: To tell the compiler that the variable defined in somewhere else.

-- Assignment: To tell the compiler to put the value in the variable.

For Functions :

-- Definition:

-- Declaration: It is just the prototype of the function.

Amr Tarek Abdelghafar's user avatar

The differences can seem subtle, but they are important. Not every language makes the same distinctions, but in C++ a variable declaration makes the type and name of the variable known to the compiler

A variable definition allocates storage and specifies an initial value for the variable.

You can combine a variable declaration and definition into one statement, as is commonly done.

Declaring a variable inside a function will also set aside memory for the variable, so the following code implicitly defines variable a as a part of its declaration.

Since variable a is automatically defined by the compiler, it will contain whatever value was in the memory location that was allocated for it. This is why it is not safe to use automatic variables until you've explicitly assigned a known value to them.

An assignment takes place any time you change the value of a variable in your program.

A function declaration , similar to the variable declaration, makes the function signature known to the compiler. This allows you to call a function in your source code before it is defined without causing a compiler error.

A function definition specifies the return type, name, parameter list, and instructions for a function. The first three of these elements must match the function declaration. A function must only be defined once in a given program.

You can combine the function decalartion and definition into one, but you must do so before the function is called anywhere in your program.

Bill the Lizard's user avatar

  • I thought a variable declaration assigned storage space. You just get garbage in that space unless you also provide an initialisation, or until you assign a new value. You can only initialise a variable whilst declaring it. –  Artelius Mar 23, 2009 at 1:44
  • @Artelius: In most cases declaration includes an implicit definition by the compiler, which makes it appear that declaration assigns storage space. I made a (hopefully) clarifying addition to include this in my answer. Thanks for pointing it out. –  Bill the Lizard Mar 23, 2009 at 13:50
  • I voted this answer up as it is correct as I've learned it. If not, I'd really like to know why. The answers above have declaration and definition backwards. –  lakeweb Jan 29, 2017 at 17:31
  • i = 1 is an assignment, not a definition. To be a definition (e.g. of a previously declared extern variable in its defining translation unit), it would need the type (and the assignment component is optional). –  underscore_d Jun 29, 2017 at 16:17

It might depend on the language, as has been said. I think it really depends on whether the words are used for things like classes. For most of the data types discussed here, the question might not have much relevance. In C++ (see c++ - What is the difference between a definition and a declaration? ), a class or struct always has precisely one definition but can be declared zero or more times. A class cannot be declared without a definition. So "declared" might be synonymous with "used".

In most languages, simple types such as integers do not need definitions in the manner that classes do.

Community's user avatar

The correct answer depends on which language you're talking about. Computer languages often have specific terminology, either because of the language specification or the community grown up around the language. COBOL, back when I used it, had a much different terminology than more mainstream languages (in the sense of languages closer to the mainstream of language development, not mainstream business). Forth developed some strange terminology.

If you know English, you can usually get a good idea as to what a word means from its normal meaning, but never count on it too much. The same is true with specific words across languages or language communities.

David Thornley's user avatar

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct .

  • The Overflow Blog
  • What it’s like being a professional workplace bestie (Ep. 603)
  • Journey to the cloud part I: Migrating Stack Overflow Teams to Azure
  • Featured on Meta
  • Our Design Vision for Stack Overflow and the Stack Exchange network
  • Temporary policy: Generative AI (e.g., ChatGPT) is banned
  • Call for volunteer reviewers for an updated search experience: OverflowAI Search
  • Discussions experiment launching on NLP Collective

Hot Network Questions

  • I've been invited to a free 2-day conference. What's the catch?
  • Orbit stabiliser theorem as an analogue to first isomorphism theorem
  • Silicone food molds that have been used to prepare (caustic soap): Safe for use with food again?
  • Would it harm game balance to allow potions to be self-administered with a bonus action?
  • The image you are requesting does not exist?
  • How to stop long words with "+" in them from going into the margins?
  • Is it possible for the sentence "Bachelors are unmarried" to be fallible?
  • Steinhaus theorem and Hausdorff dimension
  • Question about Isolating Transformer NZART exam question
  • Can randomness create patterns?
  • Is there a term for a subgenre of puns where a word or a part of a word is replaced by its opposite?
  • Load Lines and Characteristic Curves
  • How can I find a list of Chrome's hidden URLs with chrome://?
  • Terminal: How to move files to the last used directory conveniently?
  • Will sci-fi feudalism work?
  • How can we replace strings according to requirements?
  • Is there a negative impact on an SSD drive if I power cycle it every hour for 6 months?
  • We know how expensive we are
  • What if no Republican candidate gets a majority of delegates for president?
  • How can I make a Genus 3 Surface?
  • Is there such a thing as specific agents/officers legally pursuing international criminals across borders?
  • Defining a terminating `ripple`, with arguments swapping places
  • Musk Oxen and Arctic Foxen
  • Are PCIe and USB 3.0 the same interface?

an assignment that is made when a variable is declared is known as what

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

IMAGES

  1. PPT

    an assignment that is made when a variable is declared is known as what

  2. PPT

    an assignment that is made when a variable is declared is known as what

  3. PPT

    an assignment that is made when a variable is declared is known as what

  4. PPT

    an assignment that is made when a variable is declared is known as what

  5. Variables, Assignment & Data Types

    an assignment that is made when a variable is declared is known as what

  6. PPT

    an assignment that is made when a variable is declared is known as what

VIDEO

  1. What do you understand by an instance variable and a local variable? #java #interviewtips

  2. MO CRITTER VIDEO

  3. Variable Assignment topics in python language

  4. Assignment: Design a Dual Variable Power Supply Circuit (BENE 3143)

  5. Solve an Expression with a Known Variable Value

  6. Use Destructuring Assignment to Extract Values from Objects

COMMENTS

  1. C#

    An assignment that is made when a variable is declared is known as what? a) An initialization of the variable b) An allocation of the variable c) An assignment to the variable d) A definition of the variable B What can be used to control the appearance of text output? a) A declared namespace b) A defined format string c) A set placeholder

  2. Solved An assignment that is made when a variable is

    See Answer Question: An assignment that is made when a variable is declared is known as what? a. A definition of the variable b. An allocation of the variable An assignment that is made when a variable is declared is known as what? Expert Answer 100% (3 ratings) An assignment that is made w … View the full answer Previous question Next question

  3. What exactly are C++ definitions, declarations and

    8 Answers Sorted by: 40 A definition is where a value or function is described, i.e. the compiler or programmer is told precisely what it is, e.g. int foo () { return 1; } int var; // or, e.g. int var = 5; but this is clearer. A declaration tells the compiler, or programmer that the function or variable exists. e.g. int foo (); extern int var;

  4. C# isnt for me // Chapters 1 and 2 Test Retake

    An assignment that is made when a variable is declared is known as what? An initialization of the variable Students also viewed Programming Chp 2 quiz 24 terms James_Bailey749 C# tabernik final Ch 1,2,3,4,5