Which is not a reserved keyword in c
We know that C programming language is one of the oldest languages to exist in the history of the development and it was coined by Dennis Ritchie in the late fall of the 1970. There are some words in the C programming language with fixed meaning and by the rules defined by the community we can’t use these words for our personal benefit. Even if we try to use them, they will throw an error. These are called the reserved keywords. They are the essential parts of when writing a program and their efficient and careful usage is also very important. But in c programming, not all words are reserved keywords. So, in this tutorial we are basically going to see which all are the reserved keywords, what do they mean, how they behave and what are their uses.
The Scenario
Before we move ahead and understand what are all the words that are not reserve keywords, we have to first understand what are the reserved keywords? Reserved keywords are the words that are already booked by the programming community and have their special functions and they can’t be used as names for the other variables and functions. Even if we try to do so it will throw an error.
Non- reserved
Non reserved keywords are completely different in behaviour than the reserved ones, unlike them we can use the non-reserved keywords as the names for our variables and functions and they will not interfere with any of the language rules and not throw an error like the reserved ones. One example that we can use here is “sayhello”, even though its not suggested that we use this form of naming convention as it will make the code look very messy and disturbed but we can use it and it will not throw an error if we are only looking at the technical front. If we had reserved keywords for everything it would make hard for the programmers to use the real world names for their variables and function while coding so its important to understand the significance of the non-reserved keywords as well.
Practices
When we are talking about practices, we should keep in mind that we are adopting healthy practices only, we should be able to see that we are only practicing those habits in our code that promotes the maintainability as well as the reusability of the code. One of the main concerns is we should pay attention while we are naming the variables and functions in our code and add only those names that goes in sync with our code. It will help other people understand our code better. If someone who hasn’t prepared the code looks at our code, he/she should be able to get a glimpse and grips of what we are trying to portray with our words and actions. We as developers should also use the common naming conventions and not use weird names in our program so that if anyone outside our coding background looks at it, he/she shouldn’t face any sort of trouble.
Advantages
In this section of the tutorial, we are going to see some of the plus points and benefits of using the reserved keywords: -
- Clarity
These special words in the language makes our work much easier as they help us in defining the basic parts of the language which makes the language very easy to understand and learn.
- Avoiding confusion
Reserved keywords basically define the main parts of the programs primarily and so it doesn’t cause any sort of confusion. They define the main parts and help prevent any sort of misunderstanding and tells what a particular part of the code should do at a given point.
- Code compatibility
Reserved keywords have some special meaning that are set by the community of the programming languages and it remains unaltered and so we can use them with different versions or computer systems and it will work perfectly. The main idea behind defining these words is that we can run them everywhere.
- Recognition through system
When a computer system sees a reserved keyword, it identifies that sort of word and then executes the corresponding action associated with it. This makes sure that whenever we execute a code it will work as it is supposed to be.
- Following the rules
Reserved keywords always make sure that all the developers should follow the rules of the programming languages. They provide guidance to the developers to produce high quality code and stick to the best practices while writing the code.
- Finding mistakes
Reserved keywords help us in filtering our code and catch any mistakes or errors early in the code. As suppose if anyone tries to use this keyword in any wrong way then the compiler will catch this mistake early in the code while reading the code and will not process it.
Example 1:
#include <stdio.h>
#include <string.h>
// Function to check if a word is a reserved keyword
int isReservedKeyword(char *word) {
// List of common C keywords
char *keywords[] = {
"auto", "break", "case", "char", "const", "continue", "default", "do",
"double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
"long", "register", "return", "short", "signed", "sizeof", "static", "struct",
"switch", "typedef", "union", "unsigned", "void", "volatile", "while"
// Add more keywords as needed
};
// Check if the given word is a reserved keyword
for (int i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1; // Reserved keyword found
}
}
return 0; // Not a reserved keyword
}
int main() {
// List of words to check
char *words[] = {
"hello", "world", "int", "print", "for", "variable", "if", "example", "else"
// Add more words as needed
};
// Check each word and display the result
for (int i = 0; i < sizeof(words) / sizeof(words[0]); i++) {
if (isReservedKeyword(words[i])) {
printf("\"%s\" is a reserved keyword.\n", words[i]);
} else {
printf("\"%s\" is not a reserved keyword.\n", words[i]);
}
}
return 0;
}
Output:
Example 2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to represent a bank account
struct BankAccount {
char accountNumber[20];
char holderName[50];
double balance;
double transactionHistory[100];
int transactionCount;
};
// Function to create a new bank account
void createAccount(struct BankAccount *account) {
printf("Enter account number: ");
scanf("%s", account->accountNumber);
printf("Enter account holder's name: ");
scanf("%s", account->holderName);
account->balance = 0.0;
account->transactionCount = 0;
printf("Account created successfully!\n");
}
// Function to deposit money into an account
void deposit(struct BankAccount *account, double amount) {
if (amount > 0) {
account->balance += amount;
account->transactionHistory[account->transactionCount++] = amount;
printf("Deposit successful. New balance: %.2f\n", account->balance);
} else {
printf("Invalid deposit amount. Please enter a positive value.\n");
}
}
// Function to withdraw money from an account
void withdraw(struct BankAccount *account, double amount) {
if (amount > 0 && amount <= account->balance) {
account->balance -= amount;
account->transactionHistory[account->transactionCount++] = -amount;
printf("Withdrawal successful. New balance: %.2f\n", account->balance);
} else if (amount <= 0) {
printf("Invalid withdrawal amount. Please enter a positive value.\n");
} else {
printf("Insufficient funds for withdrawal.\n");
}
}
// Function to display account information and transaction history
void displayAccount(struct BankAccount *account) {
printf("\nAccount Information:\n");
printf("Account Number: %s\n", account->accountNumber);
printf("Account Holder: %s\n", account->holderName);
printf("Current Balance: %.2f\n", account->balance);
printf("\nTransaction History:\n");
for (int i = 0; i < account->transactionCount; i++) {
if (account->transactionHistory[i] > 0) {
printf("Deposit: %.2f\n", account->transactionHistory[i]);
} else {
printf("Withdrawal: %.2f\n", -account->transactionHistory[i]);
}
}
printf("\n");
}
int main() {
struct BankAccount accounts[10]; // Assuming a maximum of 10 accounts
int accountCount = 0;
while (1) {
printf("\nBanking System Menu:\n");
printf("1. Create Account\n");
printf("2. Deposit\n");
printf("3. Withdraw\n");
printf("4. Display Account Information\n");
printf("5. Exit\n");
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (accountCount < 10) {
createAccount(&accounts[accountCount]);
accountCount++;
} else {
printf("Maximum number of accounts reached.\n");
}
break;
case 2:
if (accountCount > 0) {
int accountIndex;
printf("Enter account index (0 to %d): ", accountCount - 1);
scanf("%d", &accountIndex);
if (accountIndex >= 0 && accountIndex < accountCount) {
double depositAmount;
printf("Enter deposit amount: ");
scanf("%lf", &depositAmount);
deposit(&accounts[accountIndex], depositAmount);
} else {
printf("Invalid account index.\n");
}
} else {
printf("No accounts available.\n");
}
break;
case 3:
if (accountCount > 0) {
int accountIndex;
printf("Enter account index (0 to %d): ", accountCount - 1);
scanf("%d", &accountIndex);
if (accountIndex >= 0 && accountIndex < accountCount) {
double withdrawalAmount;
printf("Enter withdrawal amount: ");
scanf("%lf", &withdrawalAmount);
withdraw(&accounts[accountIndex], withdrawalAmount);
} else {
printf("Invalid account index.\n");
}
} else {
printf("No accounts available.\n");
}
break;
case 4:
if (accountCount > 0) {
int accountIndex;
printf("Enter account index (0 to %d): ", accountCount - 1);
scanf("%d", &accountIndex);
if (accountIndex >= 0 && accountIndex < accountCount) {
displayAccount(&accounts[accountIndex]);
} else {
printf("Invalid account index.\n");
}
} else {
printf("No accounts available.\n");
}
break;
case 5:
printf("Exiting the program. Goodbye!\n");
exit(0);
default:
printf("Invalid choice. Please enter a number between 1 and 5.\n");
}
}
return 0;
}
Output: