Unit 1 - Notes

CSE101

Unit 1: Basics and introduction to C

1. The C Character Set

The C character set denotes the set of characters used to write a source program. C uses the standard ASCII character set.

Categories of Characters

  1. Alphabets:
    • Uppercase: A to Z
    • Lowercase: a to z
  2. Digits: 0 to 9
  3. Special Characters: Symbols that have special meaning.
    • Examples: , . ; : ? ' " ! | / \ ~ + - * % & ^ = ( ) { } [ ] < > _ # $
  4. White Spaces: Characters used to separate words/items but do not cause a printable action on the screen.
    • Blank space
    • Horizontal tab (\t)
    • Carriage return (\r)
    • New line (\n)
    • Form feed

2. Identifiers and Keywords

Keywords

Keywords are reserved words that have standard, predefined meanings in the C language. They cannot be used as programmer-defined names. C (C89 standard) supports 32 keywords:

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

Identifiers

Identifiers are names given to C entities such as variables, functions, structures, etc.

Rules for Naming Identifiers:

  1. Must begin with a letter (A-Z or a-z) or an underscore (_).
  2. Subsequent characters can be letters, digits, or underscores.
  3. Case Sensitive: Sum and sum are different.
  4. No special characters allowed (except underscore).
  5. Keywords cannot be used as identifiers.
  6. Must not contain whitespace.

3. Data Types

Data types specify the type of data a variable can hold and the amount of memory allocated.

A. Primary (Fundamental) Data Types

  1. int (Integer):
    • Stores whole numbers.
    • Size: Typically 2 or 4 bytes (machine-dependent).
    • Format Specifier: %d or %i
  2. char (Character):
    • Stores a single ASCII character.
    • Size: 1 byte.
    • Format Specifier: %c
  3. float (Floating Point):
    • Stores single-precision floating-point numbers (decimals).
    • Size: 4 bytes (6 decimal places precision).
    • Format Specifier: %f
  4. double (Double Precision):
    • Stores double-precision floating-point numbers.
    • Size: 8 bytes (15 decimal places precision).
    • Format Specifier: %lf
  5. void:
    • Represents the absence of value. Used in functions returning nothing.

B. Data Type Modifiers

Used to alter the length or range of primary data types.

  • signed (default for int/char)
  • unsigned (only positive numbers)
  • short (reduces storage size)
  • long (increases storage size)

4. Constants and Variables

Variables

A variable is a named memory location used to store data that may change during program execution.

Syntax:

C
data_type variable_name;            // Declaration
data_type variable_name = value;    // Initialization

Example:
C
int age = 21;
float salary = 5000.50;
char grade = 'A';

Constants (Literals)

Fixed values that do not change during execution.

  1. Integer Constants: Whole numbers (e.g., 10, -5, 0).
    • Can be Decimal, Octal (starts with 0), or Hexadecimal (starts with 0x).
  2. Real (Floating) Constants: Numbers with fractions (e.g., 3.14, -0.005).
  3. Character Constants: Single character in single quotes (e.g., 'a', '5', '+').
  4. String Constants: Sequence of characters in double quotes (e.g., "Hello", "123"). ending with a null character \0.

Defining Constants:

  • Using const keyword: const float PI = 3.14;
  • Using #define preprocessor: #define PI 3.14

5. Expressions

An expression is a combination of operators, constants, and variables arranged according to the rules of C language that computes a value.

  • l-value: Refers to a memory location (appears on the left side of assignment).
  • r-value: Refers to the data value stored at some address (appears on the right side).

Example:

C
int a = 10, b = 5, c;
c = a + b; // 'a + b' is an expression evaluating to 15


6. Operators in C

Operators are symbols used to perform operations on operands (variables and constants).

A. Arithmetic Operators

Used for mathematical calculations.

