Character variable in C. Everything you should know.7 min read

This article is all about character variables in detailed with examples and how this character is being stored in the memory. Do you know how to know the binary of a signed digit? Learn all the things from this article.

Character variable printing method

putchar();

Meaning: put a character on a display

Use: Standard output function to print a character.

puts();

Meaning: put a string on a display

Use: One of the way to print only string on a display.


Example 1

#include<stdio.h>

int main(void)
{
     char v; //declaration
     char ch='A'; //initialisation

     //(OR) char v, ch='A';

     putchar(v);
     putchar(ch);
     putchar('*');
     putchar(' ');
     putchar(50); //This is act as a ASCII not like "50"
     putchar(100);
     putchar( ); //wrong way to call putchar function

     puts("Welcome to Electronics1010");

     return 0;
}

Output:

A* 2dWelcome to Electronics1010
Character variable example 1

Explanation:

putchar(v); //here no value present in v variable so whatever output comes it is called garbage value or in perfect technical term undefined output.

putchar(ch); // variable ch is having A character so it printed A on screen

putchar(‘*’); // function prints * character on screen

putchar(‘ ‘); // It prints space

putchar(50); // This value act as an ASCII of 2 not like “50” for more info on ASCII table please visit here.

putchar(100); //This is also ASCII value of d

putchar( ); //wrong way to call putchar function. Please comment on it or remove it for successful compilation.

puts(“Welcome to Electronics1010”); //This puts function print a string on the screen

Note: generally for data printing we are using printf(); We already learned in a past article. If you want to learn again click here.


Example 2

#include<stdio.h>

int main(void)
{
     char v, ch='A';
     
     v = ch+32;

     printf("%c \n", v);
     printf("%c %c \n", v, ch);

     return 0;
}

Output:

a 
a A 
Character variable example 2

Explanation for v = ch+32; :

ch = ‘A’ -> its ASCII value is 65.

v = 65 + 32 => 97 which is a ASCII of ‘a’


Example 3

#include<stdio.h>

int main(void)
{
     char v='a';

     printf("v= %c %d %x %o \n", v, v, v, v);

     v = v+1;
     printf("v= %c %d %x %o \n", v, v, v, v);

     v = '5';
     printf("v= %c %d %x %o \n", v, v, v, v);
 
     v = '*';
     printf("v= %c %d %x %o \n", v, v, v, v);

     return 0;
}

Output:

v= a 97 61 141 
v= b 98 62 142 
v= 5 53 35 65 
v= * 42 2a 52 
Character variable example 3

Explanation:

%d is decimal value (Base 10)

%x is hexadecimal value (Base 16)

%0 is Octal value (Base 8)

all the values are ASCII value of a particular character.

More elaborate way:

ASCII of 'a'

Example 4

#include<stdio.h>

int main(void)
{
     char v=50; // which is equal to char v='2';

     printf("v= %c %d \n", v, v);

     v = v*2;
     printf("v= %c %d \n", v, v);

     v = v*2;
     printf("v= %c %d \n", v, v);
     return 0;
}

Output:

v= 2 50 
v= d 100 
v= ? -56 
Character variable example 4

Explanation:

difference between signed char and unsigned char

By default character is signed character

So this is the reason why after 100*2 converted into -56

NOTE: for last value we can not say like “we got -56”. But, technically we can say “Last value is undefined”.


Example 5

#include<stdio.h>

int main(void)
{
     char ch='A';

     printf("ch= %c \n", ch);

     printf("Enter a character: ");
     
     ch = getchar(); 
/* (OR) scanf("%c", &ch); //both waits for a input */

     printf("ch= %c \n", ch);

     return 0;
}

Output:

Character variable example 5 output 1

Explanation:

program waits for user input.

in above input user entered @ program accepted and completed the execution by printing it.

Character variable example 5 output 2

Explanation:

here user entered two digits. So program took first char and finished the execution by printing it.


How to know binary of a signed digit.

Steps:

  1. Remove the negative sign of digit.
  2. write a binary of the only digit.
  3. Do 1’s complement of the digit
  4. Do 2’s complement of the digit
  5. The output of 2’s complement will be binary of a given negative digit.

NOTE: Below examples are having spelling mistake please forgive me. It’s “Complement” not “Compliment”.

signed digit to binary conversion example 1
signed digit to binary conversion example 2
signed digit to binary conversion example 3

How to know the value from signed binary

Steps:

  1. Make sure that you are having a signed binary.
  2. Do 1’s complement of the given number.
  3. Do 2’s complement of the given number.
  4. convert the binary into decimal.
  5. Just add a negative sign before a decimal.
  6. You got a negative decimal of the given signed binary.
signed binary to digit conversion example 1
signed binary to digit conversion example 2

Assignment

  1. Write a program in C, take character input from user and print its next character.

Example:

User input: G

Output: H

  1. Write a program in C, take character input from a user, and print its next 5 successive characters.

Example:

User input: S

Output: tuvwx

  1. Write a binary of given digits.
    • 68
    • -10
    • -34
    • -3
  1. Find a digit of a given binary. (Here binary are of signed digit)
    • 1110 1010
    • 1111 1100
    • 1010 1111
    • 1111 0011

Practice makes a man perfect

I know you can

Leave a Comment