C Programming Series
Part 1: Introduction to C Programming
Part 2: Variables and Tokens

Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type also termed as data type, which determines the size and appearance of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive(Upper-case and lower-case are treated as different)

Variable Definition in C: It tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type. Consider the below example, the data type is int which means integer.

Definition : <<Data type>><<variable name>>;
Ex: int a;
    char c;
    float d;
 

Variable Initialization in C: Variables can be initialized (assigned an initial value) in their declaration. It consists of an equal sign followed by a constant expression as follows –

Initialization : <<Data type>><<variable name>>=<<value>>;
Ex: int a=10;
    char c= 'z';
    float m=3.14;
    

Rules to declare a variable:

  • It should not be a reserved word like int, float, double, break, etc
  • It should start with a letter or an underscore (_)
  • It can contain letters, numbers, or underscore. Choose a name preferably that reflects the role of that variable in the code, e,g.customer_name and not cn
  • No other special character is allowed including space
  • Variable names are case sensitive, A and a are considered different.
Important points regarding Variable:
  • Variable represents units of storage in a program.
  • They are used to store and retrieve data throughout the functioning of a program
  • The value of a variable can change as per the programmer’s choice
  • The Variable must be declared before they are assigned with a particular value
Data Type ,size and its Range

Tokens

Tokens are the smallest elements of a program, which are meaningful to the compiler. There are six types of tokens: Keywords, Identifiers, Constants, Strings, Special symbols, and Operators.

1. Keywords :

Keywords are pre-defined or word with reserved meaning in a programming language. Each keyword is previously set to perform a specific task in a program. They are reserved names and their meaning cannot be changed, therefore cannot be used as variable names. They cannot be redefined. C language supports 32 Keywords: auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof and static. All of these have a specific meaning and they work accordingly.

2. Identifiers :

Identifiers are used as a general term for the naming of variables, functions, and arrays. These are user-defined names which consist of arbitrary long sequences of letters and digits with either a letter or the underscore(_) as a first character. Keywords cannot be used as identifiers. Once declared, the identifier can be used in the program later as well to refer to the associated value.

There are certain things to keep in mind while naming C identifiers :

  • They must begin with a letter or underscore(_).
  • They must consist of only letters, digits, or underscore.No other special character is allowed.
  • It should not be a keyword.
  • It must not contain white space.
  • It should be up to 31 characters long as only the first 31 characters are significant.
  • Example : _A9 -VALID , Temp.var-INVALID, void-INVALID

3.Constants :

Constants are like variables but their values cannot be changed by the program once they are defined. They are also called as literals. Constants may belong to any of the data types. C supports five types of Constants: Integer, Real or Floating, Character, Octal & Hexadecimal, and String constants.

i. Integer Constant
  • Represents a signed integer of 2, 4 or 8 Bytes(16, 32 or 64 bits)
  • Precise size is dependent on machine
  • It is a number that has an integer value
  • It can be specified in decimal, octal or hexadecimal form
    • Example: 5,125
ii. Floating Constant
  • Floating constant have mantissa and exponent part (Including the letter E or e is optional) ddd.dddE(+/-)dd
  • Mantissa (significant part) contains digit followed by a decimal point(.) and then digit
    • Example: 453.678
iii. Character Constant
  • It is grouped into two categories: integer character, wide character
  • integer character is a sequence of one or more characters enclosed in single quotes. Ex: ‘a’
  • The character within the single quote may be any character (except backslash or newline or single quote)Ex: ‘d’, ‘f’.
iv. Octal & Hexadecimal Constant
  • C allows us to specify integer constants in terms of octal(number system based on 8, uses the digit 0 through 7) or hexadecimal(number system based on 16, uses digits 0 through 9 plus the letters A through F) instead of decimal.
  • A hexadecimal constant must consist of a Ox followed by the constant in hexadecimal form.
  • An octal constant begins with an O.
  • Example :
    • octal: (013) in octal = (11) in decimal
    • hexadecimal: (013) in hexadecimal = (19) in decimal
v. String Constant
  • A string constant may consist of any combination of digits, letters, escaped sequences, and spaces.
  • The string constant “A” consists of character A and \0. However, a single character string constant does not have an equivalent in bytes, one for ASCII code of A and another for the NULL character with a value 0, which terminates all strings.
  • Example :
    • valid string constant : “W”, “100”, “24, Hauskhas”
    • an invalid string constant: “W – the closing double-quotes missing, Raja” – the beginning double-quotes missing
Invisible Characters in C language

Some special characters are not visible directly in the output stream such characters begin with an escape character (i.e. \) and are called Invisible Characters.

  • \n: newline
  • \t: horizontal tab
  • \a: alert bell
  • \v: vertical tab

4. 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. Whereas, a character is enclosed in single quotes in C. Declaration of the string can be done in the following ways:

char string[20]={'v','t','u','c','o','n','n','e','c','t','\0'};
char string[20]="vtuconnect";
char string[]="vtuconnect";

5. Special Symbols :

