C Programming: Tokens, Operators, and Logic

Classified in Computers

Written at on English with a size of 2.55 KB.

Tokens

In programming, a token is the smallest meaningful element in code. They are the building blocks of a language's syntax. Common token types include:

  • Keywords: Reserved words like if, else, while, and int (for declaring integers).
  • Identifiers: Names given to elements like variables (e.g., sum), functions, and arrays.
  • Constants: Unchanging values during program execution (e.g., 3.14 for pi).
  • Operators: Symbols for mathematical or logical operations (e.g., + for addition).
  • Separators: Punctuation like commas (,), semicolons (;), and braces ({}).

Example: int sum = 10 + 5;

In this line, int is a keyword, sum is an identifier, = is an operator, 10 and 5 are constants, and ; is a separator.

Arithmetic Operators

C has nine arithmetic operators for basic mathematical operations:

  • +: Addition (z = x + y;)
  • -: Subtraction (z = x - y;)
  • *: Multiplication (z = x * y;)
  • /: Division (z = x / y;)
  • %: Modulus (z = x % y;)
  • ++: Increment
  • --: Decrement

Relational Operators

Relational operators compare two values, resulting in a Boolean (true/false) outcome. They are used in conditional statements and loops.

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Example:

int x = 7, y = 3, z;z = x > y;printf("%d is greater than %d is %d\n", x, y, z);z = x == y;printf("%d is equal to %d is %d\n", x, y, z);z = x != y;printf("%d is not equal to %d is %d\n", x, y, z);

Logical Operators

Logical operators combine multiple conditions. They return 1 (true) or 0 (false).

  • &&: Logical AND (true if both statements are true)
  • ||: Logical OR (true if at least one statement is true)
  • !: Logical NOT (true if the operand is false)

Entradas relacionadas: