Sizeof Operator in C Programming Language

Lets write a C program to see how to use sizeof() method or function in C programming language.

sizeof() is a builtin method/function present in C programming. It is used to calculate the size(in bytes) that a datatype occupies in the computers memory.

sizeof() method takes a single argument and it is not a function whose value is determined at run time, but rather an operator whose value is determined by compiler – so it’s called as compile time unary operator.

sizeof() method can be used with primitive data type like int, float, char or user defined data types like structures, unions etc.

Video Tutorial: Sizeof Operator in C Programming Language


[youtube https://www.youtube.com/watch?v=Mlw4jJ4WX8A]

YouTube Link: https://www.youtube.com/watch?v=Mlw4jJ4WX8A [Watch the Video In Full Screen.]

Source Code: Sizeof Operator in C Programming Language

#include < stdio.h >

int main()
{
    int a, a1[10];
    char s;
    char s1[10];
    int b = 10, c = 5;

    printf("Size of a single character is %d byes\n", sizeof(s));
    printf("Size of a character array s1[10] is %d byes\n", sizeof(s1));
    printf("Size of a integer is %d byes\n", sizeof(a));
    printf("Size of a long integer is %d byes\n", sizeof(long int));
    printf("Size of a long long integer is %d byes\n", sizeof(long long int));
    printf("Size of a integer array a1[10] is %d byes\n", sizeof(a1));
    printf("Size of a float is %d byes\n", sizeof(float));
    printf("Size of a double is %d byes\n", sizeof(double));
    printf("Size of a long double is %d byes\n", sizeof(long double));
    printf("Size of a b+c is %d byes\n", sizeof(b+c));

    return 0;
}

Output:
Size of a single character is 1 byes
Size of a character array s1[10] is 10 byes
Size of a integer is 4 byes
Size of a long integer is 4 byes
Size of a long long integer is 8 byes
Size of a integer array a1[10] is 40 byes
Size of a float is 4 byes
Size of a double is 8 byes
Size of a long double is 12 byes
Size of a b+c is 4 byes

short, int, long int, long long int

#include < stdio.h >

int main()
{
    printf("Size of short data type is %d\n", sizeof(short));
    printf("Size of integer data type is %d\n", sizeof(int));
    printf("Size of long integer data type is %d\n", sizeof(long int));
    printf("Size of long long integer data type is %d\n", sizeof(long long int));

    return 0;
}

Output:
Size of short data type is 2
Size of integer data type is 4
Size of long integer data type is 4
Size of long long integer data type is 8

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Print ASCII Value of a Character

Lets write a C program to print/display ASCII value of a user entered character.

Note: In C programming language, every alphabet, number and symbol has corresponding ASCII value(a integer number representing the character).

Data Type

char is character data type in C programming language. It stores single character information in it and has a corresponding integer value for the character, which is its ASCII value.

Source Code: C Program to Print ASCII Value of a Character

#include < stdio.h >

int main()
{
    char c;

    printf("Enter a character\n");
    scanf("%c", &c);

    printf("ASCII value of %c is %d\n", c, c);

    return 0;
}

Output 1:
Enter a character
A
ASCII value of A is 65

Output 2:
Enter a character
a
ASCII value of a is 97

Output 3:
Enter a character
1
ASCII value of 1 is 49

Output 4:
Enter a character
0
ASCII value of 0 is 48

Output 5:
Enter a character
$
ASCII value of $ is 36

Video Tutorial: C Program to Find ASCII Value of a Character


[youtube https://www.youtube.com/watch?v=XVz8n7UDVA0]

YouTube Link: https://www.youtube.com/watch?v=XVz8n7UDVA0 [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert