Here is a simple example of a C program that prints "Hello, World!" to the console:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
This program uses the printf
function from the stdio.h
library to print the string "Hello, World!" to the console. The #include
statement is used to include the stdio.h
library, which provides the printf
function.
The main
function is the entry point of the program, and it is where the program starts executing. Inside the main
function, the printf
function is called with the string "Hello, World!" as its argument.
The \n
at the end of the string is called a newline character, which tells the console to start a new line after the string is printed.
The return 0
statement at the end of the main
function tells the operating system that the program completed successfully.
Here is another example which takes user input, calculate the square and prints the result:
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
int square = number * number;
printf("The square of %d is %d", number, square);
return 0;
}
In this example, scanf
function is used to take user input, and it stores the input in the variable number
. The program then calculates the square of the number and stores it in the variable square
. Finally, the printf
function is used to print the result to the console.
These are just basic examples, C programming has many features, and you can use it to build a wide variety of software applications.
0 Comments