Saturday, September 26, 2020

thumbnail

enums and char | Data Types | C

 Hello Guys, 


In this blog, we will be discussing enums and char in C. 

Enums

    A datatype that allows a programmer to define a variable and specify the valid values that can be stored into the variables.

    For example, let's create a variable named "optCompany" and it can only contain one of the primary colors as "GOOGLE", "MICROSOFT", or "NETFLIX", and no other values.

HOW TO DEFINE THE ENUM TYPE

    You have to define the enum type and give it a name as:

        1) initiated by keyword enum
        2) then the name of the enumerated datatype 
        3) then the list of identifiers (enclosed within a set of curly braces) that define the permissible values that can be assigned to the type.

enum optCompany {GOOGLE, MICROSOFT, NETFLIX};
    Variables declared to be of this datatype can be assigned GOOGLE, MICROSOFT, and NETFLIX inside the program, and no other value.

DECLARING THE VARIABLE 

    To declare the variable to be of type enum optCompany 
        1) use the keyword enum
        2) followed by the enumerated datatype
        3) followed by the variable list so the statement.

enum optCompany myCompany, jakesCompany;

The above sentence defines the variable myCompany and jakesCompany to be of optCompany datatype.

The only permissible values that can be assigned to these variables are the names GOOGLE, MICROSOFT, and NETFLIX.

As 
myCompany = GOOGLE;
Another Example,
enum month {January, February, March, April, May, June, July, August, September, October, November, December};

ENUM AS INT

    The compiler actually treats enumeration identifiers as an integer constant.
        The first name on the list is 0.

example;
enum month thisMonth;
....
thisMonth = February;
    Here, the value 1 is assigned to this month (not the name February) because it is the second identifier listed inside the enumeration list.

    If you want to have the special integer value associated with an enumeration identifier the integer can be assigned to the identifier directly when the data type is defined.

eg.
enum direction {up, down, left = 10, right};
        1) an enumerated datatype direction is defined with the values up, down, left, right.
        2) up = 0 because it appears first in the list.
        3) 1 to down because it appears next
        4) 10 to left because it is explicitly assigned this value
        5) 11 to the right because it appears immediately after left on the list.

Char

    char represents a single character such as the letter 'a' and a digital number '7', or a semicolon ';'.

    Character literal uses single quotes such as 'A' or '8'. 

    You can also declare the char variable to be unsigned.
        It can also be used to explicitly tell the compiler that a particular variable is a signed quantity.

DECLARING THE CHAR

char hello;            /* declare a char variable */
hello = 'T'            /* OK */
hello = "T"          /* No! Thinks T is a string --- ERROR!!! */
hello = T            /* Searches for the variable T in the program --ERROR!!!*/
char grade = 65;    /* OK for ASCII, but poor style */

Escape Characters

    C contains a special character that represents an action as:
  •         backspacing
  •         going to the next line
  •         making the terminal bell ring
    We can represent these actions by using a special; symbol a sequence called an escape sequence.

    These are also encoded in single quotes when assigned to a char variable

    Below are the escape characters:::

                        \n                                    ---        next line
                        \a                                    ---        Alert (ANSI C)
                        \b                                    ---        Blankspace
                        \f                                    ---        Form Feed
                        \r                                    ---        carriage return
                        \t                                    ---        horizontal tab
                        \v                                    ---        vertical tabs
                        \\                                     ---        backslash (\)
                        \'                                     ---        single quotes (')
                        \"                                    ---        double quotes (")
                        \?                                    ---        Question mark (?)
                        \Ooo                               ---        Octal Values
                        \xhh                                ---         Hexadecimal values

Example:

/* program to demonstrate enum datatype */
#include<stdio.h>
int main() 
{    
    enum gender {male, female};
    enum gender myGender; 
    myGender = male return
}

/* program to demonstrate char datatype and escape character */
#include<stdio.h>
int main()
{
    char myCharacter = '\n';
    print("%c", myCharacter);
    return 0;
}


You can get the video on my YouTube channel eat_sleep_ code repeat or play the video below.


Thanks, for reading. Do subscribe to getting notified for the next blog. 

Then it is Harsh Jaiswal.
Signing off.



Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

Sieve Of Eratosthenes | Mathematics | Data Structures and Algorithm | Python

Problem Statement: The  Sieve of Eratosthenes  is a method for finding all primes up to (and possibly including) a given natural n . n .  Th...