Syntax and Constants
Introduction to C Programming- C Programming Series - Article 1

π Hey, I'm Shreya, a Senior Software Engineer in an Leading MNC. Join me on this blog as I share insights, tips, and experiences. Let's explore these fascinating worlds together! ππ» #CodeAndSecure
Learning a programming language is surprisingly similar to learning a spoken language like English. If you can understand the structure of a sentence, you can understand the structure of code. In this article, we will break down the fundamental building blocks of C programming, explore data types, and dive into the rules of assignment operators.
Part 1: C Language vs. English Language
To understand C, let's compare it to something we already know: English. Just as English relies on alphabets, words, and grammar, C relies on keywords, operators, and syntax.
| Feature | English Language | C Language |
| Basic Unit | Alphabets (26 letters) | Keywords (32 reserved words like int, float, char) |
| Connectors | Words (> 10,000) | Operators (45 symbols like +, *, -) |
| Structure | Sentences | Separators (14 types like ;, " ", space) |
| Rules | Grammar | Syntax |
The Importance of Syntax
In English, grammar defines the rules and regulations for constructing sentences. In C, this is called Syntax.
English: If we violate grammar, the sentence sounds wrong.
C Programming: If we violate Syntax rules, we get an Error.
Part 2: Constants and Data Types
In C, we deal with various types of data, known as Constants. These are broadly classified into three main categories based on the data they hold.
1. Integer Constants (int)
These represent whole numbers. They can be:
Positive (
+ve)Negative (
-ve)Zero (
0)Example:
100
2. Real Constants (float)
These represent numbers with decimal points.
- Examples:
5.0,7.2,-5.25,.3(which is read as 0.3).
3. Character Constants (char)
These represent a single character.
The Golden Rule: A character constant must be enclosed in single quotes (
' ') and must be exactly one unit in length.Examples:
'a','5','?','*'
Data Types and Format Specifiers
In C, these constants correspond to "Basic," "Primitive," or "Fundamental" data types. When printing or scanning these types, we use specific format specifiers:
| Data Type | Description | Format Specifier |
int | Decimal Integer | %d or %i |
float | Floating point | %f |
char | Character | %c |
Common Misconceptions: Valid vs. Invalid Data
Based on standard C rules, let's look at what is valid and what causes an error:
100β Valid (int)6.2β Valid (float)-8.β Valid (float, treated as -8.0)'m'β Valid (char)'3184'β Invalid (Content length is 4;charonly allows length 1).abcβ Invalid (Missing quotes; treated as a variable).salaryβ Invalid (Treated as a variable name).
Part 3: The Assignment Operator
In mathematics, the = symbol means "is equal to." However, in C programming, = is the Assignment Operator.
The Anatomy of an Assignment
An assignment statement generally follows this structure:
Variable = Value (or) Expression
The 3 Rules of Assignment:
Arguments: It requires 2 arguments (one on the left, one on the right).
LHS (Left Hand Side): Must be a Variable. This is often called the L-value.
RHS (Right Hand Side): Can be a variable, a constant, or an expression.
Analyzing Assignment Errors - A Small Exercise !!
Every statement in C must end with a semicolon separator (;). Let's debug some example statements:
a = 5Status: Invalid.
Reason: Missing the semicolon.
a = 5;Status: Valid.
Note: LHS (
a) is a variable, RHS (5) is a constant.
a = ;Status: Invalid.
Reason: Expression Syntax Error. Both arguments must be present.
= 10;Status: Invalid.
Reason: Missing LHS.
10 = 20;Status: Invalid.
Reason: L-value Error. The LHS is
10(a constant), but the LHS must be a variable.
π Navigate the Series
Previous Article: https://shreyashree.hashnode.dev/c-programming-series-guide
Next Article: (Link to be added once published)
π Whole Series Link: https://shreyashree.hashnode.dev/series/c-course


