Variables and Data types


   Keywords / Reserve Words   

Keywords are predefined, reserved words used in programming that have special meanings to the compiler. As C is a case sensitive language, all keywords must be written in lowercase. There are only 32 keywords in the C language.

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


   Identifiers   

C identifiers represent the name in the C program, for example, variables, functions, arrays, structures, unions, labels, etc. An identifier can be composed of letters such as uppercase, lowercase letters, underscore, digits, but the starting letter should be either an alphabet or an underscore. If the identifier is not used in the external linkage, then it is called as an internal identifier. If the identifier is used in the external linkage, then it is called as an external identifier.

We can say that an identifier is a collection of alphanumeric characters that begins either with an alphabetical character or an underscore, which are used to represent various programming elements such as variables, functions, arrays, structures, unions, labels, etc. There are 52 alphabetical characters (uppercase and lowercase), underscore character, and ten numerical digits (0-9) that represent the identifiers. There is a total of 63 alphanumerical characters that represent the identifiers.

Rules for writing an identifier
  • The first character of an identifier should be either an alphabet or an underscore, and then it can be followed by any of the character, digit, or underscore.
  • It should not begin with any numerical digit.
  • In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers are case sensitive.
  • Commas or blank spaces cannot be specified within an identifier.
  • Keywords cannot be represented as an identifier.
  • The length of the identifiers should not be more than 31 characters.
  • Identifiers should be written in such a way that it is meaningful, short, and easy to read.


   Variable    

Programming language enable you to assign symbolic names, known as variable names, for storing program computations and results. C variables are flexible containers for data. You can pick descriptive names to show what they hold, and they can store various types of information: whole numbers (integers), decimals (floats), single characters, text strings, and even memory addresses (pointers). They must begin with a letter or underscore ( _ ) and can be followed by any combination of letters (upper-or lowercase), underscores, or the digits 0–9.

Valid variable names
  • sum
  • pieceFlag
  • i
  • J5x7
  • Number_of_moves
  • _sysflag

Invalid variable names
  • sum$value -  $ is not a valid character.
  • piece flag - Embedded spaces are not permitted.
  • 3Spencer Variable names cannot start with a number.
  • int - int is a reserved word.


   Data Types   

A data type specifies the type of data that a variable can store such as integer, floating, character, etc. There are the following data types in C language.

         Types                              Data Types                   
Primitive Data Type         int, char, float, double              
Derived Data Type         array, pointer, structure, union
Enumeration Data Type enum                                       
Void Data Type                 void                                           

Int:
Integers are entire numbers without any fractional or decimal parts, and the int data type is used to represent them. It is frequently applied to variables that include values, such as counts, indices, or other numerical numbers. The int data type may represent both positive and negative numbers because it is signed by default. An int takes up 4 bytes of memory on most devices, allowing it to store values between around -2 billion and +2 billion.

Char:
Individual characters are represented by the char data type. Typically used to hold ASCII or UTF-8 encoding scheme characters, such as letters, numbers, symbols, or commas. There are 256 characters that can be represented by a single char, which takes up one byte of memory. Characters such as 'A', 'b', '5', or '$' are enclosed in single quotes.

Float:
To represent integers, use the floating data type. Floating numbers can be used to represent fractional units or numbers with decimal places. The float type is usually used for variables that require very good precision but may not be very precise. It can store values with an accuracy of about 6 decimal places and a range of about 3.4 x 1038 in 4 bytes of memory.

Double:
Use two data types to represent two floating integers. When additional precision is needed, such as in scientific calculations or financial applications, it provides greater accuracy compared to float. Double type, which uses 8 bytes of memory and has an accuracy of about 15 decimal places, yields larger values. C treats floating point numbers as doubles by default if no explicit type is supplied.

Data Types                    Memory Size                Range                   
char                                             1 byte                                   −128 to 127
signed char                         1 byte                             −128 to 127
unsigned char                 1 byte                             0 to 255
short                                         2 byte                             −32,768 to 32,767
signed short                         2 byte                             −32,768 to 32,767
unsigned short                 2 byte                             0 to 65,535
int                                         2 byte                             −32,768 to 32,767
signed int                         2 byte                             −32,768 to 32,767
unsigned int                         2 byte                             0 to 65,535
short int                         2 byte                             −32,768 to 32,767
signed short int         2 byte                             −32,768 to 32,767
unsigned short int         2 byte                             0 to 65,535
long int                                 4 byte                             -2,147,483,648 to 2,147,483,647
signed long int                 4 byte                             -2,147,483,648 to 2,147,483,647
unsigned long int         4 byte                             0 to 4,294,967,295
float                                         4 byte
double                                 8 byte
long double                         10 byte

   Data Types & Constants   

In C, any number, single character, or character string is known as a constant. There are five types of constants in C programming language.
  • Integer constants
  • Floating point constants
  • Character constants
  • Character String Constants
  • Enumeration Constants 
There are two different types of define constants in C programming language
  1. Use const keyword
  2. Use #define preprocessor
Use const keyword
const data_type constant_name = constant_value;
const float PI = 3.1412;

Use #define preprocessor
#define constant_name constant_value
#define PI 3.1412

   Constant Expression   

Expressions consisting entirely of constant values are called constant expressions. Expression 128 + 7 – 17 is a constant expression because the all values are constant values. 128 + 7 – i would not represent a constant expression because i was represented as a variable.

   Variable Declaration    

  • Declaration is done by simply listing the variables before the terminating semicolon of the definition.
                data_type variable;
                int age;

  • You can declare multiple variables in same type using a single statement as follows;
                                           int height, width;

   Assigning Values to Variables    

  • After declaring a variable with a type, you can add some content or value to that variable.
  • This is known as assigning values to variable. 
  • Some times we have to initialize the variables to its initial or default values and it is based on the problem we are addressing.  
  • This is known as variable initialization.
There are two different way to assign value to variable
  • Assign the value directly in the program.
  • Ask from user to input a value and then assign that value.
Assign the value directly in the program.
method 1 :-
        data_type variable;
        variable = value;
        int length;
        length = 20; 
method 2 :-
                      data_type variable = value;
        int length = 20;


   Variable initialization   

int age = 0;
float average = 0.0;
int minimum_marks = 40;

1)
#include <stdio.h>
int main (void)
{
int answer, result;
answer = 100;
result = answer - 10;
printf ("The result is %i\n", result + 5);
return 0;

Output - The result is 95

2)
#include <stdio.h>
int main (void)
{
char c, d;
c = 'd';
d = c;
printf ("d = %c\n", d);
return 0;
} 

Output - d = d

Comments

Popular posts from this blog

Programming Errors in C

Simple and basic C program code

printf() Function