The Following special symbols are used in C having some special meaning and thus, cannot be used for any other use. Some of the special symbols are :

  • Brackets[]: Opening and closing brackets are used as array element reference. 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 statements like for separating parameters in function calls.
  • semicolon: It is an operator that essentially invokes something called an initialization list.
  • asterisk (*): It is used to create a pointer variable.
  • assignment operator: It is used to assign values.
  • preprocessor(#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before actual compilation.

6. Operators :

Operators are symbols that cause an action when applied to C variables and other objects. The data items on which operators act upon are called operands. Depending on the number of operands that an operator acts upon, operators can be classified as follows:

  • Arithmetic operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Conditional Operators
  • Increment and decrement Operators
  • Special Operators
i. Arithmetic Operators:

Arithmetic operations are performed on integer or floating points such as addition, subtraction, multiplication, division, and modulus. Operators perform operations on operands.

Integer Arithmetic– When both operands are integers then the result of the arithmetic operation between them yields an integer value. Example: If a=10 and b=4, then a-b=6, a+b=14, a/b=2, a*b=40, and a%b=2.

Real Arithmetic– When both operands are decimal point numbers(float or double) then the result of the arithmetic operation between them yields the real value i.e; a decimal point number. If x and y are floats, then x =6.0/7.0=0.857143 and y=2.0/3.0=-0.666667.

Mixed-mode Arithmetic– One of the operands is real and the other is an integer.Example: 19/10.0 =1.9

ii. Assignment Operators:

Besides, C has a set of shorthand assignment operators of the form (var oper = exp;) which are used to assign certain values or variables containing certain values.It may also contain an expression as shown in the example below.
Example: x = a + b;

iii. Relational Operators:

To find the relationship between any two values, the relational operator is used, they compare the value and result in either true or false. The following relational Operators used in C programming :

  • < is less than
  • <= is less than or equal to
  • > is greater than
  • >= is greater than or equal to
  • == is equal to
  • != is not equal to
iv. Logical Operators:

These operators are used to perform logical operations on the given expressions. There are logical operators in C language. They are logical and (&&), logical or(||), and logical not(!).

  • Logical AND(&&) – It returns true when both conditions are true
    • Ex : if x=2 and y=3 , (x>1) && (y<5) will return true
  • Logical OR(||) – It returns true at least one of the conditions is true
    • Ex : if x=2 and y=3 ,(x>1) || (y<5) will return true
  • Logical NOT(!) – It reverses the state of the operand, if the expression evaluates to true, it will reverse it to false
    • Ex : !(x>1) will return false if x=2
v. Bitwise Operators:

Bitwise Operators are used while performing bit-level operations in C programming. The following are the bitwise operators used:

  • & (Bitwise AND) – The output of the bitwise AND is 1 if the corresponding bits of two operands are 1. If either bit of an operand is 0, the result of the corresponding bit is evaluated to 0.
    • Ex: 12= 00001100 , 25=00011001
    • on performing Bitwise And operation, the resultant output is 00001000 i.e, 8
  • | (Bitwise OR) – The output of bitwise Or is 1 if at least one corresponding bit of the two operands is 1. In C Programming, bitwise OR operator is denoted by |.
    • On performing Bitwise OR operation of 12 and 25, it yields the resultant as 00011101 i.e; 29
  • ^ ( Bitwise XOR) – Also known as Bitwise exclusive OR operator. The result of Bitwise XOR operator is 1 if the corresponding bits of two operands are opposite.
    • On performing Bitwise XOR operation of 12 and 25, it yields the resultant as 00010101 i,e; 21
  • ~ (Bitwise Complement) – It is a unary operator. It changes 1 to 0 and 0 to 1. 
    • Bitwise complement operation of 35 ~(00100011)=11011100 i.e;220
  • >> (Right Shift ) – It shifts all bits towards the right by a certain number of specified bits.
    • Ex: 212 = 11010100 , 212>>2 = 00110101
  • << (Left Shift) – It shifts all bits towards left by a certain number of specified bits. The bit positions that have been vacated by the left shift operator are filled with 0.
    • Ex: 212 = 11010100 , 212<<1 = 10101000
vi. Conditional Operators:

They return one value if the condition is true and returns another value if the condition is false. Also termed as Ternary Operator.

Syntax : (condition ? true_value : false_value);
Ex: (A >100 ? 0 : 1 ); // If A is greater than 100, 0 is returned else 1 is returned.
vii. Increment and Decrement Operators:

C programming has two operators increment (++) and decrement (–) to change the value of an operand (constant or variable) by 1. Increment (++) increases the value by 1 whereas decrement (–) decreases the value by 1. These two operators are unary, meaning they only operate on a single operand.

Example : if a = 10, ++a =11 ,–a=9

viii.Special Operators:
  • & – This is used to get the address of the variable.
    • Example: &a will give the address of a
  • * – This is used as a pointer to a variable.
    • Example: * a here, * is a pointer to a variable a
  • Sizeof() – This gives the size of the variable.
    • Example: sizeof(char) will give us 1.

https://divyakhyani.wordpress.com
Contributor
Do you like Divya Khyani's articles? Follow on social!
Comments to: Variables and Tokens

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

Download VTU Connect

Get it on Google Play

Latest Articles

VTU Updates

VTU Provisional Degree Certificates (PDC) and extract of Grade/ Marks Card will be issued to the students who have completed their degree during August / September 2020 examinations and the following are the prerequisites and conditions for submission of application to Provisional Degree Certificate and Extract of 8th semester grade/marks card. Prerequisites for Applying for […]
VTU in association with IIT Bombay – Spoken Tutorial has organized Student Development Program on Scilab, Drupal & Python. These programs helps Students of B.E., B.Tech, M.Tech, MCAdepartments in their academic activities and also in enriching their technical skills. Students can enroll to more than one course. However the registration fee to be paid for […]

Trending

VTU has released circular specifying the question paper pattern and instructions for August/September 2020 examinations. 1. For Regular Students: Students of Terminal Semesters of U.G and P.G programs have to answer any five full questions, irrespective of modules. 2. For Eligible Arrear Students: The question paper pattern remains unchanged as per respective scheme. 3. All […]

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Categories

Login

Welcome to VTU Connect

Register to VTU Connect now for instant updates on new articles and news
Join VTU Connect

%d bloggers like this: