The C language is known for its straightforward syntax and structure, which lays the foundation for many other programming languages. Here is an overview of the basic syntax in C:
1. Structure of a C Program
A typical C program has the following structure:
#include <stdio.h> // Preprocessor directive
int main() {
// Statements go here
printf("Hello, World!\n");
return 0;
}
#include <stdio.h>
: This is a preprocessor directive that includes the standard input/output library. It allows the program to use functions likeprintf()
.int main()
: Themain()
function is the entry point of every C program. The execution starts from here.return 0;
: Indicates that the program finished successfully. Non-zero values usually indicate an error.
2. Data Types
C supports several basic data types, which determine the type of data a variable can hold:
- Integer Types:
int
,short
,long
,unsigned int
- Floating-Point Types:
float
,double
- Character Type:
char
(used to store a single character) - Void:
void
(used for functions that do not return a value)
Example:
int age = 25;
float height = 5.9;
char initial = 'A';
3. Variables
Variables are used to store data. They must be declared before they are used, specifying their type.
int number; // Declaration
number = 10; // Initialization
4. Constants
Constants are values that cannot be changed during the program execution. You can use #define
or const
.
- Using
#define
:#define PI 3.14
- Using
const
:const int MAX_AGE = 100;
5. Operators
C provides a range of operators:
- Arithmetic Operators:
+
,-
,*
,/
,%
- Relational Operators:
==
,!=
,<
,>
,<=
,>=
- Logical Operators:
&&
(AND),||
(OR),!
(NOT) - Assignment Operators:
=
,+=
,-=
,*=
,/=
- Increment/Decrement Operators:
++
,--
Example:
int a = 5, b = 10;
int sum = a + b; // sum is 15
6. Control Flow Statements
C provides several statements to control the flow of execution.
Conditional Statements:
if
,else if
,else
:if (a > b) { printf("a is greater than b"); } else { printf("b is greater or equal to a"); }
switch
Statement: Used for multi-way branching.switch (choice) { case 1: printf("Choice is 1"); break; case 2: printf("Choice is 2"); break; default: printf("Invalid choice"); }
Loops:
for
Loop:for (int i = 0; i < 5; i++) { printf("%d ", i); }
while
Loop:int count = 0; while (count < 5) { printf("%d ", count); count++; }
do...while
Loop:int count = 0; do { printf("%d ", count); count++; } while (count < 5);
7. Functions
Functions allow you to modularize code by encapsulating specific tasks. A function has the following syntax:
return_type function_name(parameter_list) {
// Function body
}
Example:
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("Sum: %d\n", result);
return 0;
}
8. Input/Output
C provides functions for input and output through the stdio.h
library:
printf()
: Used to output text to the console.scanf()
: Used to read input from the user.
Example:
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You entered: %d\n", age);
9. Comments
Comments are used to explain code and are ignored by the compiler.
- Single-line comment:
// This is a single-line comment
- Multi-line comment:
/* This is a multi-line comment */
Summary
- Program Structure: Includes headers, the
main()
function, and statements. - Data Types and Variables: Declaring and initializing data.
- Operators: Performing operations on data.
- Control Flow: Using
if
,switch
, loops (for
,while
,do-while
). - Functions: Reusable blocks of code.
- Input/Output: Using
printf()
andscanf()
to interact with the user. - Comments: For readability.
These are the basic building blocks of C programming. Understanding them is crucial to writing functional and efficient C programs.