Sonzaschool
Back

O-Level · Form 2

Computer Science

Use variables, constants, and data types of a selected programming language in a program (operators and expressions)

about 7 minutes to read

Discussions
Topics in this sectionDemonstrate an understanding of basic principles of computer programming (using an appropriate structured programming language such as C, Python, etc.)7 topics

Variables, Constants, and Data Types in C Programming

A variable is a named storage location in memory that holds a value which can change during program execution, while a constant is a value that remains fixed throughout the program. In C programming, we use different data types to specify what kind of value a variable can store, such as whole numbers, decimal numbers, or single characters.

A variable is like a labelled box in the computer's memory where we store data. Each variable has a name that we use to find and use it.

Declaring a Variable

Before using a variable, we must declare it by specifying its data type and name:

int age;        // Declares an integer variable named 'age'
float height;   // Declares a float variable named 'height'
char grade;     // Declares a character variable named 'grade'

Initialising a Variable

Initialising means giving a variable a value when we declare it:

int age = 16;           // Declares and initializes 'age' with 16
float height = 5.9;     // Declares and initializes 'height' with 5.9
char grade = 'A';       // Declares and initializes 'grade' with 'A'

Updating a Variable

We can change the value of a variable after it has been declared:

int age = 15;   // age is 15
age = 20;       // Now age is updated to 20

Question

Which type of variable in C is only accessible within the function where it is declared?

Sign in to submit your answer and have it count toward your mastery.

Sign in to practice

Tutor

Stuck? Ask me anything about this topic.

Sign in to ask the Sonza AI Tutor about this question.

Sign in to ask

Discussions

No questions yet

Sign in to ask
Loading discussions…