Welcome to your C Programming journey with CodeYourCraft! In this lesson, we'll delve into the fundamental building blocks of C programming - C Tokens. By the end of this lesson, you'll have a solid understanding of these building blocks, and you'll be able to construct your own C programs with confidence.
C Tokens are the smallest units of a C program. They are used to represent program entities, such as keywords, identifiers, literals, operators, and punctuation. C Tokens help in understanding the meaning and structure of a C program, making it easier to write, debug, and maintain.
Keywords are reserved words in C programming that have specific meanings and uses. They are used to perform various tasks like control flow, data types, and functions. Here's a list of some common C keywords:
int: Integer data typefloat: Floating-point data typechar: Character data typevoid: Represents the absence of a valueif: Conditional statementelse: Alternative conditionwhile: Loop structure for repetitionfor: Loop structure for repetitiondo-while: Loop structure for repetitionswitch: Multiple conditional statementscase: Part of switch statementsdefault: Default case for switch statementsbreak: Exit a loop or switchcontinue: Skip the current iteration of a loopreturn: End a function and return a valueIdentifiers are names given to variables, functions, and labels in a C program. They are used to create a unique name for each entity in the program. Here are some rules for creating identifiers in C:
Literals are constant values that are used in a C program. They are of various types such as integer, floating-point, character, and string literals.
Integer literals are values that do not contain a decimal point. They can be positive or negative.
int num = 123; // Decimal number
int num = 0xABCD; // Hexadecimal number
int num = 0b10101010; // Binary numberFloating-point literals are values that contain a decimal point. They can be positive or negative.
float pi = 3.141592; // Decimal number
float e = 2.7182818; // Scientific notationCharacter literals are enclosed in single quotes ('). They can represent any printable ASCII character.
char letter = 'A';String literals are enclosed in double quotes ("). They can represent a sequence of characters.
char name[] = "John Doe";Operators are symbols that perform specific mathematical or logical operations. Here are some common C operators:
+: Addition-: Subtraction*: Multiplication/: Division%: Modulus^: Exponentiation+=: Addition and assignment-=: Subtraction and assignment*=: Multiplication and assignment/=: Division and assignment%=: Modulus and assignment==: Equality operator!=: Inequality operator<: Less than operator>: Greater than operator<=: Less than or equal to operator>=: Greater than or equal to operator&&: Logical AND operator||: Logical OR operator!: Logical NOT operatorPunctuation marks are used to separate various parts of a C program and to provide structure. Here are some common C punctuation marks:
;: Semicolon - Ends a statement,: Comma - Separates multiple expressions in a list{}: Braces - Encloses code blocks(): Parentheses - Group expressions or function calls[]: Brackets - Array subscripts.: Dot - Accesses a member of a structure or classNow that we've covered the basics, let's put our knowledge into practice. Here are two complete, working examples to help you get started:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("The entered integer is: %d\n", num);
return 0;
}#include <stdio.h>
int main() {
int num1, num2, operation;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the operation (1 - Addition, 2 - Subtraction, 3 - Multiplication, 4 - Division): ");
scanf("%d", &operation);
switch (operation) {
case 1:
printf("%d + %d = %d\n", num1, num2, num1 + num2);
break;
case 2:
printf("%d - %d = %d\n", num1, num2, num1 - num2);
break;
case 3:
printf("%d * %d = %d\n", num1, num2, num1 * num2);
break;
case 4:
printf("%d / %d = %f\n", num1, num2, (float)num1 / num2);
break;
default:
printf("Invalid operation.\n");
}
return 0;
}What is the output of the following code?
That concludes our comprehensive guide to C Tokens. We hope you found this lesson informative and engaging. Keep practicing, and remember, programming is a skill that improves with practice!
Happy coding! 💡🎯🚀