C – Basic Syntax

Basic Syntax

Like any programming language, C has its own set of rules and conventions for writing code. Understanding the basic syntax of C is essential for writing correct and readable programs.

Comments

In C, comments are used to add explanatory notes to the code. They are ignored by the compiler and do not affect the execution of the program. Comments can be single-line or multi-line.

// This is a single-line comment

/*
This is a multi-line comment
It can span multiple lines
*/

Variables

Variables are used to store data in a program. In C, variables must be declared before they can be used. The declaration specifies the type of the variable and its name.

int age; // Declaring an integer variable named age
float pi; // Declaring a floating-point variable named pi
char letter; // Declaring a character variable named letter

Constants

Constants are fixed values that do not change during the execution of a program. In C, constants can be defined using the #define directive or the const keyword.

#define PI 3.14159 // Defining a constant named PI
const int MAX_VALUE = 100; // Defining a constant named MAX_VALUE

Data Types

C supports several data types, including integers, floating-point numbers, characters, and strings. Each data type has a specific range of values and operations that can be performed on it.

int age = 25; // Integer data type
float pi = 3.14; // Floating-point data type
char letter = 'A'; // Character data type
char name[] = "John"; // String data type

Operators

Operators are used to perform operations on variables and constants. C supports a wide range of operators, including arithmetic, assignment, comparison, and logical operators.

int sum = 10 + 5; // Addition operator
int difference = 10 - 5; // Subtraction operator
int product = 10 * 5; // Multiplication operator
float quotient = 10 / 5; // Division operator
int remainder = 10 % 5; // Modulo operator

Control Structures

Control structures are used to control the flow of a program. C supports several control structures, including if-else statements, loops, and switch statements.

if (condition) {
    // Code to be executed if the condition is true
} else {
    // Code to be executed if the condition is false
}

while (condition) {
    // Code to be executed repeatedly as long as the condition is true
}

for (initialization; condition; increment) {
    // Code to be executed repeatedly as long as the condition is true
}

switch (expression) {
    case value1:
        // Code to be executed if the expression is equal to value1
        break;
    case value2:
        // Code to be executed if the expression is equal to value2
        break;
    default:
        // Code to be executed if the expression does not match any of the values
        break;
}

Functions

Functions are blocks of code that perform a specific task. They allow code to be organized into reusable modules. In C, functions have a return type, a name, and a set of parameters.

int add(int a, int b) {
    return a + b;
}

float calculateCircleArea(float radius) {
    return PI * radius * radius;
}

Certainly! Let’s break down each of these concepts in C programming:

 Tokens

In C programming, a token is the smallest unit of a program that is meaningful to the compiler. Tokens can be classified into various types:
– Keywords: Reserved words that have predefined meanings in the C language (e.g., `int`, `if`, `else`).
– Identifiers: Names given to variables, functions, and other user-defined entities in a program (e.g., `sum`, `counter`, `main`).
– Constants: Fixed values that do not change during program execution (e.g., `10`, `3.14`, `’A’`).
– Operators: Symbols used to perform operations on variables and values (e.g., `+`, `-`, `*`, `/`).
– Punctuators: Special symbols used to separate parts of the program (e.g., `{`, `}`, `;`, `,`).
– Strings: Sequences of characters enclosed in double quotes (e.g., `”Hello, world!”`).

Semicolons (;)

In C programming, the semicolon (`;`) is used to terminate statements. Every statement in C must end with a semicolon. Omitting a semicolon at the end of a statement results in a syntax error.

“`c
int x = 10; // Statement ending with a semicolon
printf(“Hello, world!\n”); // Another statement ending with a semicolon
“`

Identifiers

Identifiers are names given to various program elements such as variables, functions, arrays, etc. They consist of letters (both uppercase and lowercase), digits, and underscores. Identifiers must follow certain rules:
– They cannot be a keyword (reserved word).
– They cannot contain spaces or special characters (except underscores).
– They must begin with a letter or underscore.

“`c
int count; // Identifier for a variable
void calculateSum(); // Identifier for a function
float average_score; // Identifier for a variable with underscores
“`

Keywords

Keywords are reserved words that have predefined meanings in the C language. They cannot be used as identifiers. Examples of keywords in C include `int`, `float`, `if`, `else`, `for`, `while`, `return`, etc.

“`c
int main() {
int num = 10;
if (num > 0) {
printf(“Positive number\n”);
} else {
printf(“Non-positive number\n”);
}
return 0;
}
“`

Whitespace

Whitespace refers to spaces, tabs, and newline characters in a program. In C, whitespace is used to separate tokens and make the code more readable. Whitespace is ignored by the compiler, except when it’s used to separate identifiers, keywords, constants, and operators.

“`c
int a = 10; // Spaces between tokens are ignored
if(num>0) { // Spaces between tokens can improve readability
printf(“Positive number\n”);
}
“`

Understanding these concepts is essential for writing correct and understandable C programs.

Scroll to Top