Basic Syntax of C Programming
Basic Syntax
Section titled “Basic Syntax”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.
Variables
Section titled “Variables”- 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.
Rules for Declaring Variables:
Section titled “Rules for Declaring Variables:”- 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
, orunderscores
- Cannot use C keywords (like int, main, return).
- Case sensitive (Age and age are different).
Syntax For Creating Variables
Section titled “Syntax For Creating Variables”#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;}
Age: 20Height: 5.9Grade: A+
Data Types in C
Section titled “Data Types in C”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.
Fundamental Data Types:
Section titled “Fundamental Data Types:”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.
Example Of Initialize Data Types:
Section titled “Example Of Initialize Data Types:”#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;}
Number: 100Percentage: 89.75Grade: A+Value of PI: 3.14
Constants in C
Section titled “Constants in C”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.
Two common ways to define constants in C:
Section titled “Two common ways to define constants in C:”- 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;}
Student Name: AkashRoll Number: 1Grade: A+Body Temperature: 36.6Value of PI: 3.14159