C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Basic Data Types in C
  • 1. int (Integer)
  • 2. float
  • 3. double
  • 4. char (Character)
  • 5. _Bool (Boolean)
  • Declaring Variables
  • Constants
  • Modifiers
  • Sizeof Operator
  • Type Casting
  • size of common data types
  • Conclusion

Was this helpful?

  1. Basic Concepts

Data Types and Variables

In C programming, data types and variables are fundamental concepts that form the building blocks of your programs. Understanding how to declare and use different data types allows you to store and manipulate information effectively. In this section, we'll explore the common data types in C and how to declare variables.

Basic Data Types in C

1. int (Integer)

Used to store integer values.

int age = 25;

2. float

Used to store floating-point numbers (real numbers).

float salary = 50000.75;

3. double

Similar to float but with higher precision.

double pi = 3.1415926535;

4. char (Character)

Used to store a single character.

char grade = 'A';

5. _Bool (Boolean)

Used to store true or false values (0 or 1).

_Bool isStudent = 1;  // true

Declaring Variables

To declare a variable, specify its data type followed by the variable name. You can also initialize the variable at the time of declaration.

int count;           // Declaration
float temperature = 98.6;  // Declaration and initialization

Constants

Constants are values that do not change during the execution of a program. In C, you can use the const keyword to define constants.

const int MAX_VALUE = 100;

Modifiers

Modifiers such as short, long, signed, and unsigned can be used with basic data types to modify their range or behavior.

short smallNumber = 10;
unsigned long bigPositiveNumber = 4294967295;

Sizeof Operator

The sizeof operator returns the size, in bytes, of a data type or a variable.

int size = sizeof(int);

Type Casting

Type casting allows you to convert a value from one data type to another.

float average;
int sum = 105;
int count = 5;

average = (float)sum / count;

size of common data types

Here's a table showing the size of common data types in C programming, along with their typical ranges:

Data Type
Size (in bytes)
Typical Range

int

4

-2,147,483,648 to 2,147,483,647

float

4

1.2E-38 to 3.4E38 (6 decimal places)

double

8

2.3E-308 to 1.7E308 (15 decimal places)

char

1

-128 to 127 or 0 to 255

_Bool

1

0 to 1

short

2

-32,768 to 32,767

long

4 or 8

-2,147,483,648 to 2,147,483,647 (32-bit)

long long

8

-(2^63) to (2^63)-1

unsigned

varies

0 to 4,294,967,295 (32-bit)

unsigned long

varies

0 to 4,294,967,295 (32-bit) or (64-bit)

unsigned long long

varies

0 to 18,446,744,073,709,551,615 (64-bit)

Note: The actual size of data types may vary depending on the compiler and the system architecture. The sizes mentioned above are typical for a 32-bit or 64-bit system.

Conclusion

Data types and variables are essential components of C programming, enabling you to work with different types of information. By choosing the appropriate data type for your variables, you optimize memory usage and ensure accurate representation of values. Understanding these fundamental concepts is crucial as you progress in writing more complex and efficient C programs.

In the upcoming sections, we'll explore more advanced topics in C programming. If you have specific questions or areas you'd like to delve into further, feel free to ask. Happy coding!

PreviousEnums and CharsNextFormat Specifiers

Was this helpful?