StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • B Tree
  • Contact us
  • Sign in
StudyLover C Programming: Variables 馃摝
Download
  1. C Programming
  2. Unit 1: Foundations of Problem Solving & C Language Basics
C Programming Keywords : C Programming: Data Types
Unit 1: Foundations of Problem Solving & C Language Basics

A variable is a named location in the computer's memory used to store data. Think of it as a labeled container where you can put information, retrieve it later, and change it as needed while the program is running.

Variables are the fundamental building blocks for storing and manipulating data in any C program.


Declaring and Initializing Variables

Before you can use a variable, you must declare it. A declaration tells the compiler the variable's name and the type of data it will hold.

Syntax for Declaration

data_type variable_name;

路聽聽聽聽聽聽聽聽 data_type: Specifies the type of data the variable can store (e.g., int for integers, float for floating-point numbers, char for characters).

路聽聽聽聽聽聽聽聽 variable_name: The unique name you give to the variable.

Example Declaration:

C

int studentId;聽聽聽 // Declares a variable to hold an integer

float temperature;聽 // Declares a variable to hold a floating-point number

char grade;聽聽聽聽聽聽聽聽 // Declares a variable to hold a single character

Syntax for Initialization

Initialization means assigning an initial value to a variable at the time of its declaration. It's a good practice to initialize variables to avoid them holding unpredictable "garbage" values.

data_type variable_name = value;

Example Initialization:

C

int studentId = 101;

float temperature = 98.6;

char grade = 'A';


size=2 width="100%" align=center>

Rules for Naming Variables (Identifiers)

The name you give to a variable (also known as an identifier) must follow these rules:

路聽聽聽聽聽聽聽聽 It can only contain letters (a-z, A-Z), digits (0-9), and the underscore (_) character.

路聽聽聽聽聽聽聽聽 It must begin with a letter or an underscore. It cannot start with a digit.

路聽聽聽聽聽聽聽聽 It cannot be a keyword. For example, you cannot name a variable int or for.

路聽聽聽聽聽聽聽聽 C is case-sensitive, which means age and Age are two different variables.

Valid Names

Invalid Names

Reason Invalid

age

2age

Cannot start with a digit

student_name

student-name

Hyphens are not allowed

_value

int

int is a keyword

totalSum

total Sum

Spaces are not allowed

聽


Example of Variables in a Program

This program demonstrates how to declare, initialize, use, and modify variables of different types.

C

#include <stdio.h>

聽
int main() {

聽聽聽 // 1. Declare and initialize variables of different types

聽聽聽 int age = 25;

聽聽聽 float weight = 65.5;

聽聽聽 char initial = 'R';

聽
聽聽聽 // 2. Print the initial values of the variables

聽聽聽 // %d, %f, and %c are format specifiers for int, float, and char

聽聽聽 printf("Initial age: %d\n", age);

聽聽聽 printf("Initial weight: %f kg\n", weight);

聽聽聽 printf("Initial of name: %c\n", initial);

聽聽聽 

聽聽聽聽// 3. Modify the value of the 'age' variable

聽聽聽 age = age + 1; // Increment the age by 1

聽
聽聽聽 // 4. Print the updated value

聽聽聽 printf("Age next year will be: %d\n", age);

聽
聽聽聽 return 0;

}

Code Breakdown:

1.聽聽 We declare three variables: age as an integer, weight as a floating-point number, and initial as a character, assigning them initial values.

2.聽聽 The printf() function is used to display the stored values. We use format specifiers (%d, %f, %c) to tell printf what type of data to expect.

3.聽聽 The value of the age variable is changed. This demonstrates that variables are "variable" and their contents can be modified.

4.聽聽 The new value of age is printed to show the change.

聽

C Programming Keywords C Programming: Data Types
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website