Variables and constants in C

Variables and Constants in C

📌 Variables

A variable is a container (storage location) used to store data.

  • Each variable has a unique name (identifier)

  • The name represents a memory location

  • The value stored in a variable can change

Example

int playerScore = 95;
  • playerScore → variable name

  • int → data type

  • 95 → stored value

👉 In C, variables must be declared before use.


📌 Rules for Naming Variables

  1. Can contain:

    • Letters (A–Z, a–z)

    • Digits (0–9)

    • Underscore _

  2. First character must be:

    • Letter OR underscore _

    • Starting with _ is discouraged (may conflict with system names)

  3. Length:

    • No strict limit

    • Most compilers check only the first 31 characters

  4. C is strongly typed

    • Variable type cannot be changed after declaration


Constants / Literals in C

A constant is a value that cannot be changed during program execution.

Examples

1 2.5 "C programming is easy"

Constants can also be defined using identifiers:

const double PI = 3.14;

Here, PI always represents 3.14.


📌 Types of Constants in C

1️⃣ Integer constants
2️⃣ Floating-point constants
3️⃣ Character constants
4️⃣ String constants
5️⃣ Enumeration constants


1️⃣ Integer Constants

Integer constants are whole numbers without fractional or exponent parts.

Types

  • Decimal (base 10)0, -9, 22

  • Octal (base 8) → starts with 0021, 077

  • Hexadecimal (base 16) → starts with 0x or 0X0x7f, 0x2a


Special Integer Forms

  • Unsigned integer → append U

    50000U
  • Long integer → append L

    123456789L
  • Unsigned long → append UL

    123456789UL

Rules for Numeric Constants

  • No commas or spaces allowed

  • May be preceded by -

  • Must be within compiler limits


2️⃣ Floating-Point Constants

A floating constant:

  • Contains a decimal OR exponent OR both

  • Always base-10

Examples

0.2 827.602 50000. 2E-8 1.6667E+8

Scientific Notation

1.2 × 10^-31.2E-3

Example representations of 3 × 10⁵:

300000. 3e5 3E5 0.3E6

Precision Types

  • Default → double precision (8 bytes)

  • Single precision → add F

    3E5F
  • Long floating → add L

    0.123E-3L

3️⃣ Character Constants

A character constant:

  • Single character

  • Enclosed in single quotes

Examples

'a' 'F' ' '

Characters use ASCII encoding:

Character    ASCII
'A'    65
'a'    97
'0'    48

Escape Sequences in C

Escape sequences represent special characters.

Sequence    Meaning
\b        Backspace
\f        Form feed
\n        New line
\r        Carriage return
\t            Horizontal tab
\v        Vertical tab
\\        Backslash
\'        Single quote
\"        Double quote
\?        Question mark
\0        Null character

Special Note: \0

  • Represents null character

  • Marks the end of a string

  • Different from '0'


Octal Escape

\ooo (ooo = octal digits)

Example:

'A' = '\101'

Hex Escape

\xhh

Example:

'A' = '\x41'

4️⃣ String Constants

A string constant:

  • Sequence of characters in double quotes

Examples

"good" "" " " "x" "Earth is round\n"

Example Output

"Line 1\nLine 2\nLine 3"

Prints:

Line 1 Line 2 Line 3

Important Notes

  • Compiler automatically adds \0 at end of string

  • 'A'"A"

TypeMeaning
'A'single character
"A"string with 2 chars → 'A' + \0

5️⃣ Enumeration Constants

Defined using keyword enum

Example

enum color {yellow, green, black, white};

Values assigned automatically:

yellow = 0 green = 1 black = 2 white = 3

Custom Values

enum State {Working = 1, Failed = 0, Freezed = 0};

👉 Multiple names can share same value


Mixed Assignment

enum day {sunday = 1, monday, tuesday = 5, wednesday, thursday = 10, friday, saturday};

Output:

1 2 5 6 10 11 12

Rules

  • Must be integer constants

  • Must be unique within scope


Enum vs Macro

Macros:

#define sun 0 #define mon 1

Better with enum:

enum days {sun, mon, tue};

Advantages:

  • Follows scope rules

  • Automatic value assignment


Symbolic Constants (#define)

A symbolic constant is a name representing a fixed value.

Syntax

#define NAME text
  • Written at beginning of program

  • No semicolon at end


Example

#define TAXRATE 0.23 #define PI 3.141593 #define TRUE 1 #define FALSE 0 #define FRIEND "Susan"

Usage:

area = PI * radius * radius;

Compiler replaces it with:

area = 3.141593 * radius * radius;

Advantages of Symbolic Constants

  • Improves readability

  • Easy to modify values

  • Helps maintain large programs

  • Defined using C preprocessor




Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays