• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 5 pages
Other

C Programming: Variables, Data Types, and Their Uses

Document preview thumbnail
Preview 2 out of 5 pages

This document provides an in-depth introduction to variables and data types in C programming. Learn about the different types of variables in C, including int, float, char, and double, and how to declare and initialize them. This guide also explains type casting, constants, and how to choose the right data type for your programs. Perfect for first-year Computer Science students and those new to C programming, these notes will help you build a strong foundation in handling data in C.

Content preview

Variables and Data Types in C
In C, variables are used to store data, and data types define what kind of data can
be stored in these variables. Understanding variables and data types is
fundamental for creating well-structured and efficient programs.

1. Variables in C
A variable is a named storage location in memory that holds a value. The value
can be modified during program execution.

Declaration and Initialization

To use a variable, you must first declare it with its data type. A variable can also
be initialized at the time of declaration.

int number; // Declaration
number = 10; // Initialization

// Declaration and Initialization in one line
int age = 25;

 The name of a variable must start with a letter (either lowercase or
uppercase) or an underscore (_ and must only contain alphanumeric
characters and underscores).
 C is case-sensitive, so age and Age are considered different variables.



2. Data Types in C
C has a variety of data types to store different kinds of values. The main data
types can be classified into the following categories:

Primary Data Types:

1. int:
o Used to store integers (whole numbers).

, o Size: Typically 4 bytes (32 bits) on most systems.
o Example: int age = 25;
2. float:
o Used to store floating-point numbers (numbers with decimal points).
o Size: Typically 4 bytes.
o Example: float temperature = 36.6;
3. double:
o Used to store double-precision floating-point numbers (larger
decimal values).
o Size: Typically 8 bytes.
o Example: double pi = 3.14159265359;
4. char:
o Used to store a single character (like a, b, or 1).
o Size: Typically 1 byte.
o Example: char grade = 'A';


Derived Data Types:

1. Arrays:
o An array is a collection of variables of the same type, stored in
contiguous memory locations.
o Example: int numbers[5] = {1, 2, 3, 4, 5};
2. Pointers:
o A pointer is a variable that stores the memory address of another
variable.
o Example: int *ptr; (pointer to an integer)
3. Structures:
o A structure is a collection of variables of different data types grouped
together.
o Example:


struct Person {
char name[50];
int age;
};

Document information

Uploaded on
January 20, 2025
Number of pages
5
Written in
2024/2025
Type
Other
Person
Unknown
$5.19

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and smashed it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions