Basic Elements of C

By Paribesh Sapkota

Introduction:

C Standards:

C is a standardized programming language. The standards are set by organizations like ANSI (American National Standards Institute) and ISO (International Organization for Standardization). Commonly referred to standards include ANSI C, ISO C, and more recently, the C11 standard.

Character Set in C

Character Set is a collection of permissible characters that can be used in a variety of contexts by the program. In this article, we covered the history of Character encoding’s. Here, we also discuss the historical encoding system known as EBCDIC, the current coding standard Unicode, and ASCII.

Types of Character Set

Generally, there are two types of character sets in C.

Source Character Set (SCS):

Before preprocessing, SCS is used to parse the source code into an internal representation. White-space characters and the Basic Character package are included in this package. It is the collection of symbols that can be used to create source code. The initial stage of the C PreProcessor (CPP) is to translate the encoding of the source code into the Source Character Set (SCS), which is done before the preprocessing phase.

Execution Character Set (ECS):

Constants for character strings are stored in ECS. This set includes Control Characters, Escape Sequences, and the Basic Character Set. It is the set of characters that the program in use can decipher. CPP converts character and string constant encoding into the Execution Character Set (ECS) following the preprocessing phase.

 

C Data Types

Data types are the type of data stored in a C program. Data types are used while defining a variable or functions in C. It’s important for the compiler to understand the type of predefined data it is going to encounter in the program.C provides several built-in data types, such as integer (int), character (char), floating-point (float), and double-precision floating-point (double), among others. Each data type has its own set of possible values and operations that can be performed on it.

Types Of Data Types In C

There are majorly five main categories of Data Type in C:

Data Type Example of Data Type
Primary Data Type Integer, Floating-point, double, string.
Derived Data Type Union, structure, array, etc.
Enumerated Data Type Enums
Void Data Type Empty Value
Bool Type True or False

Size Of Data Types In C

The size of each data type is defined in bits or bytes (8 bits). Each data type in C is associated with a specific range of values defined as below:

Data Type Format Specifier Minimal Range Size in bit
unsigned char %c 0 to 255 8
char %c -127 to 127 8
signed char %c -127 to 127 8
int %d, %i -32,767 to 32,767 16 or 32
unsigned int %u 0 to 65,535 16 or 32
signed int %d, %i -32,767 to 32,767 (same as int) 16 or 32
short int %hd -32,767 to 32,767 16
unsigned short int %hu 0 to 65,535 16
signed short int %hd Same as short int 16
long int %ld, %li -2,147,483,647 to 2,147,483,647 32
long long int %lld, %lli -(2^63) to (2^63)-1 64
signed long int %ld, %li Same as long int 32
unsigned long int %lu 0 to 4,294,967,295 32
unsigned longlong int %llu (2^63)-1 64
float %f 1E-37 to 1E+37 along with six digits of the precisions 32
double %lf 1E-37 to 1E+37 along with six digits of the precisions 64
long double %Lf 1E-37 to 1E+37 along with six digits of the precisions 80

 

Primary Data Types In C

The C programming language has five primitive or primary data types.

1. Integer (int): Refers to positive and negative whole numbers (without decimal), such as 10, 12, 65, 3400, etc.

Example:

#include <stdio.h>   
void main() 
{ 
 int i = 5; 
 printf("The integer value is: %d \n", i); 
}

2. Character (char): Refers to all the ASCII character sets within single quotes such as ‘a’, ‘A’, etc.

Example:

#include <stdio.h>   
void main() 
{ 
 char c = 'b'; 
 printf("The character value is: %c \n", c); 
}

3. Floating-point (float): Refers to all the real number values or decimal points, such as 3.14, 10.09, 5.34, etc.

Example:

#include <stdio.h>   
void main() 
{
 float f = 7.2357; 
 printf("The float value is: %f \n", f); 
}

4. Double (double): Used when the range exceeds the numeric values that do not come under either floating-point or integer data type.

Example:

#include <stdio.h>   
void main() 
{ 
 double d = 71.2357455; 
 printf("The double value is: %lf \n", d); 
}

Identifiers
Identifiers are names given to identify various items in the program, such as variables, functions and arrays. An identifier consists of letters and digits, in any order, except that the first character must be a letter. Both upper and lowercase letters are permitted. Upper and lowercase letters are however not interchangeable (i.e., an uppercase letter is not equivalent to the corresponding lowercase letter). The underscore character (_) can also be included, and it is treated as a letter. Keywords like if, else, int, float, etc., have special meaning and they cannot be used as identifier names.

Identifiers Naming Rules
◼ Identifier names must consists of combination of only letters, digits, underscore and must begin with an alphabet (or underscore)
◼ An identifier should not contain a space.
◼ Underscore is permitted between two digits and must begin with a letter
◼ An identifier name should have less than 31 characters. Only first 31 characters are significant.
◼ Any standard C language keyword cannot be used as a identifier.
◼ It is case sensitive, i.e. uppercase and lowercase letters are not interchangeable.

Examples of valid identifier
◼ X
◼ Y1
◼ name123
◼ stud_name
◼ _row
◼ circumference
◼ volume
◼ TOTAL

 

Examples of invalid identifier
1st – the first character must be a letter
“Jamshedpur” – illegal characters (“)
‘Y’ – illegal characters (‘)
stud-name – illegal character (-)
stud name – illegal character (blank space)

Types of identifiers

  • Internal identifier
  • External identifier

Internal Identifier

If the identifier is not used in the external linkage, then it is known as an internal identifier. The internal identifiers can be local variables.

External Identifier

If the identifier is used in the external linkage, then it is known as an external identifier. The external identifiers can be function names, global variables.

 

Variables in C

variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily identified.

Let’s see the syntax to declare a variable:

  1. type variable_list;

Rules for defining variables

  • A variable can have alphabets, digits, and underscore.
  • A variable name can start with the alphabet, and underscore only. It can’t start with a digit.
  • No whitespace is allowed within the variable name.
  • A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Valid variable names:

  1. int a;
  2. int _ab;
  3. int a30;

Invalid variable names:

  1. int 2;
  2. int a b;
  3. int long;

Types of Variables in C

There are many types of variables in c:

  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

Local Variable

A variable that is declared inside the function or block is called a local variable.

It must be declared at the start of the block.

  1. void function1(){
  2. int x=10;//local variable
  3. }

You must have to initialize the local variable before it is used.

Global Variable

A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions.

It must be declared at the start of the block.

  1. int value=20;//global variable
  2. void function1(){
  3. int x=10;//local variable
  4. }

Static Variable

A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

  1. void function1(){
  2. int x=10;//local variable
  3. static int y=10;//static variable
  4. x=x+1;
  5. y=y+1;
  6. printf(“%d,%d”,x,y);
  7. }

If you call this function many times, the local variable will print the same value for each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on.

Automatic Variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.

  1. void main(){
  2. int x=10;//local variable (also automatic)
  3. auto int y=20;//automatic variable
  4. }

External Variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.

  1. extern int x=10;//external variable (also global)
  1. #include “myfile.h”
  2. #include <stdio.h>
  3. void printValue(){
  4.     printf(“Global variable: %d”, global_variable);
  5. }

 

C Tokens:

A token in C can be defined as the smallest individual element of the C programming language that is meaningful to the compiler. It is the basic component of a C program.

Types of Tokens in C

The tokens of C language can be classified into six types based on the functions they are used to perform.

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Special Symbols
  6. Operators

 Keywords

The keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. C language supports 32 keywords which are given below:

auto         double      int        struct
break        else        long       switch
case         enum        register   typedef
char         extern      return     union
const        float       short      unsigned
continue     for         signed     void
default      goto        sizeof     volatile
do           if          static     while

Note: The number of keywords may change depending on the version of C you are using

Constants/literal

The constants refer to the variables with fixed values. They are like normal variables but with the difference that their values can not be modified in the program once they are defined.

Constants may belong to any of the data types.

Examples of Constants in C

const int c_var = 20;
const int* const ptr = &c_var;

Strings

strings  are nothing but an array of characters ended with a null character (‘\0’). This null character indicates the end of the string. Strings are always enclosed in double quotes.

Special Symbols

The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose. Some of these are listed below:

  • Brackets[]: Opening and closing brackets are used as array element references. These indicate single and multidimensional subscripts.
  • Parentheses(): These special symbols are used to indicate function calls and function parameters.
  • Braces{}: These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.
  • Comma (, ): It is used to separate more than one statement like for separating parameters in function calls.
  • Colon(:): It is an operator that essentially invokes something called an initialization list.
  • Semicolon(;): It is known as a statement terminator.  It indicates the end of one logical entity. That’s why each individual statement must be ended with a semicolon.
  • Asterisk (*): It is used to create a pointer variable and for the multiplication of variables.
  • Assignment operator(=): It is used to assign values and for logical operation validation.
  • Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before actual compilation.
  • Period (.): Used to access members of a structure or union.
  • Tilde(~): Bitwise One’s Complement Operator.

Escape Sequence: Escape sequences are special characters in strings that are preceded by a backslash. For example, `\n` represents a newline character.

Delimiters:
Delimiters are characters used to separate or define the boundaries of different parts of the program. For example, semicolons (`;`) are used to terminate statements.

Expressions:
– Expressions are combinations of variables, constants, and operators that can be evaluated to produce a value. For example, `a + b` is an expression.

Statements and Comments:
Statements are executable units of code. They can be expressions, assignments, loops, or control flow statements.
Comments are used to add explanations or notes within the code. They are ignored by the compiler.

Library Functions:
C provides a standard library that contains a set of functions that can be used in C programs. These functions perform various tasks and operations.

Preprocessor Directives:
– Preprocessor directives are commands that are processed by the C preprocessor before actual compilation. They start with a `#` symbol and include commands like `#include`, `#define`, etc.

Important Questions
Comments
Discussion
0 Comments
  Loading . . .