Syntax of C, Structure & Basic Terms
The syntax of a programming language defines the rules that dictate how code must be written. In C, the syntax is quite rigid, and a small mistake can lead to compilation errors.
Below are key elements of C syntax:
- Semicolon: Every statement in C ends with a semicolon (;).
- Braces: Curly braces ({}) define the scope of functions, loops, and other structures.
- Case Sensitivity: C is case-sensitive, so int and Int are different identifiers.
- Main Function: Every C program must have a main() function, which is the entry point of the program.
Example:
#include<stdio.h> int main() { printf("Hello, World!"); return 0; }
# | Preprocessor |
---|---|
Comments |
/*Hello World program*/ |
Header file |
#include <stdio.h> |
Global declarations |
int a = 10; |
main() function |
int main() |
Local variable |
{ |
Structure of a C Program
A C program typically follows a specific structure, which includes several key components:
- Preprocessor Directives: These lines begin with # and are used to include libraries or define macros.
- Global Declarations: Variables and functions declared outside of main() are global and can be accessed throughout the program.
- Main Function: This is where the program execution begins. Every C program must have a main() function.
- Local Declarations: Variables declared inside the main() or any other function are local to that function.
- Statements & Expressions: These are the executable parts of the program.
- Functions: C programs can have additional functions other than main() for better modularity.
stdio:
- Stands for standard input/output.
- Collection of predefined functions/methods.
- Also known as the C library.
include:
- Used to include header files into the program.
- Allows inclusion of standard or user-defined header files for accessing predefined functions or declaring custom functions.
#:
- Called preprocessor.
- Includes the C library into the program before execution.
- Used for preprocessor directives, which are instructions for the compiler.
- Common directives include #include, #define, and #ifdef.
There are two ways for using the #include directive:
- #include<filename>: with angle brackets.
- #include"filename": with quotes.
conio:
- Stands for console input/output.
- Provides functions for handling console input/output.
- Allows reading and writing to the console.
return:
- A statement to exit from a function.
- In the main function, it returns a value to the operating system.
- It signifies successful termination of the program.
Function:
- Block of code that performs a specific task.
- Can be reused multiple times throughout the program.
- C allows both built-in functions and user-defined functions.
int:
- Data type that denotes an integer value.
- Indicates that the function will return an integer value.
void:
- Indicates that a function does not return a value.
- Commonly used for functions that perform tasks but do not need to return any result.
Variable:
- Memory location with a name that can hold data.
- Variable values can change during program execution.
- Must be declared with a data type before use.
Data Types:
- Types of data that can be stored in a variable.
- C supports various data types including:
- int: Integer values
- float: Floating-point numbers
- char: Character values
- double: Double-precision floating-point numbers
Example Structure:
#include<stdio.h> // Global variable int globalVar = 10; // Function prototype void greet(); int main() { int localVar = 20; // Local variable greet(); // Function call printf("Global Variable: %d\n", globalVar); printf("Local Variable: %d\n", localVar); return 0; } // Function definition void greet() { printf("Hello, from a function!\n"); }
Hello, from a function!
Global Variable: 10
Local Variable: 20
Some Rules to Write a C Program
While writing a C program, there are several rules to follow to ensure that the program compiles and runs successfully:
- Every program must include a main() function: This is mandatory for program execution.
- C is case-sensitive: Ensure you are consistent with your variable names and function calls.
- Statements must end with a semicolon (;): This is a critical syntactical rule.
- Preprocessor directives don't end with a semicolon: Lines starting with # don’t need a semicolon.
- Use comments for better code readability: Single-line comments are written using //, and multi-line comments are enclosed within /* */.
- Declare variables before use: Every variable used must be declared before it can be utilized.
- Braces {} to define scope: Blocks of code in functions, loops, and conditions must be enclosed in curly braces.
- Whitespace is ignored: C ignores extra spaces, tabs, and newlines except inside string literals.
Example:
#include<stdio.h> int main() { // Declare an integer variable int age = 25; if(age >= 18) { printf("You are an adult.\n"); } else { printf("You are a minor.\n"); } return 0; }
Variables in C
Variables are containers for storing data values. In C, each variable must be declared with a specific data type before it can be used. The basic data types are:
- int: Used for storing integers (whole numbers) like 1, 2, 3, etc.
- float: Used for storing floating-point numbers (numbers with decimals) like 3.14, 9.81, etc.
- char: Used for storing single characters like 'a', 'b', etc.
- double: Used for storing large floating-point numbers (double precision).
Example:
#include<stdio.h> int main() { int myAge = 25; // Integer variable float myHeight = 5.9; // Float variable char grade = 'A'; // Character variable printf("I am %d years old.\n", myAge); printf("My height is %.1f feet.\n", myHeight); printf("My grade is %c.\n", grade); return 0; }
I am 25 years old.
My height is 5.9 feet.
My grade is A.
Data Types in C
C offers a variety of data types. Each variable in C has a specific data type, which determines the size and type of data it can store.
Data Type | Description | Size (Bytes) |
---|---|---|
int | Stores integers (whole numbers). | 4 |
float | Stores floating-point numbers (decimals). | 4 |
double | Stores large floating-point numbers. | 8 |
char | Stores a single character. | 1 |
Functions in C
Functions in C are blocks of code that perform specific tasks. Functions help in code reusability and organization. A function is defined once and can be called multiple times.
Every C program has a main function. Functions can also take parameters and return values.
Example:
#include<stdio.h> int add(int a, int b) { return a + b; // Returns the sum of a and b } int main() { int result = add(5, 3); printf("Sum: %d\n", result); return 0; }
Sum: 8
Conclusion
C programming is foundational and widely used in various fields, from system software to game development. Understanding its fundamentals is crucial for aspiring programmers.