C Cheat Sheet


  // Basic Syntax in C
  
  #include <stdio.h>  // Standard input-output header
  
  int main() {
      // Variable declaration
      int age = 25;
      float height = 5.9;
      char grade = 'A';
  
      // Printing variables using printf
      printf("Age: %d\n", age);      // %d is the format specifier for integers
      printf("Height: %.1f\n", height); // %.1f is the format specifier for float with one decimal
      printf("Grade: %c\n", grade);  // %c is the format specifier for characters
  
      // Taking user input
      int userAge;
      printf("Enter your age: ");
      scanf("%d", &userAge);  // %d is the format specifier for integers, &userAge stores input
      printf("Your age is: %d\n", userAge);
  
      // Conditional statement
      if (userAge >= 18) {
          printf("You are an adult.\n");
      } else {
          printf("You are a minor.\n");
      }
  
      // Loop example (for loop)
      printf("Counting from 1 to 5:\n");
      for (int i = 1; i <= 5; i++) {
          printf("%d\n", i);  // Prints numbers from 1 to 5
      }
  
      // Returning from main function
      return 0; // Indicates successful execution
  }
  
© 2024 CheatsheetCoder, All Rights Reserved.