ASCII value in C

ASCII value in C

ASCII value in C:

What is ASCII code?

The full form of ASCII is the American Standard Code for information interchange. It is a character encoding scheme used for electronics communication. Each character or a special character is represented by some ASCII code, and each ascii code occupies 7 bits in memory.

In C programming language, a character variable does not contain a character value itself rather the ascii value of the character variable. The ascii value represents the character variable in numbers, and each character variable is assigned with some number range from 0 to 127. For example, the ascii value of ‘A’ is 65.

In the above example, we assign ‘A’ to the character variable whose ascii value is 65, so 65 will be stored in the character variable rather than ‘A’.

Let’s understand through an example.

We will create a program which will display the ascii value of the character variable.

#include <stdio.h>

int main()

{

char ch;    // variable declaration

printf(“Enter a character”);

scanf(“%c”,&ch);  // user input

printf(“\n The ascii value of the ch variable is : %d”, ch);

return 0;

}

In the above code, the first user will give the character input, and the input will get stored in the ‘ch’ variable. If we print the value of the ‘ch’ variable by using %c format specifier, then it will display ‘A’ because we have given the character input as ‘A’, and if we use the %d format specifier then its ascii value will be displayed, i.e., 65.

Output

ASCII value in C

The above output shows that the user gave the input as ‘A’, and after giving input, the ascii value of ‘A’ will get printed, i.e., 65.

Now, we will create a program which will display the ascii value of all the characters.

#include <stdio.h>

int main()

{

int k;   // variable declaration 

for(int k=0;k<=255;k++)  // for loop from 0-255

{

printf(“\nThe ascii value of %c is %d”, k,k);

}

return 0;

}

The above program will display the ascii value of all the characters. As we know that ascii value of all the characters starts from 0 and ends at 255, so we iterate the for loop from 0 to 255.

Now we will create the program which will sum the ascii value of a string.

#include <stdio.h>

int main()

{

int sum=0;  // variable initialization

char name[20];  // variable initialization

int i=0;  // variable initialization

printf(“Enter a name: “);

scanf(“%s”, name);

while(name[i]!=‘\0’)  // while loop

{

printf(“\nThe ascii value of the character %c is %d”, name[i],name[i]);

sum=sum+name[i];

i++;

}

printf(“\nSum of the ascii value of a string is : %d”, sum);

return 0;

}

In the above code, we are taking user input as a string. After taking user input, we execute the while loop which adds the ascii value of all the characters of a string and stores it in a ‘sum‘ variable.

Output

ASCII value in C

 

Related Post:

Features of C Programming Language

Variables in C

Data Types in C

Keywords in C

C Operators

Comments in C

Escape Sequence in C

C Functions

Storage Classes in C

Dynamic memory allocation in C

Factorial Program in C

Leap year program in C

Fibonacci Series in C

Prime Number program in C

Palindrome program in C

Sum of digits program in C

Escape Sequence in C

Difference Between Type Casting and Type Conversion in C

Difference Between Variables and Constants

Matrix multiplication in C

C Program to generate Fibonacci Triangle

C Program to print “hello” without semicolon

C Program to swap two numbers without third variable

C Program to reverse number

Count the number of digits in C

Tokens in C

C Identifiers

C Strings

Compile time vs Runtime in C

C break statement

C goto statement

Type Casting in C

C String Functions

C Pointers

Dangling Pointers in C

void pointer in C

Pointer to Pointer in C

Recursion in C

Call by value and Call by reference in C

File Handling in C

C fprintf() and fscanf()

C fputc() and fgetc()

C fputs() and fgets()

C fseek() function

Constant Pointers in C