C PROGRAM
A C program is a set of instructions written in the C programming language
that a computer can execute. C is a procedural, general-purpose
programming language developed by Dennis Ritchie at Bell Laboratories in
1972. It is known for its efficiency, low-level memory access capabilities,
and versatility, making it suitable for various applications, including
operating systems, embedded systems, and high-performance computing.
A basic C program typically includes:
Header files:
These files, indicated by #include directives (e.g., #include <stdio.h>), provide
access to pre-defined functions and macros for common tasks like input/output
operations.
main() function:
This is the entry point of every C program, where execution begins.
Statements:
These are instructions that perform specific actions, such as declaring variables,
performing calculations, or printing output to the console. Each statement
typically ends with a semicolon (;).
Comments:
These are explanatory notes within the code that are ignored by the compiler,
used to improve code readability.
BASIC PROGRAMS:
Example of a simple C program (Hello World):
C
#include <stdio.h> // Includes the standard input/output library
int main() { // The main function, where program execution begins
printf("Hello, World!\n"); // Prints "Hello, World!" to the console
return 0; // Indicates successful program execution
}
To run a C program, it must first be compiled using a C compiler (e.g.,
GCC) to translate the source code into an executable file that the computer
can understand and execute.
/ Header file for input output functions
#include <stdio.h>
// Main function: entry point for execution
int main() {
// Writing print statement to print hello world
printf("Hello World");
return 0;
}
Output
Hello World
Explanation:
#include <stdio.h> – This line includes the standard
input-output library in the program.
int main() – The main function where the execution of
the program begins.
printf(“Hello, World!\n”); – This function call prints
“Hello, World!” followed by a new line.
return 0; -This statement indicates that the program
ended successfully.
To go beyond the basics and explore data structures and more
advanced topics, the C Programming Course Online with
Data Structures takes you from beginner to expert
C Program To Print Your Own Name
Printing your own name means displaying your name on the
computer screen. In this article, we will learn how to print your
own name using a C program.
Examples
Input: name = "Rahul"
Output: Rahul
Explanation: The program prints "Rahul" to the screen.
Input: name = "Vikas"
Output: Vikas
Explanation: The program prints "Vikas" to the screen.
Print Your Own Name Using printf()
The simplest way to print something is to use
the printf() function. You can provide your name in the form
of string to printf() function and it will print it on the output
screen.
Syntax of printf
printf("your_name_here")
Program to Print Your Own Name Using printf
// C Program to Print Your Own Name using printf
#include <stdio.h>
int main() {
// Printing your name "Rahul" on the output screen
printf("Rahul");
return 0;
}
Output
Rahul
Print an Integer Value in C
Last Updated : 26 May, 2023
Printing an Integer in C is one of the most basic tasks. Input and
output of the elements are essential for taking input from the
user and then giving the output to the user. Here we are going to
take an integer as input from the user with the help of the
scanf() function and print that integer with the help of the printf()
function in C language.
How to Read and Print an Integer Value in
C
1. Printing Integer values in C
Approach:
1. Store the integer value in the variableOfIntType x.
2. Print this value using the printf() method. The printf()
method, in C, prints the value passed as the parameter
to it, on the console screen.
Syntax:
printf("%d", variableOfIntType);
Below is the C program to print the integer value:
// C Program to Print
// Integer value
#include <stdio.h>
// Driver code
int main()
{
// Declaring integer
int x = 5;
// Printing values
printf("Printing Integer value %d", x);
return 0;
}
Output
Printing Integer value 5
2. Reading Integer values in C
Approach:
1. The user enters an integer value when asked.
2. This value is taken from the user with the help of
the scanf() method. The scanf() method, in C, reads the
value from the console as per the type specified.
3. For an integer value, the X is replaced with the type int.
The syntax of the scanf() method becomes as follows
then:
Syntax:
scanf("%d", &variableOfIntType);
Below is the C program to read integer values in C:
// C program to take an integer
// as input and print it
#include <stdio.h>
// Driver code
int main()
{
// Declare the variables
int num;
// Input the integer
printf("Enter the integer: ");
scanf("%d", &num);
// Display the integer
printf("Entered integer is: %d", num);
return 0;
}
Output:
Enter the integer: 10
Entered integer is: 10
C Program To Convert Fahrenheit To Celsius
Last Updated : 21 Jul, 2023
In this article, we will learn to write a C Program to convert
temperature from Fahrenheit to Celsius by applying the
conversion formula to calculate the equivalent temperature in
Celsius. For example, 82° in Fahrenheit is equal to 27.7° in
Celcius.
Formula to Convert Fahrenheit to Celsius
T(°C) = (T(°F) - 32) × 5/9
where,
T(°C): Temperature in Celsius.
T(°F): Temperature in Farenheit.
Algorithm
1. Define temperature in Fahrenheit Units.
2. Apply the formula to convert temperature in Fahrenheit to
Celsius.
3. Print the temperature in Celsius.
Program to Convert Fahrenheit to Celcius in
C
Loading Playground...
// C Program to convert
// Fahrenheit to Celsius
#include <stdio.h>
// Function to convert Degree
// Fahrenheit to Degree Celsius
float fahrenheit_to_celsius(float f)
{
return ((f - 32.0) * 5.0 / 9.0);
}
// Driver code
int main()
{
float f = 40;
// Passing parameter to function
printf("Temperature in Degree Celsius : %0.2f",
fahrenheit_to_celsius(f));
return 0;
}
Output
Temperature in Degree Celsius : 4.44
Complexity Analysis
Time Complexity: O(1)
Auxiliary Space: O(1)
Prime Number Program in C
Last Updated : 11 Jul, 2025
A prime number is a natural number greater than 1 and is
completely divisible only by 1 and itself. In this article, we will
learn how to check whether the given number is a prime number
or not in C.
Examples:
Input: n = 29
Output: 29 is Prime
Explanation: 29 has no divisors other than 1 and 29 itself.
Hence, it is a prime number.
Input: n = 15
Output: 15 is NOT prime
Explanation: 15 has divisors other than 1 and 15 (i.e., 3 and 5).
Hence, it is not a prime number.
We can check whether a number is prime using various
approaches:
Table of Content
Brute Force Method - O(n) Time
Optimized Approach - O(√n) Time
Brute Force Method - O(n) Time
We can check whether the number is prime or not by iterating in
the range from 1 to n using loops. We will count the number of
divisors. If there are more than 2 divisor (including 1 and n) then
the given number n is not prime, else n is prime. This method is
known as trial division method.
Example:
#include <stdbool.h>
#include <stdio.h>
int main() {
int n = 29;
int cnt = 0;
// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
printf("%d is NOT prime", n);
else {
// Count the all divisors of
// given number
for (int i = 1; i <= n; i++) {
// Check n is divided by
// i or not
if (n % i == 0)
cnt++;
}
// If n is divisible by more than 2 numbers
// then it is not prime
if (cnt > 2)
printf("%d is NOT prime", n);
// else it is prime
else
printf("%d is prime", n);
}
return 0;
}
Output
29 is prime
C Program to Multiply two Floating Point
Numbers
Last Updated : 11 Jul, 2025
Floating point numbers are a way to represent real numbers with
both whole parts and fractional parts. In this article, we will learn
how to write a C program to find the product of two floating-point
numbers.
The below image shows an example of floating point
multiplication in C:
C Program To Multiply Two Floating-Point
Numbers
The below C program multiplies two floating numbers using the
multiplication operator ( * ).
// C program to multiply two
// floating point numbers
#include <stdio.h>
// Function to multiply floating point
// numbers
float multiply(float a, float b)
{
return a * b;
}
// Driver code
int main()
{
float A = 2.12, B = 3.88, product;
// Calling product function
product = multiply(A, B);
// Displaying result up to 3 decimal places.
printf("Product of entered numbers is:%.3f", product);
return 0;
}
Output
Product of entered numbers is:8.226
C Program to Swap Two Numbers
Last Updated : 01 May, 2025
Swapping two numbers means exchanging their values. In this
article, we will learn how to swap values of two numbers in a C
program.
The easiest method to swap two numbers is to use a temporary
variable. First, we assign the value of first variable to temporary
variable, then assign the value of second variable to first
variable and in the last assign the value of temporary variable to
second variable, which is the value of first variable.
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
// Swapping values of a and b
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output
a = 10, b = 5
Without Using a Temporary Variable
In this method, we use arithmetic operations to swap the values
without using a temporary variable.
#include <stdio.h>
int main() {
int a = 5, b = 10;
// Arithmetic operations to swap values
a = a + b;
b = a - b;
a = a - b;
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output
a = 10, b = 5
Using Bitwise XOR Operator
We can also use the bitwise XOR operator to swap the values
without using a temporary variable.
#include <stdio.h>
int main() {
int a = 5, b = 10;
// Apply XOR operations in the given order
// to swap values
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("a = %d, b = %d\n", a, b);
return 0;
}
Try it on GfG Practice
Output
a = 10, b = 5
ASCII Value of a Character in C
Last Updated : 12 Aug, 2024
In this article, we will discuss about the ASCII values that are bit
numbers used to represent the character in the C programming
language. We will also discuss why the ASCII values are needed
and how to find the ASCII value of a given character in a C
program.
Table of Content
What is ASCII Value of a Character in C
Why are ASCII values needed in C?
How to Find ASCII Value of a Character in C?
o Find ASCII Value of a Character Using Format
Specifier
o Find ASCII Value of a Character Using Explicit
Typecasting
Conclusion
Frequently Asked Questions - FAQs on C ASCII Values
What is ASCII Value of a Character in C
American Standard Code for Information Interchange
(ASCII) is a character encoding standard that assigns a unique
numerical value to all characters including special symbols. In C
programming, the ASCII value of the character is stored instead of
the character itself.
Each character or special character is represented by
some ASCII code.
Each ASCII code occupies 7 bits in memory.
Each character variable is assigned an ASCII value
ranging from 0 to 127.
Example
Input: char ch = 'A';
Output: ASCII value of A is 65
Explanation: The character 'A' has an ASCII value of 65.
Input: char ch = 'z';
Output: ASCII value of z is 122
Explanation: The character 'z' has an ASCII value of 122.
Why are ASCII Values Needed in C?
Digital computers store the information in the form of bits (0 or
1). So, to represent different characters, which can be very well
over 200s, we use a sequence of bit that is stored in the memory
in place of that character.
ASCII was introduced to standardize this representation so that
the text written in one compiler can be read as it is in the other
compiler. Otherwise, bit sequence representing the character 'A'
in one compiler can represent the whole different thing in another
compiler.
How to Find ASCII Value of a Character in C?
There are two major ways to find the ASCII value of a character:
1. Using Format Specifier - Implicit
2. Using Typecasting - Explicit
1. Find ASCII Value of a Character Using Format
Specifier
We can find the ASCII value of a character using the %d format
specifier in the printf function instead of %c while printing the
character. This happens because of the integer promotion where
the character is converted to the corresponding ASCII value when
some integer operation is performed on it.
C Program To Print ASCII Value of a Character Using
Format Specifier
// C program to print ASCII Value of Character using
// implicit conversion with format specifier.
#include <stdio.h>
int main() {
char c = 'k';
// %d displays the integer value of
// a character
// %c displays the actual character
printf("The ASCII value of %c is %d", c, c);
return 0;
}
Output
The ASCII value of k is 107
Time complexity: O(1)
Auxiliary Space: O(1)
Apart from the method shown above, we can also find the ASCII
value of a character using following methods:
2. Find ASCII Value of a Character Using Explicit
Typecasting
We can also convert the character to its corresponding ASCII
number using manual type conversion/casting.
Syntax of Typecasting in C
(target_type) variable
C Program To Print ASCII Value of a Character Using
Explicit Typecasting
// C program to print ASCII Value of Character using
// typecasting into integer
#include <stdio.h>
int main() {
char ch = 'A';
// Find the ASCII value of a character using typecasting
int asciiValue = (int)ch;
printf("ASCII value of %c is %d\n", ch, asciiValue);
return 0;
}
Output
ASCII value of A is 65
Time complexity: O(1)
Auxiliary Space: O(1)
Conclusion
In this article, we’ve discussed the concept of ASCII values and
their significance in C programming. We also learned how to find
the ASCII value of a given character in a C program.