C Syntax
1. Basic Structure of a C Program
Every C program follows a standard structure:
Explanation:
-
#include <stdio.h>→ Tells the compiler to include the standard input/output library. -
int main()→ Entry point of the program where execution begins. -
{ }→ Curly braces define the start and end of a code block. -
return 0;→ Terminates the program and returns 0 to the operating system.
2. Case Sensitivity
-
C is case-sensitive, meaning
Variableandvariableare different identifiers.
3. Semicolon ;
-
Every statement must end with a semicolon. Missing it causes a compilation error.
4. Comments
Comments are ignored by the compiler and are used for documentation.
5. Identifiers
-
Names given to variables, functions, arrays, etc.
-
Rules:
-
Can contain letters, digits, and underscores.
-
Must start with a letter or underscore.
-
Cannot be a C keyword (like
int,if,return).
-
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:
-
Variables can be initialized at declaration or later.
9. Operators
C supports different operators:
-
Arithmetic Operators →
+ - * / % -
Relational Operators →
== != > < >= <= -
Logical Operators →
&& || ! -
Assignment Operators →
= += -= *= /= %=
10. Statements and Blocks
-
Statement: Single instruction ending with a semicolon.
-
Block: Multiple statements enclosed in
{}.
11. Functions
Functions break a program into reusable parts:
12. Input and Output
-
Output:
printf() -
Input:
scanf()
-
%d→ integer,%f→ float,%c→ character,%s→ string
