Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
How to Find the Size of an Array in C?
Next article icon

C Program to Find the Size of int, float, double and char

Last Updated : 16 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C program to find the size of the data types: int, float, double, and char in bytes and print it on the output screen.

Examples

Input: char
Output: Size of char: 1 byte

Input: int
Output:Size of int: 4 bytes

Different Methods to Find the Size of int, float, double and char in C

We can find the size of the int, float, double and char data types using many different methods available in C:

Table of Content

  • Using sizeof() Operator Directly
  • Using sizeof Operator on Variables
  • Using Pointers

1. Using sizeof() Operator Directly

In C, we have sizeof() operator that can find the size of the data type that is provided as the argument. We can use this operator to find the size of int, char, float and double by passing them as parameters.

Syntax of sizeof()

sizeof(data_type);

Implementation

The below program uses the sizeof operator on the data type directly to find its size of it in bytes

C
// C Program to Find the Size of int, float, double, and // char using sizeof operator directly #include <stdio.h>  int main() {        // Determine and Print the size of int     printf("Size of int: %u bytes\n", sizeof(int));      // Determine and Print the size of float     printf("Size of float: %u bytes\n", sizeof(float));      // Determine and Print the size of double     printf("Size of double: %u bytes\n", sizeof(double));      // Determine and Print the size of char     printf("Size of char: %u bytes\n", sizeof(char));      return 0; } 

Output
Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes Size of char: 1 bytes 

Time Complexity: O(1)
Aulixiary Space: O(1)

2. Using sizeof Operator on Variables

The sizeof operator does not only works on the data types, but also on the variables of these data types. We just need to pass the variable name instead of type.

Implementation

The below program uses the sizeof operator on the variable of a data type to find its size of it in bytes

C
// C program to find the size of int, char, // float and double data types #include <stdio.h>  int main() {      	// Variables of int, char, float and double     int integerType;     char charType;     float floatType;     double doubleType;      // Determine and Print  the size of integer type     printf("Size of int is: %u bytes", sizeof(integerType));      // Determine and Print the size of floatType     printf("\nSize of float is: %u bytes", sizeof(floatType));      // Determine and Print the size of doubleType     printf("\nSize of double is: %u bytes", sizeof(doubleType));      // Determine and Print the size of charType     printf("\nSize of char is: %u bytes", sizeof(charType));      return 0; } 

Output
Size of int is: 4 bytes Size of float is: 4 bytes Size of double is: 8 bytes Size of char is: 1 bytes

Time Complexity: O(1)
Aulixiary Space: O(1)

3. Using Pointers

In C, when you increment or decrement a pointer, the address changes based on the size of the data type it points to, not just by 1. For example, a pointer to integer stores address: 0x12fb1 will not simply increase to 0x12fb2 after incrementing, it will be 0x12fb5 accounting for the size of the integer.

So, the idea is to store this pointer as integer and find the difference between the memory addresses to find the size in bytes.

Note: This method only works if the system is byte addressable.

Implementation

The below program implements the above approach:

C
// C program to find the size of given data type using pointers #include <stdio.h>  int main() {     int intType;      // Pointer to the variable of type int     int *ptr = &intType;      // Converting the pointer to the long long unsigned int value     unsigned long long start = (unsigned long long )ptr;    	// Incrementing and converting it again     ptr++;     unsigned long long end = (unsigned long long)ptr;      // Find the difference/size     unsigned long long size = (unsigned long long)(end - start);     printf("Size of int is: %llu bytes", size);      return 0; } 

Output
Size of int is: 4 bytes

Time Complexity: O(1)
Aulixiary Space: O(1)

  • Data Types in C
  • Pointer Arithmetics in C

Next Article
How to Find the Size of an Array in C?
author
kartik
Improve
Article Tags :
  • C Programs
  • C Language
  • C-Data Types
  • C Basic Programs

Similar Reads

  • C Program to Find the Length of a String
    The length of a string is the number of characters in it without including the null character (‘\0’). In this article, we will learn how to find the length of a string in C. The easiest way to find the string length is by using strlen() function from the C strings library. Let's take a look at an ex
    2 min read
  • C Program to find size of a File
    Given a text file, find its size in bytes. Examples: Input : file_name = "a.txt" Let "a.txt" contains "geeks" Output : 6 Bytes There are 5 bytes for 5 characters then an extra byte for end of file. Input : file_name = "a.txt" Let "a.txt" contains "geeks for geeks" Output : 16 Bytes The idea is to us
    1 min read
  • How to Find the Size of an Array in C?
    The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C. The simplest method to find the size of an array in C is by using sizeof operator. First determine
    2 min read
  • C Program For Char to Int Conversion
    Write a C program to convert the given numeric character to integer. Example: Input: '3'Output: 3Explanation: The character '3' is converted to the integer 3. Input: '9'Output: 9Explanation: The character '9' is converted to the integer 9. Different Methods to Convert the char to int in CThere are 3
    3 min read
  • C Program For Int to Char Conversion
    To convert the int to char in C language, we will use the following 2 approaches: Using typecastingUsing sprintf() Example: Input: N = 65 Output: A1. Using TypecastingMethod 1:Declaration and initialization: To begin, we will declare and initialize our integer with the value to be converted.Typecast
    2 min read
  • C Program to Print the Length of a String using Pointers
    C language provides a built-in function strlen() but in this article, we will learn how to find and print the length of the string using pointers in C. The easiest method to find the length of a string using pointers is by calculating difference between the pointers to the first and last character o
    2 min read
  • C Program to Print the Length of a String Using %n Format Specifier
    In C, strings are arrays of characters terminated by a null character ('\0') and the length of a string is the number of characters before this null character. In this article, we will learn how to find the length of the string using %n format specifier. The %n is a special format specifier for prin
    1 min read
  • How to Find the Range of Numbers in an Array in C?
    The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra
    2 min read
  • C Program For Double to String Conversion
    To convert double to string in C language, we will use the sprintf function as follows: Input: n = 456321.7651234 Output: string: 456321.7651234 Method: Using sprintf By specifying the precision in sprintf, we can convert double to string or character array with custom precision. We can use sprintf
    1 min read
  • Program to find out the data type of user input
    Take a input from user and find out the data type of input value. Examples : Input : geek Output : The input is a string Input : chetna Output : The input is a string Input : 4 Output : The input is a integer Below is C program to find the datatype of user input : // C program to find data type #inc
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences