C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In the C programming language, an identifier is a name given to a variable, function, or other entity in the program. Identifiers can consist of letters, digits, and underscores, and must begin with a letter or underscore. Some examples of identifiers in C include myVariable
, myFunction
, and myStruct
.
Keywords are reserved words in the C programming language that have special meaning and cannot be used as identifiers. Some examples of keywords in C include int
, float
, char
, if
, while
, and return
.
Comments are used to document the code and provide explanations for the programmer or anyone reading the code. There are two types of comments in C: single-line comments, which begin with //
and continue until the end of the line, and multi-line comments, which begin with /*
and end with */
. Here's an example of comments in C:
// This is a single-line comment /* This is a multi-line comment */
Expressions are combinations of operators and operands that evaluate to a value. Some examples of expressions in C include 5 + 3
, x * y
, and z == 10
.
Statements are executable lines of code that perform a specific action. Statements in C can include assignments, function calls, loops, conditional statements, and more. Here's an example of a statement in C:
int x = 5; // This is an assignment statement that assigns the value 5 to the variable x
Understanding identifiers, keywords, comments, expressions, and statements is essential to writing and understanding C programs. By using meaningful identifiers, appropriate keywords, clear comments, and well-written expressions and statements, programmers can create efficient, effective, and maintainable code.
List of C Keywords and Their Meanings:
C keywords are reserved words that have special meanings in the C language. Here's a brief list of some C keywords:
auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
These keywords play crucial roles in defining the structure and behavior of C programs.
How to Use if-else Statements in C:
#include <stdio.h> int main() { int num = 10; // if-else statement if (num > 0) { printf("Positive\n"); } else if (num < 0) { printf("Negative\n"); } else { printf("Zero\n"); } return 0; }
The if-else
statement allows conditional execution of code based on the evaluation of a condition.
Loops and Iteration in C Programming:
#include <stdio.h> int main() { // While loop int i = 1; while (i <= 5) { printf("%d ", i); i++; } printf("\n"); // For loop for (int j = 5; j >= 1; j--) { printf("%d ", j); } printf("\n"); // Do-while loop int k = 1; do { printf("%d ", k); k++; } while (k <= 5); return 0; }
Loops in C, including while
, for
, and do-while
, enable repetitive execution of a block of code.
Switch Statements and Case Handling in C:
#include <stdio.h> int main() { int choice = 2; // Switch statement switch (choice) { case 1: printf("Option 1 selected\n"); break; case 2: printf("Option 2 selected\n"); break; case 3: printf("Option 3 selected\n"); break; default: printf("Invalid option\n"); } return 0; }
The switch
statement provides a way to handle multiple cases based on the value of an expression.