Data and Memory in C

Computers keep track of memory by using addresses. Computers maintain addresses of bytes.  A byte is 8 bits arranged in order in memory. Each bit represents a position whose value is 2 to the power of n, where n is the numeric value of the bit’s position, starting from 0. By convention, the leftmost bit is called the most significant bit, as this has the largest value (2^7), whereas the rightmost bit is called the least significant bit, as it has the value of 2^0, which is 1.

We use bytes to form memory objects. A memory object, not to be confused with an object that is an instantiation of a class, is a region of memory made up of a contiguous collection of bytes in order to store a value. All objects have addresses, which is the address of the object’s first byte in memory. On microcomputers, this byte is the smallest.

A variable is thus a memory object that has a name, and has an amount of storage determined by its type. Variables are declared, whereby the compiler is told that a variable will be used and what its data type is. A variable can be assigned an initial value when it is declared. The C language has a number of types, such as char, which stores an ASCII character, an int, which stores a whole number, an unsigned int, which stores a positive whole number, and a double, which stores a double-precision floating-point number.

The char type specifies an ASCII character. Any ASCII character is guaranteed to have a positive value. The char keyword sets the type of the memory object and the identifier, which is the variable’s name.

#include    <stdio.h>    
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
char cX = 'c';
char cY = 100;

printf("%c \t %c\n", cX, cY);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

The expression ‘c’ is called a character constant. It yields the numeric value that is stored to represent the character. Note that it uses a pair of single quotation marks, not double quotation marks.  Double quotation marks are used to delimit a string literal. A string literal is stored as an array of characters. We can access it either via a char pointer or a char array.

#include    <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
char *szStringOne = "El Psy Congroo";
char szStringTwo[] = "Nice Boat!";

printf("%s\n", szStringOne);
printf("%s\n", szStringTwo);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

An equals sign in C, as in most languages, assigns the value of an expression on the right side to the memory object on left side.

Formatting characters, such as newline or tab, are represented via escape sequences. For instance, you can’t put a newline in a string literal by pressing Enter, as that only puts a newline in the source code! The escape sequence for a tab is \t and the escape sequence for a newline is \n.  Single and double quotation marks can also be represented via escape sequences, \’ and \”, respectively.

While the size of an int variable isn’t specified, it is typically 32 bits, which was the word size on x86 machines. Note that an int value is still 32 bits. Long or at least long long values should be 64 bits in length. Pointers are 64 bits as well, at least on x64 machines

#include     <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
int iValue;
int *piValue;
long long llValue;

