Introduction to Variables in Programming
13 Questions
3 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is a variable in programming?

A named storage location that stores a value of a particular data type.

The declaration syntax for a variable is: data_type _________.

variable_name

What must every variable do before it is used?

Be declared

A variable can store a value of any data type without declaration.

<p>False</p> Signup and view all the answers

Which data type is used for floating-point numbers?

<p>float</p> Signup and view all the answers

How many bytes does a double data type typically occupy?

<p>8 bytes</p> Signup and view all the answers

What is the prefix for an octal number?

<p>0</p> Signup and view all the answers

What does the sizeof() operator return?

<p>The number of bytes required for a data type</p> Signup and view all the answers

Constants can be modified during program execution.

<p>False</p> Signup and view all the answers

What are special backslash character constants?

<p>Characters like '\n' that represent special characters.</p> Signup and view all the answers

Which of the following is a correct way to define a constant?

<p>Both A and B</p> Signup and view all the answers

In the function scanf(), the variable arguments are preceded by the ________ operator.

<p>&amp;</p> Signup and view all the answers

What format specifier is used for reading an integer with scanf()?

<p>%d</p> Signup and view all the answers

Study Notes

Variables

  • A variable is a memory location with a name that holds a specific data type value.
  • Syntax for declaring a variable: data_type variable_name;
  • Multiple variables of the same data type can be declared in one statement separated by commas. Example: int number1, number2, sum;
  • Variables can be initialized during declaration using the assignment operator (=). Example: int number1 = 1;
  • Every variable must be declared before use.
  • Variables can only store values of their specified data type.
  • A variable's value can change throughout program execution.
  • Every declaration statement must end with a semicolon (;).

Variable Names

  • Variable names follow the same rules as identifiers.

Data Types

  • Data types are classifications of data that indicate the type and size of information a variable can hold.
  • Basic (Primitive/Primary) Data Types:
    • Character (char): Stores a single character (1 byte).
    • Integer (int): Stores whole numbers (2 or 4 bytes).
    • Floating-point (float): Single precision floating-point numbers (4 bytes).
    • Double precision floating-point (double): Double precision floating-point numbers (8 bytes).
    • Void (void): Represents the absence of a type.
  • Derived Data Types: Combinations of basic data types, used to represent more complex data structures.
    • Array: Stores collections of elements of the same data type.
    • Pointer: Holds the memory address of another variable.
    • Structure: Groups variables of different data types under a single name.
  • User-defined Data Types: Data types created by the programmer.
    • Union: Allows different data types to occupy the same memory location.
    • Enumeration (enum): Defines a set of named integer constants.
    • Function: Represents a reusable block of code.

Data Type Modifier Keywords

  • signed, unsigned, short, long: Used to modify the range and size of basic data types.

Data Type Sizes and Ranges

  • Data Type: Keyword: Storage Size: Value Range:
  • Character:* | char | 1 byte | -128 to 127 or 0 to 255

  • Unsigned Character:* | unsigned char | 1 byte | 0 to 255

  • Signed Character:* | signed char | 1 byte | -128 to 127

  • Integer:* | int | 2 or 4 bytes | -32,768 to 32,767 OR -2,147,483,648 to 2,147,483,647

  • *Unsigned Integer: **| unsigned int | 2 or 4 bytes | 0 to 65,535 OR 0 to 4,294,967,295

  • Short Integer:* | short int | 2 bytes | -32,768 to 32,767

  • Unsigned Short Integer:* | unsigned short int | 2 bytes | 0 to 65,535

  • Long Integer:* | long int | 4 bytes | -2,147,483,648 to 2,147,483,647

  • Unsigned Long Integer:* | unsigned long int | 4 bytes | 0 to 4,294,967,295

  • Float:* | float | 4 bytes | -1.2E-38 to 3.4E+38 (Precision: 6 decimal places)

  • Double:* | double | 8 bytes | 2.3E-308 to 1.7E+308 (Precision: 15 decimal places)

  • Long Double:* | long double | 10 bytes | 3.4E-4932 to 1.1E+4932 (Precision: 19 decimal places)

  • Data type sizes and ranges can vary depending on the compiler.

  • The sizeof() operator can be used to determine the size of a data type in bytes. Example: sizeof(int) returns the size of an integer in bytes.

Constants (Literals)

  • Constants are fixed values that cannot be modified during program execution.
  • Types of Constants:
    • Integer:
      • Decimal: Example: 123
      • Octal: Using prefix 0. Example: 0123
      • Hexadecimal: Using prefix 0x or 0X. Example: 0x2A
    • Floating-point: Examples: 123.45, -0.2E-2
    • Character: Examples: 'A', '1', '&'
    • String: Example: "Seneca"

Defining Constants

  • Using the #define preprocessor:
    • Example:
      • #define PI 3.14
      • #define NEWLINE '\n'
  • Using the const keyword:
    • Example:
      • const int SIZE = 100;
      • const float PI = 3.14;
      • const char NEWLINE = '\n';

The scanf() Function

  • Reads data from the input device (usually the keyboard) and stores it in a variable.
  • Requires the stdio.h header file.
  • Syntax: scanf("format_string", &variable1, &variable2, ...);
  • format_string: specifies the data type of each variable.
    • Common Format Specifiers:
      • %d: Integer
      • %f: Float
      • %lf: Double
      • %c: Character
      • %s: String
  • & (Ampersand sign): The "address of" operator, tells scanf() where to store the input value in memory.
  • Missing & in scanf() is a common error and can cause program termination.

Example

#include <stdio.h> 
#define SIZE 10

int main() {
   const float PI = 3.14;
   const char letter = 'A';                                        
   printf("pi=%.2f\n", PI);
                                                                  
   printf("Section: %c\n", letter);
   printf("size = 2 x %d = %d\n", SIZE, 2*SIZE);                   
   return 0;                                                     
}

This program uses constants defined with both #define and const keywords. It prints the values of the constants as well as a calculation using SIZE.

Example of scanf()

int number;
printf("Enter a number: ");      
scanf("%d", &number);            

This code prompts the user to enter a number and then uses scanf() to read the value from the keyboard and store it in the number variable.

References

  • Tan, H.H., and T.B.D'Orazio. C Programming for Engineering & Computer Science. USA: WCB McGraw-Hill. 1999. Print.
  • Hock-Chuan, Chua. C programming Tutorial. Programming notes, n.d. Web. 23 Jan, 2017.
  • Tutorialspoint.com. "C Data Types." Www.tutorialspoint.com. N.p., n.d. Web. 02 Mar. 2017.
  • Tutorialspoint.com. "C Constants and Literals." Www.tutorialspoint.com. N.p., n.d. Web. 02 Mar. 2017.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Description

This quiz covers the fundamental concepts of variables, including their definitions, syntax for declaration, and initialization. It also explores variable names and the various data types used in programming. Test your knowledge on how to effectively use variables in your coding practice.

More Like This

Introduction to Variables in Programming
40 questions
JavaScript Overview and Data Types
23 questions
C# Basics Programming short answer
40 questions
Java Variable Declaration and Output
8 questions
Use Quizgecko on...
Browser
Browser