Let's start with some fundamental C code examples that demonstrate basic compiler functionality. These examples focus on simple input, processing, and output, perfect for beginners.
Example 1: Hello, World!
This is the quintessential introductory program. It showcases the basic structure of a C program and demonstrates output to the console.
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
This code includes the standard input/output library (stdio.h
), defines a main
function (the entry point of the program), prints "Hello, World!" to the console using printf
, and returns 0 to indicate successful execution.
Example 2: Adding Two Numbers
This example demonstrates variable declaration, arithmetic operations, and input/output.
#include <stdio.h> int main() { int num1, num2, sum; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); sum = num1 + num2; printf("Sum: %d\n", sum); return 0; }
This code takes two integer inputs from the user using scanf
, adds them together, and displays the result using printf
. Note the use of &
before num1
and num2
in scanf
; this is crucial for providing the memory addresses where the input values should be stored.
The examples in the previous section are designed for easy compilation. Here are a couple more, focusing on slightly different aspects:
Example 3: Using a for
loop
This demonstrates iterative processing using a for
loop.
#include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { printf("%d ", i); } printf("\n"); return 0; }
This code prints the numbers 0 through 9 using a for
loop. It initializes i
to 0, continues as long as i
is less than 10, and increments i
by 1 in each iteration.
Example 4: Conditional statements (if-else
)
This example demonstrates decision-making using if-else
statements.
#include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); if (number > 0) { printf("The number is positive.\n"); } else if (number < 0) { printf("The number is negative.\n"); } else { printf("The number is zero.\n"); } return 0; }
This code takes an integer input and checks whether it's positive, negative, or zero, printing an appropriate message.
Numerous online resources offer simple C code examples for beginners. Here are some suggestions:
The examples provided earlier are all relatively easy to understand. Here are a couple of shorter snippets focusing on specific aspects:
Snippet 1: Calculating the area of a rectangle
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
This snippet directly calculates and prints the area of a rectangle without user input.
Snippet 2: Using a while
loop
#include <stdio.h> int main() { int num1, num2, sum; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); sum = num1 + num2; printf("Sum: %d\n", sum); return 0; }
This snippet uses a while
loop to print the numbers 0 through 4. The loop continues as long as count
is less than 5. Remember to compile these examples using a C compiler (like GCC) before running them.
The above is the detailed content of What are the simple codes for C language compilers. For more information, please follow other related articles on the PHP Chinese website!