printf("size of an int = %lu\n", sizeof(iValue));
printf("size of a pointer = %lu\n", sizeof(piValue));
printf("Size of a long long = %lu\n", sizeof(llValue);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- *

Computers keep track of memory by using addresses. Computers maintain addresses of bytes.  A byte is 8 bits arranged in order in memory. Each bit represents a position whose value is 2 to the power of n, where n is the numeric value of the bit’s position, starting from 0. By convention, the leftmost bit is called the most significant bit, as this has the largest value (2^7), whereas the rightmost bit is called the least significant bit, as it has the value of 2^0, which is 1.

We use bytes to form memory objects. A memory object, not to be confused with an object that is an instantiation of a class, is a region of memory made up of a contiguous collection of bytes in order to store a value. All objects have addresses, which is the address of the object’s first byte in memory. On microcomputers, this byte is the smallest.

A variable is thus a memory object that has a name, and has an amount of storage determined by its type. Variables are declared, whereby the compiler is told that a variable will be used and what its data type is. A variable can be assigned an initial value when it is declared. The C language has a number of types, such as char, which stores an ASCII character, an int, which stores a whole number, an unsigned int, which stores a positive whole number, and a double, which stores a double-precision floating-point number.

The char type specifies an ASCII character. Any ASCII character is guaranteed to have a positive value. The char keyword sets the type of the memory object and the identifier, which is the variable’s name.

#include    <stdio.h>    
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
char cX = 'c';
char cY = 100;

printf("%c \t %c\n", cX, cY);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

The expression ‘c’ is called a character constant. It yields the numeric value that is stored to represent the character. Note that it uses a pair of single quotation marks, not double quotation marks.  Double quotation marks are used to delimit a string literal. A string literal is stored as an array of characters. We can access it either via a char pointer or a char array.

#include    <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
char *szStringOne = "El Psy Congroo";
char szStringTwo[] = "Nice Boat!";

printf("%s\n", szStringOne);
printf("%s\n", szStringTwo);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

An equals sign in C, as in most languages, assigns the value of an expression on the right side to the memory object on left side.

Formatting characters, such as newline or tab, are represented via escape sequences. For instance, you can’t put a newline in a string literal by pressing Enter, as that only puts a newline in the source code! The escape sequence for a tab is \t and the escape sequence for a newline is \n.  Single and double quotation marks can also be represented via escape sequences, \’ and \”, respectively.

While the size of an int variable isn’t specified, it is typically 32 bits, which was the word size on x86 machines. Note that an int value is still 32 bits. Long or at least long long values should be 64 bits in length. Pointers are 64 bits as well, at least on x64 machines

 #include     <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
int iValue;
int *piValue;
long long llValue;

printf("size of an int = %lu\n", sizeof(iValue));
printf("size of a pointer = %lu\n", sizeof(piValue));
printf("Size of a long long = %lu\n", sizeof(llValue);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

Computers keep track of memory by using addresses. Computers maintain addresses of bytes.  A byte is 8 bits arranged in order in memory. Each bit represents a position whose value is 2 to the power of n, where n is the numeric value of the bit’s position, starting from 0. By convention, the leftmost bit is called the most significant bit, as this has the largest value (2^7), whereas the rightmost bit is called the least significant bit, as it has the value of 2^0, which is 1.

We use bytes to form memory objects. A memory object, not to be confused with an object that is an instantiation of a class, is a region of memory made up of a contiguous collection of bytes in order to store a value. All objects have addresses, which is the address of the object’s first byte in memory. On microcomputers, this byte is the smallest.

A variable is thus a memory object that has a name, and has an amount of storage determined by its type. Variables are declared, whereby the compiler is told that a variable will be used and what its data type is. A variable can be assigned an initial value when it is declared. The C language has a number of types, such as char, which stores an ASCII character, an int, which stores a whole number, an unsigned int, which stores a positive whole number, and a double, which stores a double-precision floating-point number.

The char type specifies an ASCII character. Any ASCII character is guaranteed to have a positive value. The char keyword sets the type of the memory object and the identifier, which is the variable’s name.

#include    <stdio.h>    
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
char cX = 'c';
char cY = 100;

printf("%c \t %c\n", cX, cY);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

The expression ‘c’ is called a character constant. It yields the numeric value that is stored to represent the character. Note that it uses a pair of single quotation marks, not double quotation marks.  Double quotation marks are used to delimit a string literal. A string literal is stored as an array of characters. We can access it either via a char pointer or a char array.

#include    <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
char *szStringOne = "El Psy Congroo";
char szStringTwo[] = "Nice Boat!";

printf("%s\n", szStringOne);
printf("%s\n", szStringTwo);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

An equals sign in C, as in most languages, assigns the value of an expression on the right side to the memory object on left side.

Formatting characters, such as newline or tab, are represented via escape sequences. For instance, you can’t put a newline in a string literal by pressing Enter, as that only puts a newline in the source code! The escape sequence for a tab is \t and the escape sequence for a newline is \n.  Single and double quotation marks can also be represented via escape sequences, \’ and \”, respectively.

While the size of an int variable isn’t specified, it is typically 32 bits, which was the word size on x86 machines. Note that an int value is still 32 bits. Long or at least long long values should be 64 bits in length. Pointers are 64 bits as well, at least on x64 machines

#include     <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
int iValue;
int *piValue;
long long llValue;

printf("size of an int = %lu\n", sizeof(iValue));
printf("size of a pointer = %lu\n", sizeof(piValue));
printf("Size of a long long = %lu\n", sizeof(llValue);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- *

Computers keep track of memory by using addresses. Computers maintain addresses of bytes.  A byte is 8 bits arranged in order in memory. Each bit represents a position whose value is 2 to the power of n, where n is the numeric value of the bit’s position, starting from 0. By convention, the leftmost bit is called the most significant bit, as this has the largest value (2^7), whereas the rightmost bit is called the least significant bit, as it has the value of 2^0, which is 1.

We use bytes to form memory objects. A memory object, not to be confused with an object that is an instantiation of a class, is a region of memory made up of a contiguous collection of bytes in order to store a value. All objects have addresses, which is the address of the object’s first byte in memory. On microcomputers, this byte is the smallest.

A variable is thus a memory object that has a name, and has an amount of storage determined by its type. Variables are declared, whereby the compiler is told that a variable will be used and what its data type is. A variable can be assigned an initial value when it is declared. The C language has a number of types, such as char, which stores an ASCII character, an int, which stores a whole number, an unsigned int, which stores a positive whole number, and a double, which stores a double-precision floating-point number.

The char type specifies an ASCII character. Any ASCII character is guaranteed to have a positive value. The char keyword sets the type of the memory object and the identifier, which is the variable’s name.

#include    <stdio.h>    
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
char cX = 'c';
char cY = 100;

printf("%c \t %c\n", cX, cY);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

The expression ‘c’ is called a character constant. It yields the numeric value that is stored to represent the character. Note that it uses a pair of single quotation marks, not double quotation marks.  Double quotation marks are used to delimit a string literal. A string literal is stored as an array of characters. We can access it either via a char pointer or a char array.

#include    <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
char *szStringOne = "El Psy Congroo";
char szStringTwo[] = "Nice Boat!";

printf("%s\n", szStringOne);
printf("%s\n", szStringTwo);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

An equals sign in C, as in most languages, assigns the value of an expression on the right side to the memory object on left side.

Formatting characters, such as newline or tab, are represented via escape sequences. For instance, you can’t put a newline in a string literal by pressing Enter, as that only puts a newline in the source code! The escape sequence for a tab is \t and the escape sequence for a newline is \n.  Single and double quotation marks can also be represented via escape sequences, \’ and \”, respectively.

While the size of an int variable isn’t specified, it is typically 32 bits, which was the word size on x86 machines. Note that an int value is still 32 bits. Long or at least long long values should be 64 bits in length. Pointers are 64 bits as well, at least on x64 machines

 #include     <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
int iValue;
int *piValue;
long long llValue;

printf("size of an int = %lu\n", sizeof(iValue));
printf("size of a pointer = %lu\n", sizeof(piValue));
printf("Size of a long long = %lu\n", sizeof(llValue);

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

As we have seen, the printf() function can handle both fixed and variable parts. The function takes at least one argument, the format string that contains both literal text and conversion specifiers. The values stored in optional arguments sent after the format string literal are inserted in the places indicated by the conversion specifiers. The literal text is printed unchanged.

The %d conversion specifier is used for int values. The %c specifier is used for char values. The %u specifier is used for unsigned values. The %x specifier converts an unsigned integer to hexadecimal notation.

#include    <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
unsigned x = 0;

for(x; x < 100; x++){
printf("%d \t %x \n", x, x);
}

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

As we have seen, the printf() function can handle both fixed and variable parts. The function takes at least one argument, the format string that contains both literal text and conversion specifiers. The values stored in optional arguments sent after the format string literal are inserted in the places indicated by the conversion specifiers. The literal text is printed unchanged.

The %d conversion specifier is used for int values. The %c specifier is used for char values. The %u specifier is used for unsigned values. The %x specifier converts an unsigned integer to hexadecimal notation.

#include    <stdio.h>
#include    <stdlib.h>
/* ------ main function ------- */

int main ( int argc, char *argv[] )
{
unsigned x = 0;

for(x; x < 100; x++){
printf("%d \t %x \n", x, x);
}

return EXIT_SUCCESS;
}

/* ----------  end of main function  ---------- */

Floating point numbers are real numbers; they include the types float, double, and long double. Each of these types varies in the precision with which a particular value is stored and expressed. A long double may or may not be more precise than a double, and float value types are rarely used.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s