C Programming: Identifiers and Operators - A Comprehensive Guide, Lecture notes of Computer Science

This document offers a thorough explanation of c identifiers and operators, covering their rules, types, and precedence. it includes detailed examples of arithmetic, relational, logical, bitwise, and other operators, making it a valuable resource for understanding fundamental c programming concepts. The guide also clarifies operator precedence and associativity, crucial for writing correct and efficient c code. each operator is explained with clear examples and syntax.

Typology: Lecture notes

2024/2025

Available from 04/21/2025

johnson-prince
johnson-prince 🇺🇸

9 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C IDENTIFIERS AND OPERATORS
C 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 wi th 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 constructing C identifiers
o 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.
o It should not begin with any numerical digit.
o In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers
are case sensitive.
o Commas or blank spaces cannot be specified within an identifier.
o Keywords cannot be represented as an identifier.
o The length of the identifiers should not be more than 31 characters.
o Identifiers should be written in such a way that it is meaningful, s hort, and easy to read.
Example of valid identifiers
1. total, sum, average, _m _, sum_1, etc.
Example of invalid identifiers
1. 2sum (starts with a numerical digit)
2. int (reserved word)
3. char (reserved word)
4. m+n (s pecial character, i.e., '+')
Types of identifiers
o Internal identifier
o 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.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C Programming: Identifiers and Operators - A Comprehensive Guide and more Lecture notes Computer Science in PDF only on Docsity!

C IDENTIFIERS AND OPERATORS

C 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 constructing C identifiers

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

Example of valid identifiers

  1. total, sum, average, _m _, sum_1, etc.

Example of invalid identifiers

  1. 2sum (starts with a numerical digit)
  2. int (reserved word)
  3. char (reserved word)
  4. m+n (special character, i.e., '+')

Types of identifiers

o Internal identifier o 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.

Differences between Keyword and Identifier

Keyword Identifier Keyword is a pre-defined word. The identifier is a user-defined word It must be written in a lowercase letter. It can be written in both lowercase and uppercase letters. Its meaning is pre-defined in the c compiler. Its meaning is not defined in the c compiler. It is a combination of alphabetical characters. It is a combination of alphanumeric characters. It does not contain the underscore character. It can contain the underscore character.

Let's understand through an example.

  1. int main()
  2. {
  3. int a=10;
  4. int A=20;
  5. printf("Value of a is : %d",a);
  6. printf("\nValue of A is :%d",A);
  7. return 0;
  8. }

Output

Value of a is : 10 Value of A is : The above output shows that the values of both the variables, 'a' and 'A' are different. Therefore, we conclude that the identifiers are case sensitive.

C Operators

An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise, etc. There are following types of operators to perform different types of operations in C language. o Arithmetic Operators o Relational Operators o Shift Operators o Logical Operators o Bitwise Operators o Ternary or Conditional Operators o Assignment Operator o Misc Operator

Example:

  1. int a = 5;
  2. int b = 3;
  3. int result = a + b; Output: result = 8 Subtraction Operator (-): The second operand is subtracted from the first operand via the subtraction operator.

Syntax:

It has the following syntax:

  1. result = operand1 - operand2; Example:
  2. int a = 8;
  3. int b = 3;
  4. int result = a - b;

Output:

result = 5 Multiplication Operator ():* This operator is used to multiply the two operands.

Syntax:

It has the following syntax:

  1. result = operand1 * operand2; Example:
  2. int a = 4;
  3. int b = 5;
  4. int result = a * b;

Output:

result = 20 Division Operator (/): The first operand and the second operand are divided using the division operator.

Syntax:

It has the following syntax:

  1. result = operand1 / operand2; Example:
  2. int a = 10;
  3. int b = 2;
  4. int result = a / b;

Output: result = 5

Modulus Operator (%): The modulus operator determines the remainder of the division between two operands.

Syntax:

It has the following syntax:

  1. result = operand1 % operand2; Example:
  2. int a = 10;
  3. int b = 3;
  4. int result = a % b;

Output:

result = 1

Relational Operators:

Relational operators assess the relationship between values by comparing them. They return either true (1) or false (0). The relational operators in C are as follows: Equality Operator (==): If two operands are equal, the equality operator verifies this.

Syntax:

It has the following syntax:

  1. result = operand1 == operand2; Example:
  2. int a = 5;
  3. int b = 5;
  4. int result = a == b;

Output:

result=1 (true) Inequality Operator (!=): The inequality operator determines whether two operands are equal or not. Syntax: It has the following syntax:

  1. result = operand1 != operand2; Example:
    1. int a = 5;
    2. int b = 3;
    3. int result = a != b; Output: result=1 (true) Greater than Operator (>): The greater than operator determines if the first operand exceeds the second operand.

Example:

  1. int a = 3;
  2. int b = 6;
  3. int result = a <= b; Output: result=1 (true)

Shift Operators:

A binary number's bits can be moved to the left or right using shift operators. The C shift workers are listed below: Left Shift Operator (<<): The left shift operator moves the bits of the first operand to the left by the number of places indicated by the second argument. Syntax: It has the following syntax:

  1. result = operand1 << operand2; Example:
  2. unsigned int a = 5; // 0000 0101 in binary
  3. int result = a << 2; Output: result = 20 // 0001 0100 in binary Right Shift Operator (>>): The right shift operator shifts the bits of the first operand to the right by the number of positions specified by the second operand. Syntax: It has the following syntax:
  4. result = operand1 >> operand2; Example:
  5. unsigned int a = 20; // 0001 0100 in binary
  6. int result = a >> 2; Output: result = 5 // 0000 0101 in binary

Logical Operators:

Logical operators perform logical operations on boolean values and return either true (1) or false (0). Here are the logical operators in C: Logical AND Operator (&&): The logical AND operator returns true if both operands are true. Syntax: It has the following syntax:

  1. result = operand1 && operand2;

Example:

  1. int a = 5;
  2. int b = 3;
  3. int result = (a > 3) && (b < 5); Output: result = 1 (true) Logical OR Operator (||): The logical OR operator returns true if at least one of the operands is true. Syntax: It has the following syntax:
  4. result = operand1 || operand2; Example:
  5. int a = 5;
  6. int b = 3;
  7. int result = (a > 3) || (b > 5); Output: result = 1 (true) Logical NOT Operator (!): The logical NOT operator negates the value of the operand. Syntax: It has the following syntax:
  8. result = !operand; Example:
  9. int a = 5;
  10. int result = !(a > 3); Output: result = 0 (false)

Bitwise Operators:

Bitwise operators perform operations on individual bits of the operands. Here are the bitwise operators in C: Bitwise AND Operator (&): The bitwise AND operator performs a bitwise AND operation on the corresponding bits of the operands. Syntax: It has the following syntax:

  1. result = operand1 & operand2;

Ternary or Conditional Operator: The ternary or conditional operator allows you to assign a value based on a condition. Syntax: It has the following syntax:

  1. result = condition? value1 : value2; Example:
  2. int a = 5;
  3. int b = 3;
  4. int result = (a > b)? a : b; Output: result = 5

Assignment Operator:

Assignment operators are used to assign values to variables. Here is some of the assignment operator in C: Simple Assignment Operator (=): The simple assignment operator assigns the value from the right side operands to the left side operands. Syntax: It has the following syntax:

  1. variable = value; Example:
  2. int a;
  3. a = 5; Output: No output. The value 5 is assigned to variable 'a'.

Miscellaneous Operator:

The sizeof operator and the comma operator fall under the miscellaneous operator category. size of Operator: The sizeof operator returns the size, in bytes , of a variable or a data type. Syntax: It has the following syntax:

  1. result = sizeof (variable / data type); Example:
  2. int a;
  3. int size = sizeof (a); Output: size = 4 // Assuming int occupies 4 bytes Comma Operator (,): The comma operator evaluates multiple expressions and returns the value of the last expression.

Syntax: It has the following syntax:

  1. makefileCopy code
  2. result = (expression1, expression2,..., expressionN); Example:
  3. int a = 5, b = 3;
  4. int result = (a += 2, b *= 2, a + b); Output: result = 15 // a = 7, b = 6, a + b = 13

Uses of Operators:

The following are some common uses for the various kinds of operators in C: o Calculations in fundamental mathematics are performed using the addition and subtraction operators (+ and - ). o If the user wants to do some multiplication and division operations, utilize the multiplication and division operators _( and /)_*. o The remainder of a division operation is obtained using the modulus operator (%). o Equality and inequality operators (== and!=) are needed to compare values and determine whether they are equal or not. o Use the greater than and less than operators (>and <) to compare values and determine if one value is larger than or less than o A value's relationship to another value may be determined using the larger than or equal to and less than or equal to operators (>= and <=). o A binary number's bits are shifted to the left using the left shift operator (<<). o A binary number's bits can be shifted to the right using the right shift operator (>>). o Use the logical AND operator (&&) to combine many criteria and determine if each condition is true. o When combining several criteria, the logical OR operator (||) is used to determine if at least one of the conditions is true. o The logical NOT operator (!) is used to negate a condition's value. o When two numbers' individual bits are involved, the bitwise AND operator (&) is utilized to accomplish the action. o The bitwise OR operator (|) is employed when two numbers' individual bits are involved. o Bitwise exclusive OR operator is performed on individual bits of two integers using the bitwise XOR operator (). o Use the bitwise NOT operator () to flip or invert the bits of an integer. o Use the ternary operator (?:) to assign a value depending on a condition in a compact form. o A value is assigned to a variable using the simple assignment operator (=). o The sizeof operator is used to calculate a variable's or data type's size in bytes. o When evaluating several expressions, the comma operator (,) returns the result of the last expression that was evaluated.