Skip to content

Basic Syntax of C Programming

C shares similarities with other programming languages like Python, Java and C++, but it also introduces some distinct differences that set it apart. It known for its simplicity, efficiency, and closeness to hardware. Let’s dive into writing and executing our first C program, and explore the different modes in which C can be executed.


  • Named memory location used to store a value that can be modified during program execution.
  • It is identified by names, used to hold data.
  • It acts as a container for data, and each variable is associated with a specific data type that determines the size and layout of its memory.
  • Must begin with a letter (A-Z or a-z) or an underscore (_). Cannot start with a digit.
  • The rest of the name may consist of letters, digits, or underscores
  • Cannot use C keywords (like int, main, return).
  • Case sensitive (Age and age are different).
#include <stdio.h>
int main() {
int age = 20; // Integer variable
float height = 5.9; // Floating-point variable
char grade = 'A+'; // Character variable
// Printing the values
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
return 0;
}

Data types tell the compiler what kind of data a variable can store. C is a strongly typed language, so each variable must be declared with a type.

  • int : Stores integers like -1, 0, 42.
  • float: Floating point type, e.g., float salary = 75000.50.
  • char: Character type, e.g., char grade = ‘A’.
  • double: Double precision floating point type, e.g., double pi = 3.14159.
  • void: Represents absence of data type.
#include <stdio.h>
int main() {
int number = 100;
float percentage = 89.75;
char grade = 'A+';
const float PI = 3.14;
printf("Number: %d\n", number);
printf("Percentage: %.2f\n", percentage);
printf("Grade: %c\n", grade);
printf("Value of PI: %.2f\n", PI);
return 0;
}

A constant is similar to a variable, but its value cannot be changed after it is defined. Constants are used when you know the value will remain the same throughout the program, such as the value of pi or the number of days in a week.

  • Using the const keyword.
  • Using the #define preprocessor directive.
#include <stdio.h>
#define PI 3.14159 // Constant using #define
int main() {
const int rollNumber = 1; // Integer constant using const
const float temperature = 36.6; // Floating-point constant using const
const char grade = 'A+'; // Character constant using const
const char name[] = "Akash"; // String constant using const
printf("Student Name: %s\n", name);
printf("Roll Number: %d\n", rollNumber);
printf("Grade: %c\n", grade);
printf("Body Temperature: %.1f\n", temperature);
printf("Value of PI: %.5f\n", PI);
return 0;
}