C Syntax

1. Basic Structure of a C Program

Every C program follows a standard structure:

#include <stdio.h> // Preprocessor directive

int main() { // Main function
// Your code goes here
return 0; // Exit status
}

Explanation:

  1. #include <stdio.h> → Tells the compiler to include the standard input/output library.

  2. int main() → Entry point of the program where execution begins.

  3. { } → Curly braces define the start and end of a code block.

  4. return 0; → Terminates the program and returns 0 to the operating system.


2. Case Sensitivity

  • C is case-sensitive, meaning Variable and variable are different identifiers.

int age = 25; // valid
int Age = 30; // different variable

3. Semicolon ;

  • Every statement must end with a semicolon. Missing it causes a compilation error.

int x = 10; // Correct
int y = 20 // Incorrect, missing semicolon

4. Comments

Comments are ignored by the compiler and are used for documentation.

// Single-line comment

/*
Multi-line comment
This is ignored by the compiler
*/


5. Identifiers

  • Names given to variables, functions, arrays, etc.

  • Rules:

    1. Can contain letters, digits, and underscores.

    2. Must start with a letter or underscore.

    3. Cannot be a C keyword (like int, if, return).

int myVariable = 10;
float _price = 5.5;

6. Keywords

  • Reserved words in C with predefined meaning.

  • Examples: int, float, char, if, else, return, while, for, switch, void, break, continue.


7. Data Types

C has primary data types:

Data Type Size (Typical) Example
int 4 bytes int age = 25
float 4 bytes float pi = 3.14
double 8 bytes double d = 3.14159
char 1 byte char c = ‘A’
void 0 void function() {}

8. Variables

Variables store data. Must be declared with a data type:

int age = 25;
float height = 5.9;
char grade = 'A';
  • Variables can be initialized at declaration or later.


9. Operators

C supports different operators:

  1. Arithmetic Operators+ - * / %

  2. Relational Operators== != > < >= <=

  3. Logical Operators&& || !

  4. Assignment Operators= += -= *= /= %=


10. Statements and Blocks

  • Statement: Single instruction ending with a semicolon.

  • Block: Multiple statements enclosed in {}.

int main() {
int x = 10; // statement
int y = 20;

if(x < y) { // block
printf("x is less than y\n");
}

return 0;
}


11. Functions

Functions break a program into reusable parts:

#include <stdio.h>

void greet() { // Function definition
printf("Hello!\n");
}

int main() {
greet(); // Function call
return 0;
}


12. Input and Output

  • Output: printf()

  • Input: scanf()

int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is %d\n", age);
  • %d → integer, %f → float, %c → character, %s → string

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *