- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Basic Syntax
![]() Share with a Friend |
C Programming - C Basic Syntax
The basic syntax of C programming defines the rules for writing valid and executable programs. Understanding the syntax is essential to create, debug, and maintain C code effectively.
- Structure of a C Program
A typical C program consists of the following components:
- Preprocessor Directives: Begin with # and include libraries or macros.
- Main Function: Entry point of the program where execution starts.
- Statements and Expressions: Instructions for the compiler to execute.
- Functions: Modular blocks of code.
Example
#include <stdio.h> // Preprocessor directiveint main() { // Main function<
printf("Hello, World!\n"); // Statement
return 0; // Return statement
}
- Key Elements of C Syntax
- a) Case Sensitivity
- C is case-sensitive: main, Main, and MAIN are different identifiers.
- b) Semicolon ;
- Every statement must end with a semicolon.
Example:
int a = 10; // Validprintf("Hello"); // Valid
- c) Curly Braces {}
- Denote the start and end of a block of code.
Example:
if (a > 0) {printf("Positive number\n");
}
- d) Comments
- Single-line: Start with //
- Multi-line: Enclosed between /* and */
Example:
// This is a single-line comment/* This is a
multi-line comment */
- Data Types and Variables
- Variable Declaration: Every variable must have a type and be declared before use.
Example:
int age = 25;float salary = 5000.50;
- Input and Output
- Input: Use scanf() to take user input.
- Output: Use printf() to display output.
Example:
int num;
printf("Enter a number: ");
scanf("%d", &num); // Input
printf("You entered: %d\n", num); // Output
- Keywords and Identifiers
- Keywords: Reserved words (e.g., int, return).
- Identifiers: User-defined names for variables, functions, etc.
- Control Flow Statements
- Decision-making: if, else, switch
- Loops: for, while, do-while
Example:
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
- Functions
- Modular blocks of reusable code.
Example:
int add(int a, int b) {
return a + b;
}
- Indentation and Whitespace
- Proper indentation and spacing improve readability but do not affect program execution.
Example:
if (a > 0) {
printf("Positive\n");
}
- Example: Simple C Program
#include <stdio.h>
int main() {
int num1, num2, sum;
// Input numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Calculate sum
sum = num1 + num2;
// Print result
printf("Sum: %d\n", sum);
return 0;
}
Key Points
| Component | Description | Example |
|---|---|---|
Preprocessor Directive |
Includes libraries |
#include <stdio.h> |
Main Function |
Entry point of the program |
int main() { ... } |
Variables |
Store data |
int age = 30; |
Semicolon |
Ends a statement |
int a = 5; |
Curly Braces |
Define blocks |
{ ... } |
Input and Output |
Take input and display output |
scanf, printf |
Comments |
Document the code |
//, /* ... */ |
By adhering to C's basic syntax, you can write functional and error-free programs.
