Skip to main content

Command Palette

Search for a command to run...

Syntax and Constants

Introduction to C Programming- C Programming Series - Article 1

Updated
β€’4 min read
Syntax and Constants
S

πŸ‘‹ 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.

FeatureEnglish LanguageC Language
Basic UnitAlphabets (26 letters)Keywords (32 reserved words like int, float, char)
ConnectorsWords (> 10,000)Operators (45 symbols like +, *, -)
StructureSentencesSeparators (14 types like ;, " ", space)
RulesGrammarSyntax

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 TypeDescriptionFormat Specifier
intDecimal Integer%d or %i
floatFloating point%f
charCharacter%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; char only 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:

  1. Arguments: It requires 2 arguments (one on the left, one on the right).

  2. LHS (Left Hand Side): Must be a Variable. This is often called the L-value.

  3. 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:

  1. a = 5

    • Status: Invalid.

    • Reason: Missing the semicolon.

  2. a = 5;

    • Status: Valid.

    • Note: LHS (a) is a variable, RHS (5) is a constant.

  3. a = ;

    • Status: Invalid.

    • Reason: Expression Syntax Error. Both arguments must be present.

  4. = 10;

    • Status: Invalid.

    • Reason: Missing LHS.

  5. 10 = 20;

    • Status: Invalid.

    • Reason: L-value Error. The LHS is 10 (a constant), but the LHS must be a variable.

πŸ“š Navigate the Series

C Programming

Part 3 of 4

"Master C from the ground up! πŸš€ This series breaks down syntax, pointers, and memory management into clear, byte-sized guides. Perfect for beginners building a strong coding foundation."

Up next

C Programming Series - Guide

C Programming - Article 0