- 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 - Basics
![]() Share with a Friend |
C Programming - Basics
Introduction
C programming language is a foundational tool in computer science, combining simplicity with the ability to perform complex and hardware-level operations. Below is an outline of the key basics of C programming:
Structure of a C Program
Every C program follows a defined structure comprising the following components:
- Preprocessor Directives:
Instructions to the compiler, starting with # (e.g., #include <stdio.h>). - Main Function:
The entry point of a C program, where execution begins. - Statements and Expressions:
Instructions written to perform specific tasks. - Return Statement:
Ends the main() function and returns control to the operating system.
Example:
#include <stdio.h> // Preprocessor directive int main() { printf("Hello, World! \n"); // Print statement return 0; // Exit the program }
Basic Syntax
- Case Sensitivity:
C is case-sensitive (main is different from Main). - Semicolon:
Each statement ends with a semicolon (;). - Comments:
Used for documentation. - Single-line: // Comment here
- Multi-line: /* Comment here */
- Curly Braces {}:
Denote the beginning and end of blocks, such as functions.
Data Types
C provides a variety of data types to define variables:
- Basic Data Types:
- int: Integer numbers (e.g., 10, -5).
- float: Floating-point numbers (e.g., 3.14).
- double: Double-precision floating-point numbers.
- char: Single character (e.g., 'A').
- Derived Data Types:
- Arrays, Pointers, Structures, Unions.
- Void: Represents no value (e.g., void function).
Variables and Constants
- Variables:
Used to store data values.
int age = 25; // Variable declaration and initialization
- Constants:
Immutable values defined using const or #define.
const float PI = 3.14;
#define MAX 100
Input and Output
- Input: Use the scanf() function.
int num;
scanf("%d", &num); // Reads an integer input
- Output: Use the printf() function.
printf("Number is: %d", num); // Prints the variable's value
printf("Number is: %d", num); // Prints the variable's value
Operators
C supports various operators:
- Arithmetic Operators: +, -, *, /, %.
- Relational Operators: ==, !=, <, >, <=, >=.
- Logical Operators: &&, ||, !.
- Assignment Operators: =, +=, -=, etc.
- Bitwise Operators: &, |, ^, ~, <<, >>.
Control Flow
C provides control structures for decision-making and looping:
- Conditional Statements:
- if, else if, else.
- switch-case.
- Loops:
- for: For fixed iteration.
- while: For condition-based iteration.
- do-while: Executes at least once before checking the condition.
Example:
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i); // Outputs 0 1 2 3 4
}
Functions
Functions are used to encapsulate reusable code blocks.
- Syntax:
returnType functionName(parameters) {
// Code
return value;
}
- Example:
int add(int a, int b) {
return a + b;
}
Arrays
Arrays store multiple values of the same type.
int numbers[5] = {1, 2, 3, 4, 5};
printf("%d", numbers[2]); // Outputs 3
Pointers
Pointers store memory addresses of variables.
int x = 10;
int *p = &x; // Pointer to x
printf("%d", *p); // Dereferences p to output 10
Strings
Strings are arrays of characters terminated by a null character (\0).
char name[] = "John";
printf("Name: %s", name); // Outputs "John"
Memory Management
C provides dynamic memory allocation functions:
- malloc(), calloc() for allocation.
- free() for deallocation.
File Handling
C allows reading and writing to files using file pointers.
- File operations: fopen(), fclose(), fprintf(), fscanf().
FILE *fptr;
fptr = fopen("example.txt", "w");
fprintf(fptr, "Hello, File!");
fclose(fptr);
