Thread: variable in C programming

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    variable in C programming

    Hello everyone
    simply variable are names and the value of variable will store on the memory of PC. as variable is declared in the following format:

    Code:
    <data type of variable> <name of variable>;
    OR
     <data  type of variable> <name of variable>=<initial value of  variable>;:
    example of variable
    Code:
    int i; /* * this declares the variables 'i', */
    int i =6; /* * this declares the variables 'i', with assign value 6 */
    I want to understand how and where does value of variable store on memory of PC?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The variable could be stored in either of two places, generally speaking, depending on how or where it is defined. If you declare that variable at global scope, it will be stored in the process's global data region. This is defined by the operating system. If you declare it within a function, it will typically be stored on the stack, unless it is declared to be static, in which case, it will be stored in the process's global data region.

    You can also store variables in the "free store," or "heap," as it's more commonly called, by using malloc. This is generally done more often for large objects, such as memory buffers and structures.

    As for how it is stored, on most C compilers for PC hardware, an int is 4 bytes, and is stored in memory least-significant-byte first. So in your example, starting at the memory address of your variable, you would find the four bytes (6, 0, 0, 0). You can experiment with this a bit, by taking the address of your variable, using the address-of operator (&), and cast it to a char pointer, then inspect the first four bytes, starting at the address stored in that pointer
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    "simply variable are names and the value of variable will store on the memory of PC"

    What???

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    129
    for example int i,j,k; /* * this declares the variables name 'i', 'j' and 'k' */
    int i,j,k=5; /* * this declares the variables name 'i', 'j' and 'k' and k=5 */

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by abhi143 View Post
    for example int i,j,k; /* * this declares the variables name 'i', 'j' and 'k' */
    int i,j,k=5; /* * this declares the variables name 'i', 'j' and 'k' and k=5 */
    Please learn the difference between declare and define; then try to ask a question that makes some sense.

    What's the difference between declaring and defining in C and C++ - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    In case you missed it, be sure to read Elkvis's answer as he's given some good info there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-18-2015, 02:02 PM
  2. Replies: 6
    Last Post: 12-26-2013, 01:32 PM
  3. Replies: 1
    Last Post: 02-19-2013, 11:30 PM
  4. Replies: 8
    Last Post: 08-25-2012, 06:46 AM
  5. Replies: 27
    Last Post: 04-20-2011, 07:56 PM

Tags for this Thread