Variable names in C must start with a letter or an underscore, followed by letters, numbers, or underscores. They cannot start with a number and should not be a reserved word in C.
// Valid variable names int age; float _price; int total1; // Invalid variable names int 1stNumber; float double;
In C, the main data types are used to specify what kind of data a variable can hold. The most common types are: - `int` for integers (whole numbers) - `float` for single-precision floating-point numbers (decimals) - `double` for double-precision floating-point numbers (more precise decimals) - `char` for single characters (like letters)
// Declaring variables with different data types int count = 10; float price = 9.99; double balance = 1000.50; char grade = 'A';
You can set a value to a variable when you declare it, or you can assign it a value later in the code. Initializing a variable means giving it a starting value.
// Initializing a variable at declaration int age = 25; // Assigning a value to a variable later int age; age = 25;
Sometimes you need to convert a variable from one data type to another. This can be done explicitly by using a cast or implicitly by the compiler.
// Explicit conversion (casting) float price = 9.99; int roundedPrice = (int)price; // roundedPrice will be 9 // Implicit conversion int number = 10; float decimal = number; // decimal will be 10.0
In C, you can perform basic arithmetic operations using symbols. These symbols help you add, subtract, multiply, and divide numbers.
// Basic math operations int sum = 5 + 3; // Addition int difference = 5 - 3; // Subtraction int product = 5 * 3; // Multiplication int quotient = 5 / 3; // Division
C provides shorthand operators that combine assignment with a basic arithmetic operation. These operators make code shorter and often clearer.
// Shorthand operators int x = 10; x += 5; // Equivalent to x = x + 5; x *= 2; // Equivalent to x = x * 2;
C allows you to compare two values using comparison operators. These operators return true or false based on whether the comparison is true.
// Comparison operators int a = 5; int b = 10; bool isEqual = (a == b); // false bool isGreater = (a > b); // false bool isLess = (a < b); // true
Logical operators are used to combine multiple conditions. They help in making decisions based on multiple criteria.
// Logical operators bool result1 = (5 > 3) && (8 < 10); // true (both conditions are true) bool result2 = (5 > 3) || (8 > 10); // true (one condition is true) bool result3 = !(5 > 3); // false (negation of the condition)
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!