Broad Network


Declare, Initialize and Define Fundamental Objects in C

Part 4 of Complete C Course

Foreword: How to declare, initialize and define Fundamental Objects in C

By: Chrysanthus Date Published: 31 May 2024

Recall

The memory of a computer is a long series of cells. Imagine the cells placed next to one another in one long line, going up. A consecutive group of these cells that represent an object, is called a region. The group should have a minimum of one cell. If this region has been designated to hold datum, such as an int or float or any of the other data types, it is called an object (precisely a fundamental object). So, an object can consist of one or more consecutive cells. An object can hold a datum. This datum is the value of the object. There are different types of data. Fundamental object types are: the int, float, char, bool and void object types. Some of these object types have variants (subtypes). A derived object type is a construction of two or more of the fundamental types. The array type (see later) is a derived object type.

Integer and floating types are collectively called Arithmetic Types. Arithmetic types and Pointer types (see later) are collectively called scalar types. The type of datum an object will hold is determined, when the object is created.

The series of cells (bytes) of the memory are numbered numerically, consecutively. These numbers are called addresses. So each memory cell (byte) has a unique memory address (typically written in hexadecimal).

The int Identifier
If an object is to hold an integer, say 45, then that can be written as a statement, as follows:

    int myInt = 45;

The statement begins with the type of value the object holds. This is followed by a space and then a name that identifies the object. This name is called the identifier. The programmer decides on what name to give, as the identifier. Then there is the = symbol. Here the = symbol is called the assignment operator, and it is not called the equal sign. Never call it the equal sign, as is done in mathematics; that would be misleading. Then there is the value of the integer. Finally there is the semicolon, which makes the line, a statement. Now, we say, the value 45 is assigned to the integer object, identified by myInt.

To test the following code, replace all the content of the temp.c file with the following code, then do "gcc -o temp temp.c" without the quotes at the terminal window, and press Enter:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int myInt = 45;
        printf("%i\n", myInt);
    
        return 0;
    }

45 should be displayed in the terminal window. The first statement in the main function is,

    int myInt = 45;

45 has been assigned to an object of type integer, with identifier name, myInt. That is, 45 is now in the location (object area) in memory, identified by myInt. A typical statement, ends with a semicolon. The second statement (also ending with a semicolon) is:

        printf("%i\n", myInt);

This statement prints the value of myInt to the screen (terminal). Details of the printf() function, will be explained later in this course. For now, just note that its first argument is "%i\n", and its second argument is myInt. Notice that the C main function, has also been given the parameters, "int argc" and "char *argv[]". Details of these special parameters will be explain before the end of this course. The last statement, "return 0;" , should appear at the end of every C or C++ main function.

Note that the opening and closing double quote, for programming is " , and not " or " . " or " is provided by the word processor, while " for opening and closing, is provided by the text editor. With " or " as double quote in programming, the program will not compile, and the compiler will issue an error message (complaining of an unexpected character).

The float Identifier
If an object is to hold a float, say 56.74, then that can be written as a statement, as follows:

    float myFloat = 56.74;

The statement begins with the type of value the object holds; the object holds a float. This is followed by a space and then a name that identifies the object. This name is called the identifier. It is the programmer who decides on what name to give, as the identifier. Then there is the assignment operator. Then there is the value for the float. Finally there is the semicolon, which makes the line, a statement. We say the value 56.74 is assigned to the float object, identified by myFloat.

Test (type or copy and paste, save, compile and run) the following code:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        float myFloat = 56.74;
        printf("%f\n", myFloat);
    
        return 0;
    }

The output is 56.740002 at the author's computer. How to remove trailing decimal digits or rounding numbers, will be explained in this course, later. The first statement in the block should be self-explanatory by now, as it is similar to the first statement of the previous block. The second statement displays the value held by the object identified by myFloat. The first argument in the printf() function here, for float, is "%f\n" and not "%i\n" for integer.

Now, though identifiers can be called variables, avoid doing so. Only use the word, variable, for identifier, when it would not be misleading.

The char Identifier
If an object is to hold a char, say 'E', then that can be written as a statement, as follows:

    char myChar = 'E';

The statement begins with the type of value the object holds; the object holds a char. This is followed by a space and then the identifier of the object. Then there is the assignment operator. Then there is the value for the character (char). Finally, there is the semicolon, which makes the line a statement. We say the value 'E' is assigned to the char object, identified by myChar.

With the char object, the value assigned has to be in single quotes as with 'E'. Double quotes is used for strings (see later). Test the following code:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        char myChar = 'E';
        printf("%c\n", myChar);
    
        return 0;
    }

The value, 'E' should be printed at the terminal window, without the single quotes. The first and second statements in the block should be self-explanatory, by now. Note that the first argument in the printf() function here, for char, is "%c\n" and not "%f\n" for float, or "%i\n" for integer.

The bool Identifier
There are only two possible values for the _Bool type, which are true and false. With the gcc compiler, true is typed as 1 and false is typed as 0. With the gcc compiler, the bool type is typed as "_Bool" and not "bool", as given in the specification. Test the following code:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        _Bool myBoolTrue = 1;
        _Bool myBoolFalse = 0;
        printf("%i\n", myBoolTrue);
        printf("%i\n", myBoolFalse);
    
        return 0;
    }

