Learn int, char, float, double basic data types, how to declare the basic data types, how to use the data types in your c program for different projects and applications. Subscribe here to enable complete access to all technical articles, online tests and projects.
This article will help you to gain required knowledge to successfully complete online tests, technical projects which are required to complete online course requirements and earn course completion certificates for software jobs.
A variable occupies some space in memory. The data type that is assigned to a variable determines how many bytes of space it will occupy in memory, the range of values that can be stored within that memory space and the set of operations that can be applied to the variable.
For example int salary statement defines a variable salary. The data type int defines that it will take(2bytes or 4 bytes) of space in memory. Storing data in a variable is called intializing the variable and data stored in a variable can be printed using its name.
Data types in C can be categorized as basic data types and derived data types. Lets dicuss all the basic data types in this article.
A char data type can be declared by writing char prior to the char variable name. Examples of char variables are all numeric characters A to Z or a to z. char type is 8 bit signed or unsigned number. If nothing is mentioned before the variable name then it is counted as signed char variable.
If a character variable is to be declared as unsigned you need to write unsigned before char during declaration of the variable. Unsigned char can hold values from 0 to 255 and signed char can hold values from -128 to 127. To print a character %c notation is used to indicate just where in a line the character is to be printed. %c is called a format specifier.
An integer is a number with no fractional part. In C, an integer is never written with a decimal point. Examples are 2, –23, and 2456. Numbers such as 3.14, 0.22, and 2.000 are not integers. Integers are stored as binary numbers. The integer 7, for example, is written 111 in binary. Therefore, to store this number in an 8-bit byte, just set the first(MSB) 5 bits to 0 and the last(LSB) 3 bits to 1.
The int type is a signed or unsigned integer. If nothing is mentioned before the variable name then it is counted as signed int variable. If an integer variable is to be declared as unsigned you need to write unsigned before int during declaration of the variable. When an integer is declared as unsigned its value during initialization has to be appended by letter U.
An integer can be positive, negative, or zero. The range in possible values depends on the computer system. For some computers size of integer is 16bit(2 bytes) where as for some computers size of integer is 32bit(4 bytes). To print an integer %d notation is used to indicate just where in a line the integer is to be printed. %d is called a format specifier.
The type short int or, more briefly, short may use less storage than int , thus saving space when only small numbers are needed. Like int, short is a signed type. So a short integer can be declared like short int val or short val. A short integer value can be printed using percentage hd or hi. The type long int, or long, may use more storage than int, thus enabling you to express larger integer values. Like int, long is a signed type. %li is used to print signed long integers and %lu for unsigned long integers.
The type long long int, or long long, may use more storage than long int, thus enabling you to express larger integer values. Like int, long long is a signed type. %lli is used to print signed long long integers and %llu for unsigned long long integers.
Financial and mathematically oriented programs often make use of floating-point numbers. In C, such numbers are called type float, double, or long double. Data with decimal places are written by using floating point. Some floating-point numbers are 9.57, 3.16E7, 7.00, and 2e–8. Notice that adding a decimal point makes a value a floating-point value. So 7 is an integer type but 7.00 is a floating-point type. The notation 3.16E7 means to multiply 3.16 by 10 to the 7th power, that is, by 1 followed by 7 zeros. The 7 would be termed the exponent of 10. A variable of float data type is printed using %f format specifier.
Double has 64 bit precision for floating point number (1 bit for the sign, 11 bits for the exponent and 52 bits for the value) i.e. double has 15 decimal digits of precision. Float has 32 bit precision for floating point number ( 1-bit for the sign, 8 bits for exponent, 23 bits for the value) i.e. float has 6 decimal digits of precision.
|Data Type||Memory (bits)||Range||Format Specifier|
|short int||16||-32,768 to 32,767||%hd|
|unsigned short int||16||0 to 65,535||%hu|
|unsigned int||32||0 to 4,294,967,295||%u|
|int||32||-2,147,483,648 to 2,147,483,647||%d|
|long int||64||-2,147,483,648 to 2,147,483,647||%ld|
|unsigned long int||64||0 to 4,294,967,295||%lu|
|long long int||64||-(2^63) to (2^63)-1||%lld|
|unsigned long long int||64||0 to 18,446,744,073,709,551,615||%llu|
|signed char||8||-128 to 127||%c|
|unsigned char||8||0 to 255||%c|
You need to subscribe & logged inSubscribe
|Medium||Write a program with two files ext1.c and ext2.c to demonstrate the use of extern keyword|
|Medium||Write a program to demonstrate the use of auto/local and global variables in C programming|
|Low||Write a c program function to accept an integer number and return lsb first digit of the number.|
|High||Write a c program function to calculate the sum of all digits of an integer number.|
|Medium||Write a C program to check whether year is leap year or not using conditional/ternary operator.|
|Low||Write a C program to return the bigger number between 2 integers using conditional/ternary operator.|
|Medium||Write a c program function to accept an integer number and return msb last digit of the number.|
|High||Write a C program function to convert Decimal to Binary number system.|
|High||Write a C Program macro to check if a character is alphabetic or not.|
|Medium||Write a program to demonstrate the use of static keyword in C programming.|
|Strings in C examples|
|C programming compiler errors warnings debugging|
|Storage classes in C|
|Pointers in C examples|
|Data types in C with examples|
|Arrays in C|
|Functions in C examples|
|How to write makefile in linux for c|
|Memory layout of C program in linux|
|C standard library function fgets with example|
|strcat, strncat, strcmp, strncmp, strchr, strrchr library functions|
|C programming debugging exercises assignments solutions|
|Union in C|
|Embedded c programming novice to professional in 24 hours|
|Structure in C|
|Bit fields in C|
|State machine implementation in C example|
|strcpy strncpy in c example illustration|
|Linux important commands illustrations examples|
|Writing practical makefiles linux examples illustrations|
|Process in Linux|
|pthreads practical applications illustration in linux|
|Process practical applications illustration in linux|
|Linux os concepts internals applications in a nutshell|
|Shared library development with gcc in linux|
|I2C programming in embedded C|
|SPI protocol theory of operation|
|How UART protocol works?|
|Microcontroller fundamentals in a nutshell|
|What is pwm and how it works|
|Search more C examples|
|Search more C MCQs|
|Linux software development|
|C programming & applications|
|Embedded systems development|
|Strings and its practical applications|
|Functions and its practical applications|
|Enums, Bit fiddling, practical applications|
|Arrays, Two dimensional arrays|
|Control flow, loops, operators, Macros|
|Pointers and its practical applications|
|Variables, Variables scope, Datatypes|
|Structure, Unions, practical applications|
|Data structures, practical applications|
|Python Data Type: Dictionary & Applications|
|Python Data Type: Strings & Applications|
|Python Data Type: Lists & Applications|
|Python Data Type: Tuples, Files & Applications|
|Python Data Type: Objects, Classes & Applications|
|Python Data Type: Numbers & Applications|
|Git Version Control|
|Linux OS Commands|
|Linux Processes and Threads|
|Linux IPC Mechanisms|
|Linux OS Internals|
|LCM and HCF of numbers|
|Time and Distance|
|I2C Protocol & Applications|
|SPI Protocol & Applications|
|UART Protocol & Applications|
|Microcontrollers, Tools & Peripherals|