7. Components of C language

Table of Contents
< All Topics
Print

7. Components of C language

C components are essential elements of a C program, including comments, preprocessor directives, functions, statements, etc. They are utilized to write and organize the code in a logical and systematic manner. Every language has some basic components (also called as elements) & grammatical rules. Before starting with programming, we should be acquainted with the basic elements that build the language. In C language, these components are of six different types.

1.Character Set

Communicating with a computer involves speaking the language the computer understands.
In C, various characters have been given to communicate.

TypesCharacter Set
Lower casea-z
Upper caseA-Z
Digits0-9
Special Character!@#$%^&*, etc.
White spaceBlank space, newline, horizontal tab, carriage return and form feed.

List of all Special Character

,<>._
();$:
%[]#?
&{}
^!*/|
\~+

2. C Tokens

C language, serving as its basic building blocks. These tokens play a crucial role in the structure and functionality of the C programming language, allowing for the creation of complex and powerful software applications. Understanding the various types and uses of C tokens is essential for mastering the language and harnessing its full potential in software development.

In short we can summarize tokens as:

  • Tokens are basic building block of C language.
  • It is the smallest individual unit in a C program.
  • Every program is developed with the help of token only.
  • The tokens in C are-
    • Keywords
    • Identifiers/Variables
    • Constants
    • Strings
    • Special Characters
    • Operators

3. Keywords

Keywords are the words whose meaning has already been explained to the C compiler.

Keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer.

There are only 32 keywords available in ANSI C. But, C99 have added some new keywords too.

The figure below provides a list of these keywords for your quick reference.

Keywords

Rules for the keywords

  1. A valid identifier can consist of uppercase and lowercase letters, digits, and underscores.
  2. The first character of an identifier should be a letter or an underscore.
  3. You cannot use reserved keywords such as int or while as identifiers.
  4. There’s no specific rule for the length of an identifier. Nonetheless, in certain compilers, using an identifier longer than 31 characters may lead to issues.

You can select any name as an identifier if you follow the above rule but give meaningful names to identifiers that make sense.

4. Identifiers

  • A C identifier is a name used to identify a variable, function, or any other user-defined item.
  • An identifier starts with a letter A to Z, a to z, or an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9).
  • C does not allow punctuation characters such as @, $, and % within identifiers.
  • C is a case-sensitive programming language.
Example:

Humanpower andhumanpowerare two different identifiers in C. Similarly, few more valid identifiers can be−

length abc email_1 _temp a_123myname50 j a23b9 returnVal

Rules to define Identifiers-
  1. First character: either alphabet or underscore.
  2. Only letters, digits, and underscores are allowed.
  3. Only first 31 characters are significant in declaring the identifiers.
  4. Any identifier cannot use a keyword.
  5. White spaces are not allowed in defining the identifiers.

5. Constant (or Literal)

  • Constants or literals refer to fixed values that the program may not alter during its execution.
  • Constants can be of any of the basic data types like an integer, a floating, a character, or a string.
  • Constants are treated just like regular variables except that their values cannot be modified after their definition.
Image 21

Constants: Integer Type

Below mentioned points will help to understand the integer constants:

  • An integer literal can be a decimal, octal, or hexadecimal constant.
  • A prefix specifies the base or radix:
    • 0x or 0X for hexadecimal,
    • 0 for octal, and
    • nothing for decimal.
  • An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively.
  • The suffix can be uppercase or lowercase and can be in any order.
Image 22

Constants: Floating-point Literals/Constants

A floating-point literal has an integer part with a decimal point or a fractional part with an exponent part.

Image

Constants: Character Constants

Character literals are enclosed in single quotes, e.g., ‘x’ can be stored in a simple variable of char type.

A character literal can be a Plain/ single character (e.g., ‘x’), or an escape sequence (e.g., ‘\t’)

Single Character Constants

It simply contains a single character enclosed within a pair of single quotes.

Keep a note that the character ‘8’ is not the same as 8. Character constants have a specific set of integer values known as ASCII values (American Standard Code for Information Interchange).

Example:

‘X’, ‘5’, ‘;’

String Constants

Sequence of characters enclosed in double quotes, and they may include letters, digits, special characters, and blank spaces.

