Fundamental C - Variables
Written by Harry Fairhead   
Monday, 08 April 2019
Article Index
Fundamental C - Variables
Numeric Data Types
lvalue and rvalue

In practice using stdint.h is a good idea, even if you are targeting a fixed machine, because it makes clear the exact size of the variables that are in use. Specifically, if you write:

int myvar;

then a programmer unfamiliar with the machine is left wondering what the size of the variable is, but if you use:

int16_t myvar;

then, even though this might map to int, it makes it clear that this is a 16-bit int.

There are also macros that will create literals in the correct type:

INTN_C(value)
UINTN_C(value)

create signed and unsigned literals with N bits. For example:

INT16_C(255)

creates a 16-bit signed constant for 255.

There is more to say about macros in Chapter 14.

Sections in the chapter but not in this extract:

  1. Type Modifiers
  2. Variable Lifetimes

lvalue and rvalue

It is clear that in an assignment there are left- and right-hand sides. Consider:

a=2;

In this case a is called an lvalue and 2 is called an rvalue. There is an important distinction between these two types of thing. The lvalue is the address of a modifiable entity. The rvalue is something that has a value, but it doesn’t have an address that can be used to access it. What this means is that:

2=a;

is nonsense – you can’t assign to 2 as it doesn’t have an address that you can work with.

However, notice that you can write:

b=a;

so lvalue doesn’t mean strictly on the left-hand side. Today we generally say that an lvalue is a locator value and it can be assigned to or retrieved and hence it can be used on both sides of an assignment.

By contrast an rvalue really can only be used on the right of an assignment – it is, or can be reduced to, a value.

As a more subtle point, you could argue that 2 is a literal and has to be stored somewhere and so modifying the literal is possible and so perhaps an rvalue can be assigned to. This is sometimes the case, but what about 2*b+a? This is an expression and while its constituents are stored somewhere its effective value is computed when needed. This is an rvalue for which assignment makes no sense and the simple literal 2 is a special case of an expression.

The idea of an lvalue that has an “address” and an rvalue that might well not have an address is often helpful in understanding why some things can’t be used where an address is required.

Summary

  • C has only two basic types of data – integer and floating-point.

  • The amount of memory that a data type takes is machine-dependent.

  • Variables have to be declared before use and optionally initialized.

  • All integer types come in signed and unsigned forms.

  • C doesn’t have Boolean type which stores true and false. Instead integer types are used with 0 as false and any other value as false.

  • C99 adds a boolean type which uses 0 as false and 1 as true. Before C99 programmer invented a number of ways of making it seem as if there was a true and false value.

  • C99 also added a complex type which is implemented as a pair of floating-point types.

  • Character literals can be used to store ASCII or UTF-8 character codes in an integer type.

  • It is a good idea to specify the type of any literal you are using.

  • Variables can be regarded as constant pointers. There is no trace of a variable’s name in the assembler that the compiler produces just the address that it was assigned.

  • You can use integer types that have a well defined size via the stdint.h library introduced in C99.

  • Variables can be declared as const to make sure they cannot be changed and volatile to make sure that the compiler cannot assume that they do not change.

  • How long a variable exists and where you can use it – lifetime and scope – are important ideas but they only make sense when you have understood functions in a later chapter.

  • C makes use of the idea of an lvalue, something that has an address, and an rvalue, something that doesn’t.
    Cbookcover

 

Fundamental C: Getting Closer To The Machine

Now available as a paperback and ebook from Amazon.

  1. About C
      Extract Dependent v Independent
                  & Undefined Behavio
  2. Getting Started With C Using NetBeans
  3. Control Structures and Data
  4. Variables
      Extract Variables
  5. Arithmetic  and Representation
      Extract Arithmetic and Representation
  6. Operators and Expression
      Extract: Expressions
      Extract Side Effects, Sequence Points And Lazy Evaluation
      First Draft of Chapter: Low Down Data
  7. Functions Scope and Lifetime
  8. Arrays
      Extract  Simple Arrays
      Extract  Ennumerations
  9. Strings
      Extract  Simple Strings
     
    Extract: String I/O ***NEW!!
  10. Pointers
      Extract  Starting Pointers
      Extract  Pointers, Cast & Type Punning
  11. Structs
      Extract Basic Structs
      Extract Typedef
  12. Bit Manipulation
      Extract Basic Bits
      Extract Shifts And Rotates 
  13. Files
     Extract Files
     
    Extract Random Access Files 
  14. Compiling C – Preprocessor, Compiler, Linker
     Extract Compilation & Preprocessor

Also see the companion volume: Applying C

<ASIN:1871962609>

<ASIN:1871962463>

<ASIN:1871962617>

<ASIN:1871962455>

 

 

Related Articles

Remote C/C++ Development With NetBeans

Raspberry Pi And The IoT In C

Getting Started With C/C++ On The Micro:bit

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Eclipse JKube 1.16 Goes GA
08/04/2024

Eclipse JKube makes deploying your Java application to a Kubernetes cluster a breeze. Let's find out what's new.



Excel Spreadsheet - A Joke?
01/04/2024

No this isn't an April Fool's although in places it seems like one. It's a true account of how Williams Racing has suffered through reliance on an overgrown and outdated Microsoft Excel spreadsheet, l [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Monday, 29 April 2019 )