C Tutorial



SYNTAX IN C


Syntax in C Programming

In any programming language, **syntax** is like grammar in human language. It defines how we write code correctly. In C, syntax rules are strict. Even a small mistake like a missing semicolon can cause errors.

๐Ÿ“˜ Quick Note: C is a **case-sensitive** language. Main and main are different.

๐Ÿงฑ Basic Structure of a C Program

#include <stdio.h>  // Header file

int main() {        // Main function
    printf("Hello!"); // Statement
    return 0;       // Return statement
}
  

๐Ÿ“Œ Key Syntax Rules in C

  • ๐Ÿ”น Every **statement** ends with a **semicolon (;)**.
  • ๐Ÿ”น **Curly braces { }** define the start and end of a block of code.
  • ๐Ÿ”น **Comments** are added using // for single line or /* */ for multi-line.
  • ๐Ÿ”น Use **#include** to include header files like stdio.h.
  • ๐Ÿ”น The program execution starts from the **main()** function.

๐Ÿ“ Example with Comments

#include <stdio.h>

int main() {
    // This is a single-line comment
    printf("Learning C Syntax!");  // Print statement
    return 0;
}
  

๐Ÿงช Try It Yourself

Modify the code to:

  1. Print your name and age.
  2. Add a comment for each line.
  3. Use \n for line breaks.

โŒ Common Syntax Errors

  • ๐Ÿšซ Missing semicolons
  • ๐Ÿšซ Unmatched braces { }
  • ๐Ÿšซ Misspelling function names like main or printf
  • ๐Ÿšซ Using wrong casing (like writing Main instead of main)

โš ๏ธ Syntax Tip: Always compile your code to catch syntax errors. The compiler will point out which line has an error.

๐Ÿ“Œ Summary

  • Each statement in C ends with a semicolon.
  • Blocks of code are wrapped in curly braces.
  • Execution starts from the main() function.
  • Proper syntax helps the compiler understand your code.

โœ… Challenge: Write a program that prints your favorite quote and uses both single-line and multi-line comments.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review