Please note that “G” and ‘G’ are different, here “G” represents a string as it is enclosed within a pair of double quotes whereas ‘G’ represents a single character.

Example:

“Hello!“ “2015“ “2+1”

Backslash character constant

C supports character constants with a backslash in front of them.Character constants with a backslash in front of them are known as escape sequences. These escape sequences allow for the representation of non-printable characters, such as newline or tab, as well as characters that are difficult to represent directly, such as single or double quotes.

The lists of backslash characters have a specific meaning which is known to the compiler. They are also termed as “Escape Sequence“.

ConstantsMeaning
\abeep sound
\bbackspace
\fform feed
\nnew line
\rcarriage return
\thorizontal tab
\vvertical tab
\’single quote
\”double quote
\\backslash
\0null

Convert an identifier to a constant

One can convert any variable to a constant by using the keyword “const” as a prefix to its data type.
const int x = 5;

Syntax

const datatype variableName = value;

Example

const int MAX=30;

Here, the statementconst int MAX=30;is a way of declaring a constant integer variable in C or C++. A constant variable is one that cannot be changed or modified by the program after its initial declaration.

Theconstkeyword is used to specify that the variable is constant. Theintkeyword is used to specify that the variable is of integer type, which means it can store whole numbers. TheMAXis the name of the variable, which can be any valid identifier. The30is the value assigned to the variable, which must be an integral constant expression. This means that the value must be known at compile time and cannot depend on any variables or functions.

Benefits of using const keyword:

  • They can improve the readability and maintainability of the code by giving meaningful names to constant values.
  • They can prevent accidental or intentional changes to the values that could cause errors or unexpected behavior.
  • They can help the compiler optimize the code by replacing the variable names with their values at compile time.

Few more example of const:

  • To define the size of an array:
    • const int SIZE=10;
    • int arr[SIZE];
  • To define the value of pi:
    • const double PI=3.14159;
  • To define the maximum score in a game:
    • const int MAX_SCORE=100;

ASCII Table

ASCII stands for American Standard Code for Information Interchange. The ASCII table is a standard character encoding scheme that represents text in computers and other devices. It consists of 128 code points, of which 95 are printable characters such as letters, digits, and punctuation marks. The ASCII table was developed in the 1960s and is still widely used today.

The ASCII is a standard way of representing characters as numbers in the range of 0 to 127 ( as 8bit binary numbers). In C language, ASCII is used to store and manipulate characters, such as letters, digits, punctuation marks, and control codes. For example, the ASCII value of ‘A’ is 65, and the ASCII value of ‘a’ is 97. C language provides functions and operators to convert between characters and their ASCII values, such as the char type, the int type, the %c and %d format specifiers, and the getchar and putchar functions.

Image 1
Source: https://simple.wikipedia.org/wiki/ASCII

Few common and important ASCII values for a programmers are:

Image 2

Example: 1. Print ASCII char input from keyboard

// print ASCII char input from keyboard
// from 0 to 127
#include
void main()
{ int ascii;
    printf("\n Enter ascii number between ZERO to 127:\t");
    scanf("%d", &ascii);
    printf("\n for ASCII number = %d the ASCII Character is = %c", ascii, ascii);
}

Output

Image 3

Example 2: Program to convert the case of entered character

='A' && ch<='Z') {printf ("entered character is in uppercase "); printf("the conversion is = %c", ch+32); } else if (ch>=‘a' && ch<=‘z') {printf ("entered character is in lowercase "); printf("the conversion is = %c", ch-32); } else {printf (“ \n You have typed non Alphabet "); } return 1; }//end main " style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button">
// Programm to convert the case of entered character 
//i.e.lower to upper or upper to lower 
#include

int main()
{
    char ch;
    printf("\n Enter any English Alphabet\n\t");
    scanf("%c",&ch);
    

    if (ch>='A' && ch<='Z')
        {printf ("entered character is in uppercase   ");
         printf("the conversion is = %c", ch+32);          }
    else if (ch>=‘a' && ch<=‘z')
        {printf ("entered character is in lowercase   ");
         printf("the conversion is = %c", ch-32);          }
    else
        {printf (“ \n You have typed non Alphabet "); }
 

    return 1;
}//end main

Output

Image 4

Leave a comment