Basics of C, Variables & Data Types
Expert Answer & Key Takeaways
Deep dive into the foundational elements of the C programming language: history, compilation process, tokens, variables, data types, and operators.
Basics of C, Variables & Data Types
C is a foundational, middle-level, procedural programming language developed by Dennis Ritchie at Bell Labs in 1972. It was originally created to develop the UNIX operating system. Because of its incredible speed and close-to-hardware capabilities, C is often called the 'mother of all programming languages'.
1. The Structure of a C Program
A standard C program follows a specific structure:
// 1. Preprocessor Directives
#include <stdio.h>
// 2. Main Function
int main() {
// 3. Variable Declaration & Execution
printf("Hello, World!");
// 4. Return Statement
return 0;
}#include <stdio.h>: Instructs the preprocessor to include the Standard Input/Output header file, which contains definitions forprintf()andscanf().int main(): The entry point of every C program. Execution always begins here.printf(): A built-in function used to print output to the console.return 0;: Indicates that the program terminated successfully. A non-zero return indicates an error.
2. Compilation Process in C
C is a compiled language. The process of converting human-readable source code (
.c file) into machine code (.exe file) involves 4 steps:- Preprocessing: Removes comments, expands macros (
#define), and includes header files. Output is an expanded.ifile. - Compilation: Converts the expanded code into Assembly code (
.sfile). - Assembly: Converts the assembly code into pure machine code/object code (
.oor.objfile). - Linking: Combines one or more object files and library files to generate the final executable file (
.exe).
3. C Tokens
A token is the smallest individual unit in a C program. The C compiler breaks the source code into tokens.
- Keywords: 32 reserved words with predefined meanings (e.g.,
int,if,while,return). - Identifiers: Names given to variables, functions, and arrays by the programmer. Rules:
- Must start with a letter or underscore (
_). - Cannot contain spaces or special symbols (except
_). - Cannot be a keyword.
- Must start with a letter or underscore (
- Constants: Fixed values that do not change during execution (e.g.,
10,'A',3.14). - Strings: A sequence of characters enclosed in double quotes (e.g.,
"Hello"). - Operators: Symbols that perform mathematical or logical operations (e.g.,
+,-,&&). - Special Symbols: Characters with special syntactic meaning (e.g.,
{},(),;,,).
4. Data Types
Data types specify the type and size of data that a variable can hold.
4.1 Primary (Primitive) Data Types
int: Used for integers (whole numbers).- Size: 2 or 4 bytes (depending on compiler/architecture).
- Format Specifier:
%dor%i
char: Used for single characters. Internally stored as ASCII integer values.- Size: 1 byte.
- Format Specifier:
%c
float: Used for single-precision decimal numbers.- Size: 4 bytes. Precision: 6 decimal places.
- Format Specifier:
%f
double: Used for double-precision decimal numbers.- Size: 8 bytes. Precision: 15 decimal places.
- Format Specifier:
%lf
void: Means 'no value'. Used to indicate a function returns nothing.
4.2 Derived & User-Defined Types
- Derived: Arrays, Pointers, Functions.
- User-Defined:
struct,union,enum,typedef.
5. Variables & Constants
- Variable: A named memory location whose value can change during execution. Declaration:
int age = 25; - Constant: A value that cannot be altered. Created using the
constkeyword or#definemacro.- Example:
const float PI = 3.14;
- Example:
6. Operators in C
- Arithmetic:
+,-,*,/,%(Modulus: returns remainder. Cannot be used with floats). - Relational:
==,!=,>,<,>=,<=(Returns 1 for true, 0 for false). - Logical:
&&(Logical AND): True if both operands are true.||(Logical OR): True if at least one operand is true.!(Logical NOT): Reverses the logical state.
- Assignment:
=,+=,-=,*=,/=. (e.g.,a += 5isa = a + 5). - Increment/Decrement:
++,--.- Pre-increment (
++a): Increase first, then use. - Post-increment (
a++): Use first, then increase.
- Pre-increment (
- Bitwise: Operates on bits (
&,|,^,~,<<,>>). - Ternary (Conditional):
condition ? expression1 : expression2;(Shorthand for if-else).
Course4All Editorial Board
Verified ExpertSubject Matter Experts
Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.