Variable Names

  • Naming Conventions: Variable names should be descriptive and typically use camelCase or snake_case. They must begin with a letter (a-z, A-Z) or an underscore (_) and can be followed by letters, digits (0-9), or underscores.

Rules for Naming Variables:

  1. Start with a Letter or Underscore: The first character must be a letter (uppercase or lowercase) or an underscore (_).
  2. Followed by Letters, Numbers, or Underscores: Subsequent characters can be letters, numbers, or underscores.
  3. Case Sensitivity: C is case-sensitive, so variable, Variable, and VARIABLE are different identifiers.
  4. No Reserved Keywords: Keywords such as int, return, while, etc., cannot be used as variable names.

 

Data Types and Sizes 

 

Basic Data Types:
  1. Integer Types:

    • char: Typically 1 byte. Used for characters. Range: -128 to 127 (signed) or 0 to 255 (unsigned).
    • int: Typically 4 bytes. Used for integers. Range: -2147483648 to 2147483647 (signed) or 0 to 4294967295 (unsigned).
    • short: Typically 2 bytes. Used for smaller integers. Range: -32768 to 32767 (signed) or 0 to 65535 (unsigned).
    • long: Typically 4 bytes or 8 bytes (platform-dependent). Range: -2147483648 to 2147483647 or larger.
  2. Floating-Point Types:

    • float: Typically 4 bytes. Used for single-precision floating-point numbers. Range: Approximately 3.4E-38 to 3.4E+38.
    • double: Typically 8 bytes. Used for double-precision floating-point numbers. Range: Approximately 1.7E-308 to 1.7E+308.
    • long double: Typically 8, 12, or 16 bytes (platform-dependent). Used for extended-precision floating-point numbers.
  3. Void Type:

    • void: Used to specify that no value is available. Commonly used for functions that do not return a value.

 

Sizes of Data Types:

 

The size of data types can vary depending on the platform and compiler, but typical sizes on a 32-bit or 64-bit system are:


 

Constants

 

Definition :

Constants in C are fixed values that cannot be altered during the execution of a program. They are used to represent data that does not change.

 

Types of Constants

  1. Literal Constants:

    • Integer Literals: Numeric values without a decimal point (e.g., 42, -15).
    • Floating-point Literals: Numeric values with a decimal point (e.g., 3.14, -0.001).
    • Character Literals: Single characters enclosed in single quotes (e.g., ‘A’, ‘0’).
    • String Literals: Sequences of characters enclosed in double quotes (e.g., “Hello, World!”).
  2. #define Constants:

    • These are constants defined by the preprocessor using the #define directive. They do not occupy memory and are replaced by their value during preprocessing.
    • #define PI 3.14
      #define MAX_BUFFER_SIZE 1024
  3. const Keyword:

    • Variables declared with the const keyword cannot be modified after initialization. They occupy memory and have a specific type.
    • const int DAYS_IN_WEEK = 7;
      const float GRAVITY = 9.81;

 

Declarations 

 

Definition :

Declarations in C are statements that introduce identifiers such as variables, arrays, pointers, or functions to the program, specifying their type.

 

Types of Declarations

  1. Variable Declarations:

    • Introduce a variable by specifying its type and name.
    • int age;
      float height;
      char initial;
  2. Variable Declarations with Initialization:

    • Declare a variable and initialize it with a value at the same time.
    • int age = 25;
      float height = 5.9;
      char initial = ‘A’;
  3. Multiple Declarations:

    • Declare multiple variables of the same type in a single statement.
    • int a, b, c;
      float x = 1.0, y = 2.0, z = 3.0;
  4. Array Declarations:

    • Introduce an array by specifying its type, name, and size.
    • int numbers[10];
      float values[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
  5. Pointer Declarations:

    • Introduce a pointer by specifying its type and name.
    • int *ptr;
      char *str;
  6. Function Declarations:

    • Introduce a function by specifying its return type, name, and parameters.
    • void printMessage();
      int add(int a, int b);
      float calculateArea(float radius);