Operator Meaning Example (a=10, b=3)
+ Addition a + b13
- Subtraction a - b7
* Multiplication a * b30
/ Division a / b3 (Integer division truncates decimal)
% Modulus (Remainder) a % b1

Note: Modulus operator % works only with integers.

B. Unary Operators

Operate on a single operand.

  1. Unary Minus (-): Negates value.
  2. Increment (++): Increases value by 1.
  3. Decrement (--): Decreases value by 1.

Prefix vs Postfix:

  • Prefix (++a): Increment first, then use value.
  • Postfix (a++): Use value first, then increment.

C
int x = 5, y;
y = ++x; // x becomes 6, y becomes 6
y = x++; // y becomes 6, x becomes 7

C. Relational Operators

Used to compare two values. Result is always 0 (False) or 1 (True).

Operator Meaning Example (a=5, b=9)
== Equal to a == b0
!= Not equal to a != b1
> Greater than a > b0
< Less than a < b1
>= Greater than or equal to a >= b0
<= Less than or equal to a <= b1

D. Logical Operators

Used to combine multiple relational expressions.

Operator Name Description Example
&& Logical AND True if both operands are non-zero. (5>3) && (5<10)1
\|\| Logical OR True if at least one operand is non-zero. (5>3) \|\| (5>10)1
! Logical NOT Reverses the state of the operand. !(5>3)0

Note: C uses "Short Circuit Evaluation." In A && B, if A is false, B is not evaluated.

E. Assignment Operators

Used to assign the result of an expression to a variable.

  1. Simple Assignment (=): a = 10
  2. Compound Assignment (Shorthand):
    • += : a += b implies a = a + b
    • -= : a -= b implies a = a - b
    • *= : a *= b implies a = a * b
    • /= : a /= b implies a = a / b
    • %= : a %= b implies a = a % b

F. Conditional (Ternary) Operator

The only operator taking three operands. It is a shorthand for if-else.

Syntax:
Condition ? Expression1 : Expression2;

  • If Condition is true, execute Expression1.
  • If Condition is false, execute Expression2.

Example:

C
int a = 10, b = 20, max;
max = (a > b) ? a : b; // max will be 20

G. Bitwise Operators

Perform operations at the bit level (binary representation).

Given A = 60 (0011 1100) and B = 13 (0000 1101):

Operator Name Description Example
& Binary AND Copies bit if it exists in both operands. A & B = 12 (0000 1100)
\| Binary OR Copies bit if it exists in either operand. A \| B = 61 (0011 1101)
^ Binary XOR Copies bit if it is set in one operand but not both. A ^ B = 49 (0011 0001)
~ Binary One's Complement Inverts all bits (unary). ~A = -61 (in 2's complement)
<< Left Shift Shifts bits left (multiplies by 2n). A << 2 = 240 (1111 0000)
>> Right Shift Shifts bits right (divides by 2n). A >> 2 = 15 (0000 1111)

H. Special Operators

  1. Comma Operator (,): Used to link related expressions together. Evaluated left to right; the value of the rightmost expression is returned.
    • x = (a=3, b=5, a+b); // x becomes 8
  2. sizeof Operator: Unary operator that returns the size of data (constant, variable, or type) in bytes.
    • sizeof(int) returns 4 (on 32-bit system).

7. Operator Precedence and Associativity

When multiple operators appear in an expression, precedence determines which operator is performed first.

Hierarchy (Highest to Lowest):

  1. Postfix () [] -> . ++ --
  2. Unary + - ! ~ ++ -- (type)* & sizeof (Right to Left Associativity)
  3. Multiplicative * / %
  4. Additive + -
  5. Shift << >>
  6. Relational < <= > >=
  7. Equality == !=
  8. Bitwise AND &
  9. Bitwise XOR ^
  10. Bitwise OR |
  11. Logical AND &&
  12. Logical OR ||
  13. Conditional ?: (Right to Left Associativity)
  14. Assignment = += -= etc. (Right to Left Associativity)
  15. Comma ,