The syntax of C programming refers to the formal set of rules, structure, and grammar that dictates how valid programs in C are written, parsed, and executed. Understanding the core syntax of C is the foundation for mastering any procedural or object-oriented programming language, including C++, Java, and Python.
Every standard C program contains a few essential building blocks. Here is a fully compliant modern C program (C99 / C11 / C17 / C23 standards):
#include <stdio.h> /* Preprocessor directive: includes Standard Input Output header */
int main(void) /* Main function: program execution begins here */
{
/* Output statement */
printf("Hello, World!\n");
return 0; /* Return status 0 indicates successful execution to OS */
}
#include <stdio.h>: A preprocessor directive that tells the compiler to include the standard input/output header file containing declarations for functions like printf() and scanf().
int main(): The entry-point function where every C program begins. The return type int indicates that the function returns an integer exit code to the operating system.
Curly Braces { }: Delimiters that mark the beginning and end of a function or code block.
printf("Hello, World!\n");: A built-in library function used to print formatted text on the screen. The escape sequence \n inserts a new line.
Semicolon ;: The statement terminator. Every statement in C must end with a semicolon.
return 0;: Sends exit code 0 back to the host environment, signifying clean and error-free termination.
Case Sensitivity: C is strictly case-sensitive. The keywords and standard functions are lowercase. printf is valid, but Printf or PRINTF will cause a compilation error. Similarly, int count; and int Count; represent two distinct variables.
Statement Termination: Every individual executable statement must end with the terminating character — semicolon (;).
Whitespace Ignored: The C compiler ignores arbitrary spaces, tabs, and newlines except when they separate keywords and identifiers. Proper indentation improves readability for programmers.
Comments: Comments are ignored by the compiler. C supports single-line comments (// comment) and multi-line comments (/* comment */).
The character set consists of all valid symbols that can be recognized by the C compiler:
Letters: All uppercase (A-Z) and lowercase (a-z) English alphabets.
Digits: Numbers from 0 to 9.
Special Characters: Semicolon (;), Colon (:), Period (.), Underscore (_), Ampersand (&), Asterisk (*), Braces, Brackets, and Operator symbols.
White Spaces: Blank space, Horizontal tab (\t), Carriage return (\r), and Newline (\n).
Keywords are predefined, reserved identifiers that carry special meaning for the compiler. They cannot be used as variable, function, or array names. Standard ANSI C defines 32 core keywords:
Category |
Reserved Keywords |
|---|---|
Data Types |
|
Control Flow |
|
Loops |
|
Storage Classes |
|
Derived / Custom |
|
Type Qualifiers & Return |
|
In C programming, identifiers are user-defined names assigned to variables, functions, arrays, structures, and labels. When naming identifiers in C, you must follow these mandatory syntax rules:
Identifiers can only contain alphanumeric characters (a-z, A-Z, 0-9) and the underscore (_).
The first character must always be a letter or an underscore. A variable name cannot start with a digit (e.g., int 1st_rank; is invalid; int rank_1; is valid).
No special characters (such as $, @, #, %, space, or hyphen) are permitted.
Reserved keywords cannot be used as identifier names (e.g., int for; is invalid).
C is case sensitive; hence total, Total, and TOTAL represent three different identifiers.
When you write C code (e.g. program.c) and compile it using GCC or Turbo C, it passes through 4 distinct stages to produce an executable file:
1. Preprocessing: Expands macros (#define), strips comments, and replaces #include directives with header file contents (generates program.i).
2. Compilation: Converts pure C code into intermediate Assembly code (generates program.s).
3. Assembly: Translates assembly instructions into machine-level object code (generates program.o or program.obj).
4. Linking: Combines object code with C runtime libraries (e.g., code for printf) to produce the final executable file (program.exe or a.out).
Review these key viva questions, spot-the-bug challenges, and MCQs frequently featured in BCA university exams.
Part 1: Spot the Bug & Predict the Output
Problem 1: What is wrong with the following C code snippet?
#include <stdio.h>
int main() {
int total$ = 100;
Printf("%d", total$);
return 0;
}
Answer & Explanation: This code has two fatal syntax errors: (1) The dollar sign $ is not permitted in C identifiers; (2) Printf has a capital 'P', causing an 'undefined reference' error because C is strictly case-sensitive.
Part 2: Multiple Choice Questions (BCA Exam Special)
Q1. Which of the following is a valid identifier in C?
A) 2count
B) _my_var
C) float
D) total marks
Correct Answer: B) _my_var (Identifiers may begin with an underscore; they cannot start with digits, contain spaces, or use keywords).
Q2. What is the role of \n in printf("Hello\nWorld");?
A) Prints a null character
B) Moves the cursor to the beginning of the next line
C) Displays the letter 'n'
D) Terminates the program
Correct Answer: B) Moves the cursor to the beginning of the next line.
Part 3: Hands-on Lab Challenge
Lab Task: Write a C program to declare your Name, BCA Roll Number, and Semester using appropriate data types and print them in a neat tabular format using printf() and tab escape sequences (\t).
Part 4: BCA University Viva Voce Questions
1. Why does every C program require a main() function?
Answer: main() serves as the designated entry point where the operating system transfers control to start program execution.
2. What is the difference between void main() and int main()?
Answer: int main() complies with modern ISO C standards by returning an integer exit status (0 for success) to the OS. void main() is a non-standard Turbo C legacy format that generates warnings on modern GCC/Clang compilers.