The output is 1 for true and 0 for false.

In the above examples the identifiers are said to be defined and initialized at the same time. Defining and initializing an object are not exactly the same thing.

Declaring Objects

An identifier identifiers an object. An object is a region in memory, having its value. Declaring an object means deciding what type of datum (or group of data in case of an array for example) the object will hold. The following statement declares an object to be of type, int:

    int hisInt;

When the program is being executed, and it sees the above statement, a number of consecutive memory cells are allocated somewhere in memory. This group of cells form the location for the object, and will have to hold only an integer, at initialization; and not a float or char or some other value. This location is identified by the identifier, hisInt. It should only hold an integer because of the preceding reserved word, int . "int hisInt;" is an independent statement that ends with a semicolon.

float and char objects can be declared in a similar way. The following identifier (herFloat) identifiers a location that should only hold a float:

    float herFloat;

The following identifier (theChar) identifies a location that should only hold a character (char):

    char theChar;

Objects are declared this way, and the syntax is:

    Type ident;

where ident means identifier, and Type stands for int, or float, or char, etc.

An object can be declared and then have a value assigned to the object, through its identifier, later in the program. The following code segment illustrates this:

    int myInt;
    myInt = 45;

This code segment has two statements. The first one declares the identifier, myInt. So, if the location identified by myInt will have any value, it will be an integer. The second statement assigns the integer value, 45 to the location identified by myInt. Now, the object is complete. That is, the second statement gives the location, a value (of type int). In other words, the second statement makes the value of 45, be kept in the region (group of cells) identified by myInt.

Note that in the second statement, you do not have the preceding, int. The preceding int or type only occurs during declaration, with or without value assigned (as in the first statement). So, after declaration, just use the identifier for assignment, without the preceding type.

Read and test the following code:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        char herChar;
        herChar = 'q';
        printf("%c\n", herChar);
    
        return 0;
    }

The output is 'q'. Note: After declaring, use the identifier without the preceding type, as has been done in the second and third statements in the block above.

Declaring more than one Object
More than one object can be declared in one statement. Read and test the following code that illustrates this:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int myInt, yourInt, hisInt;
        yourInt = 702;
        printf("%i\n", yourInt);
    
        return 0;
    }

The result should be 702. Three identifiers (objects) are declared in one statement. Only one is used. That is, only one has been assigned a value. That is alright. Nothing stops the programmer from using two or all three for different assignments.

Initialization

Initialization means assigning a value to the newly declared object. This can be done after declaration in two statements, or during declaration in one statement. After declaration or initialization, use the identifier without the preceding type. Read and test the following code, where initialization is done with declaration, in one statement:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        float theFloat = 25.63;
        printf("%f\n", theFloat);
    
        return 0;
    }

The output from the author's computer is 25.629999. Rounding of float numbers to two decimal places, will be explained later on, in this course. Note that in the second statement of the block, the identifier is used without the preceding type (float). That is, after the single statement for declaration with or without initialization, use the identifier without the preceding type.

Definition

Definition means having a practical value for the identifier. Declaration can mean definition as in the single initialization statement above, or it can mean just having the identifier with memory allocated to it, without a practical value. The difference between declaration and definition is explained in detail in the next tutorial (part of the series/course).

Initialization after Declaration
Initialization can be done just after declaration as in the following code (read the code, comments, and test the code):

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        float theFloat;    //declaration
        theFloat = 2.5;    //initialization
        printf("%f\n", theFloat);
    
        return 0;
    }

The output is 2.500000 . How to round or remove unwanted decimal digits at the end of a float result, will be explained later in this course.

Multiple Variable Declaration
Some different data types can be grouped into a larger category, called the scalar type. The char type, the integer type, the float type, the double and the pointer type form the larger category, called the scalar type. Arrays and structures form another larger category, called the aggregate type - see later.

More than one character identifier, can be declared in one statement, as in the following code segment:

        char ch1, ch2, ch3;
        ch1 = 'A';
        ch2 = 'B';
        ch3 = 'C';
        printf("%c, %c, %c\n", ch1, ch2, ch3);

The output is: A, B, C . The first statement declares the character identifiers: ch1, ch2 and ch3. The second, third and fourth statements assign their values (initialize). Note the arrangement of text in the first argument of the printf() function. Note the corresponding second, third and fourth arguments of the printf() function.

More than one float identifiers can be declared in one statement, as in the following code segment:

        float ft1, ft2, ft3;
        ft1 = 1.1;
        ft2 = 2.2;
        ft3 = 3.3;
        printf("%f, %f, %f\n", ft1, ft2, ft3);

The output is: 1.100000, 2.200000, 3.300000 . The first statement declares the float number identifiers: ft1, ft2 and ft3. The second, third and fourth statements assign their values (initialize). Note the arrangement of text in the first argument of the printf() function. Note the corresponding second, third and fourth arguments of the printf() function.

Conclusion
Declaration, initialization and definition have been addressed above using fundamental objects in C. The difference between definition and declaration is explained in detail in the next tutorial (part of the series/course).



Related Links

More Related Links

Cousins

BACK NEXT

Comments