SB

C Programming Language Notes

Character Set

Definition: A character set is a collection of characters used for constructing words, statements, and symbols in a programming language. It defines the characters that can be utilized within the programming environment.

C Language Character Set: C supports a total of 256 characters, which are categorized into the following:

  1. Alphabets

    • Contains all English alphabets (A-Z, a-z).

    • Total: 52 characters (26 uppercase and 26 lowercase) which can be used in variable names, function names, and keywords.

  2. Digits

    • C supports digits from 0 to 9, essential for numerical operations and constants.

    • Total: 10 characters.

  3. Special Symbols

    • A variety of symbols available for various operations and syntax, including but not limited to: ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | (and others). These symbols play an important role in defining operations, grouping expressions, and writing control structures.

Tokens

Definition: In C, a token is defined as the smallest individual unit in a program. Tokens are the fundamental building blocks of programs and can be categorized as follows:

  1. Keywords: Reserved words with special meaning (e.g., int, return, for) that cannot be used as identifiers.

  2. Identifiers: Names defined by the user for variables, functions, and arrays that allow for custom naming conventions.

  3. Operators: Symbols that specify operations to be performed (e.g., arithmetic operations, logical comparisons).

  4. Special Symbols: These symbols are part of the character set and serve specific functions in syntax.

  5. Constants: Fixed values that do not change during execution (e.g., numerical values like 10 or 3.14).

  6. Strings: Collections of characters enclosed within double quotes (e.g., "Hello World"), used for representing text.

  7. Data Values: Values assigned to variables, which are dynamic in nature.

Keywords

Definition: Specific reserved words that represent features and structure of the C language and cannot be repurposed as identifiers.

Characteristics:

  • Always written in lowercase.

  • Total of 32 keywords in C including: auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while.

Identifiers

Definition: A sequence of characters that serve as names for variables, functions, arrays, etc., allowing programmers to write readable and understandable code.

Rules for Creating Identifiers:

  1. Contains only letters, digits, or underscores.

  2. Must not start with a digit.

  3. No special symbols or whitespace allowed, promoting a standard form for naming.

  4. Cannot be a keyword, as keywords serve specific syntax roles.

  5. No fixed length, though compilers generally consider the first 31 characters for certain standards.

  6. Must be unique within its specific scope, avoiding clashes and confusion within the program.

Data Types in C
  1. Primary Data Types (Predefined):

    • char: Stores a single character; typically uses 1 byte, which is sufficient for ASCII characters.

    • int: Stores an integer, commonly 2 bytes depending on the platform.

    • float: Stores single-precision floating-point numbers; usually takes up 4 bytes, suitable for most decimal values.

    • double: Stores double-precision floating-point numbers; approximately 8 bytes, offering more precision for large numbers.

Data Type

Memory (bytes)

Format Specifier

int

2

%d

char

1

%c

float

4

%f

double

8

%lf

Note: Size may vary depending on the machine architecture.

  1. Data Type Qualifiers:

    • Enhance basic data types using keywords like short, long, signed, and unsigned.

    • Examples: short int stores smaller integer values while unsigned int cannot represent negative numbers.

  2. Void Data Type:

    • Represents no value; primarily used for functions that do not return a value to the caller, highlighting the nature of certain functions.

Variables

Definition: An identifier representing a specific type of information that can be changed or manipulated during program execution.

Declaration: All variables must be declared before use to ensure proper data type management and memory allocation.
Example:

int a, b, c;
float d;
char e;
Escape Sequences

Represent special non-printing characters in C, typically used when formatting text output or including special symbols in strings. Escape sequences begin with a '\'. Examples include:

  • \a: Alarm/Beep

  • \b: Backspace

  • \n: New Line

  • \t: Horizontal Tab

  • \\: Backslash

  • \': Single Quote

  • ": Double Quote

Constants

Definition: Fixed values that cannot be changed during program execution, crucial for preventing inadvertent modifications of core values.

Types:

  1. Integer Constants: e.g., 10, 20

  2. Real Constants: e.g., 10.5, 12.3

  3. Octal Constants: e.g., 021, 045

  4. Hexadecimal Constants: e.g., 0x2a, 0x7b

  5. Character Constants: e.g., 'a', 'b'

  6. String Constants: e.g., "Hello"

  7. Symbolic Constants: Created using the #define directive for improved code readability.
    Example: #define PI 3.14

Console I/O Operations
  • printf() Function: Used for formatted output in C programming, allowing for the flexible display of data.
    Syntax:

printf("format string", argument_list);
  • scanf() Function: Used for reading input values in C, enabling user-driven data acquisition.
    Syntax:

scanf("format string", argument_list);
Operators

Types of Operators in C:

  1. Arithmetic Operators: e.g., +, -, *, / (perform mathematical operations).

  2. Relational Operators: e.g., ==, !=, <, > (compare values).

  3. Logical Operators: e.g., &&, ||, ! (used for logical operations).

  4. Assignment Operators: e.g., =, +=, -= (assign values to variables).

  5. Bitwise Operators: e.g., &, |, ^, <<, >> (manipulate data at the bit level).

Control Flow Statements
  • if Statement: Executes a block of code based on a specified condition, allowing for decision-making in programs.
    Syntax:

if (condition) {
    // code to be executed
} else {
    // code to be executed
}
  • switch Statement: Provides multi-way branching based on the value of a variable, acting similarly to multiple if statements but often more readable.
    Syntax:

switch(expression) {
    case constant1:
        // code to execute for constant1
        break;
    default:
        // code if no case is matched
}
Looping Structures
  • while Loop: Continues executing as long as the condition evaluates to true.
    Example:

while(condition) {
    // loop body
}
  • for Loop: Used for iterating over a sequence of values; well-suited for counting iterations and fixing initial variables.
    Example:

for(initialization; condition; increment/decrement) {
    // loop body
}
  • do-while Loop: Similar to a while loop, but ensures that the loop body executes at least once.
    Example:

do {
    // loop body
} while(condition);

These notes provide a comprehensive overview of the character set, keywords, data types, constants, input/output operations, and control structures in the C programming language, serving as a foundational reference for learning and using C effectively.