Basic Elements of C

By Paribesh Sapkota

Basic Elements of C language
❖ They are basic building blocks used to create a C program.
1. Character set
2. Data types
3. Identifiers and Keywords
4. Variables and constants
5. Special symbols
6. Operators
7. Expressions and statements
8. Arrays
9. Function

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.


#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.


#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.


#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.


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

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


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.

C Keywords
Keywords are predefined words and they have fixed meanings in C and their meaning cannot be changed. These keywords can be used only for their intended purpose; they cannot be used as programmer-defined identifiers.  All keywords are lowercase. Since uppercase and lowercase characters are not equivalent in C language, it is possible to utilize an uppercase keyword as an identifier.

A list of 32 keywords in the c language is given below:

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

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. }

1. **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.

2. **C Character Set:**
– The C character set is the set of characters allowed in the C programming language. It includes letters, digits, and special characters, such as operators and punctuation.

3. **C Tokens:**
– Tokens are the smallest units in a C program. They can be keywords, identifiers, constants, operators, and punctuation.

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

5. **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.

6. **Variables:**
– Variables are named storage locations in memory used to hold data during the execution of a program. They must be declared with a specific data type.

7. **Data Types:**
– Data types in C specify the type of data that a variable can hold. Common data types include int, float, double, char, etc.

8. **Constants/Literals:**
– Constants are fixed values that do not change during the program execution. They can be numeric constants, character constants, or string literals.

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

10. **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.

11. **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.

12. **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.

Understanding these basic elements is crucial for writing C programs effectively. Keep in mind that the details may vary slightly depending on the specific version of the C language you are working with.

Important Questions
  Loading . . .