Podcast
Questions and Answers
What is a variable in programming?
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 _________.
The declaration syntax for a variable is: data_type _________.
variable_name
What must every variable do before it is used?
What must every variable do before it is used?
Be declared
A variable can store a value of any data type without declaration.
A variable can store a value of any data type without declaration.
Signup and view all the answers
Which data type is used for floating-point numbers?
Which data type is used for floating-point numbers?
Signup and view all the answers
How many bytes does a double data type typically occupy?
How many bytes does a double data type typically occupy?
Signup and view all the answers
What is the prefix for an octal number?
What is the prefix for an octal number?
Signup and view all the answers
What does the sizeof() operator return?
What does the sizeof() operator return?
Signup and view all the answers
Constants can be modified during program execution.
Constants can be modified during program execution.
Signup and view all the answers
What are special backslash character constants?
What are special backslash character constants?
Signup and view all the answers
Which of the following is a correct way to define a constant?
Which of the following is a correct way to define a constant?
Signup and view all the answers
In the function scanf(), the variable arguments are preceded by the ________ operator.
In the function scanf(), the variable arguments are preceded by the ________ operator.
Signup and view all the answers
What format specifier is used for reading an integer with scanf()?
What format specifier is used for reading an integer with scanf()?
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
or0X
. Example:0x2A
- Decimal: Example:
-
Floating-point: Examples:
123.45
,-0.2E-2
-
Character: Examples:
'A'
,'1'
,'&'
-
String: Example:
"Seneca"
-
Integer:
Defining Constants
-
Using the
#define
preprocessor:- Example:
-
#define PI 3.14
-
#define NEWLINE '\n'
-
- Example:
-
Using the
const
keyword:- Example:
-
const int SIZE = 100;
-
const float PI = 3.14;
-
const char NEWLINE = '\n';
-
- Example:
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
-
-
Common Format Specifiers:
-
&
(Ampersand sign): The "address of" operator, tellsscanf()
where to store the input value in memory. - Missing
&
inscanf()
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.
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.