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:
- Start with a Letter or Underscore: The first character must be a letter (uppercase or lowercase) or an underscore (_).
- Followed by Letters, Numbers, or Underscores: Subsequent characters can be letters, numbers, or underscores.
- Case Sensitivity: C is case-sensitive, so variable, Variable, and VARIABLE are different identifiers.
- No Reserved Keywords: Keywords such as int, return, while, etc., cannot be used as variable names.
Data Types and Sizes
Basic Data Types:
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.
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.
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
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!”).
#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
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
Variable Declarations:
- Introduce a variable by specifying its type and name.
- int age;
float height;
char initial;
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’;
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;
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};
Pointer Declarations:
- Introduce a pointer by specifying its type and name.
- int *ptr;
char *str;
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);