Podcast
Questions and Answers
Which of the following is the most accurate description of what occurs when a variable is declared in C?
Which of the following is the most accurate description of what occurs when a variable is declared in C?
- The compiler reserves a memory block and associates it with a specific address.
- The compiler reads a value from memory and stores it in a register for later use.
- The compiler generates instructions to write a value to a specific memory block.
- The compiler assigns a symbolic name to a memory location and determines its size based on the variable's data type. (correct)
Consider the following C code snippet:
int global_var = 10;
int main() {
int local_var = 5;
{
int local_var = 20;
global_var = local_var;
}
return 0;
}
What is the value of global_var
after the main
function is executed?
Consider the following C code snippet:
int global_var = 10;
int main() {
int local_var = 5;
{
int local_var = 20;
global_var = local_var;
}
return 0;
}
What is the value of global_var
after the main
function is executed?
- 20 (correct)
- 5
- 15
- 10
Which statement accurately describes the behavior of variable scope in C?
Which statement accurately describes the behavior of variable scope in C?
- A variable declared in an outer scope is accessible in inner scopes unless shadowed by a variable of the same name in the inner scope. (correct)
- Variables declared in an inner scope can only be accessed after the inner scope ends.
- Redeclaring a variable with the same name in an inner scope will result in a compiler error.
- Variables declared within a function are global and accessible throughout the program.
Which of the following is true about literals in C?
Which of the following is true about literals in C?
Which of the following demonstrates the correct usage of the #define
preprocessor directive for creating a literal string in C?
Which of the following demonstrates the correct usage of the #define
preprocessor directive for creating a literal string in C?
Which of the following statements best describes the difference between using #define
and const
for defining constants in C?
Which of the following statements best describes the difference between using #define
and const
for defining constants in C?
Which of the following data types can store the largest positive integer value?
Which of the following data types can store the largest positive integer value?
If a variable of type char
is assigned the value 65, what will it represent?
If a variable of type char
is assigned the value 65, what will it represent?
Given the declaration int a = 014;
, what is the decimal value of a
?
Given the declaration int a = 014;
, what is the decimal value of a
?
What is the primary difference between signed char
and unsigned char
?
What is the primary difference between signed char
and unsigned char
?
What is the primary purpose of the sizeof
operator in C?
What is the primary purpose of the sizeof
operator in C?
Consider the following code snippet:
const float pi = 3.14159;
pi = 3.14;
What will happen when this code is compiled?
Consider the following code snippet:
const float pi = 3.14159;
pi = 3.14;
What will happen when this code is compiled?
Assuming a system where int
is represented using 4 bytes, what is the range of values that a signed int
can hold?
Assuming a system where int
is represented using 4 bytes, what is the range of values that a signed int
can hold?
Which data type is most suitable for storing decimal numbers with a high degree of precision?
Which data type is most suitable for storing decimal numbers with a high degree of precision?
Which declaration will reserve the largest amount of memory?
Which declaration will reserve the largest amount of memory?
How many distinct values can an unsigned char
variable represent?
How many distinct values can an unsigned char
variable represent?
Which of the following is a characteristic of the float
data type?
Which of the following is a characteristic of the float
data type?
What is the amount of memory used by long double
data type?
What is the amount of memory used by long double
data type?
Which of the following is NOT a type of C statement?
Which of the following is NOT a type of C statement?
What is the primary purpose of a data type in C?
What is the primary purpose of a data type in C?
Which of the following is NOT a primary data type in C?
Which of the following is NOT a primary data type in C?
Which of the following best describes the execution starting point of a C program?
Which of the following best describes the execution starting point of a C program?
Modular arithmetic operation (%) is defined for which of the following data types in C?
Modular arithmetic operation (%) is defined for which of the following data types in C?
Considering platform dependency, which of the following statements is most accurate regarding the int
data type size in C?
Considering platform dependency, which of the following statements is most accurate regarding the int
data type size in C?
What is the correct order of sections in a typical C program structure?
What is the correct order of sections in a typical C program structure?
What does the return data type specify in a function declaration?
What does the return data type specify in a function declaration?
Which element is NOT considered one of the fundamental elements of C syntax?
Which element is NOT considered one of the fundamental elements of C syntax?
What is the primary purpose of a function declaration in C?
What is the primary purpose of a function declaration in C?
What is the purpose of curly braces {}
in C programming related to statements?
What is the purpose of curly braces {}
in C programming related to statements?
Given the declaration char myChar;
, what is the valid value range that myChar
can hold?
Given the declaration char myChar;
, what is the valid value range that myChar
can hold?
In C, what is the role of 'preprocessor directives'?
In C, what is the role of 'preprocessor directives'?
Which of the following is true regarding expressions in C?
Which of the following is true regarding expressions in C?
How is a large C program typically organized into multiple files?
How is a large C program typically organized into multiple files?
Given the following code snippet, what will be the output?
int a = 5;
int add(int x, int y) {
return x + y;
}
int main() {
int b = 10;
printf("%d\n", add(a, b));
return 0;
}
Given the following code snippet, what will be the output?
int a = 5;
int add(int x, int y) {
return x + y;
}
int main() {
int b = 10;
printf("%d\n", add(a, b));
return 0;
}
What is the range of values that can be represented by an unsigned int
data type?
What is the range of values that can be represented by an unsigned int
data type?
Which of the following statements correctly describes how an int
type is stored in memory?
Which of the following statements correctly describes how an int
type is stored in memory?
Given the hexadecimal representation 0xAB12CD34
of an integer, how would it be stored in memory (starting from address 1000) in a little-endian system?
Given the hexadecimal representation 0xAB12CD34
of an integer, how would it be stored in memory (starting from address 1000) in a little-endian system?
According to the IEEE 754 standard, which of the following data types uses 8 bytes to store floating-point numbers?
According to the IEEE 754 standard, which of the following data types uses 8 bytes to store floating-point numbers?
What is the primary role of a compiler with respect to variables?
What is the primary role of a compiler with respect to variables?
At which stage is a variable assigned an absolute address in memory?
At which stage is a variable assigned an absolute address in memory?
Which of the following best describes a variable in the context of programming?
Which of the following best describes a variable in the context of programming?
Consider an int
variable x
with a value of 25 (0x19
in hexadecimal). If x
is stored at memory address 0x1000 in a little-endian system, what would the bytes at addresses 0x1000, 0x1001, 0x1002 and 0x1003 represent?
Consider an int
variable x
with a value of 25 (0x19
in hexadecimal). If x
is stored at memory address 0x1000 in a little-endian system, what would the bytes at addresses 0x1000, 0x1001, 0x1002 and 0x1003 represent?
Flashcards
Variable Declaration
Variable Declaration
Declares a variable's type and name within a specific region of code where it is valid.
Variable Assignment
Variable Assignment
Assigns a specific value to a variable, storing that value in the reserved memory location.
Variable Scope
Variable Scope
The region of code where a variable can be accessed and used.
Global Variables
Global Variables
Signup and view all the flashcards
Literals
Literals
Signup and view all the flashcards
#define PI
#define PI
Signup and view all the flashcards
char c;
char c;
Signup and view all the flashcards
sizeof operator
sizeof operator
Signup and view all the flashcards
Constants (const)
Constants (const)
Signup and view all the flashcards
int a = 12;
int a = 12;
Signup and view all the flashcards
char
char
Signup and view all the flashcards
unsigned char
unsigned char
Signup and view all the flashcards
signed char
signed char
Signup and view all the flashcards
int
int
Signup and view all the flashcards
unsigned int
unsigned int
Signup and view all the flashcards
Float
Float
Signup and view all the flashcards
double
double
Signup and view all the flashcards
Sign bit
Sign bit
Signup and view all the flashcards
C Statement
C Statement
Signup and view all the flashcards
Program Block
Program Block
Signup and view all the flashcards
Function Declaration
Function Declaration
Signup and view all the flashcards
Function Definition
Function Definition
Signup and view all the flashcards
Function Call
Function Call
Signup and view all the flashcards
Data Type
Data Type
Signup and view all the flashcards
Primary Data Types
Primary Data Types
Signup and view all the flashcards
Derived Data Types
Derived Data Types
Signup and view all the flashcards
C Program Structure
C Program Structure
Signup and view all the flashcards
Preprocessor Directives
Preprocessor Directives
Signup and view all the flashcards
main()
Function
main()
Function
Signup and view all the flashcards
Keywords in C
Keywords in C
Signup and view all the flashcards
Expression
Expression
Signup and view all the flashcards
Int (Integer)
Int (Integer)
Signup and view all the flashcards
Unsigned Int Range
Unsigned Int Range
Signup and view all the flashcards
Big-Endian
Big-Endian
Signup and view all the flashcards
Little-Endian
Little-Endian
Signup and view all the flashcards
Float Type
Float Type
Signup and view all the flashcards
Double Type
Double Type
Signup and view all the flashcards
Variable
Variable
Signup and view all the flashcards
Variable Allocation (Compile Time)
Variable Allocation (Compile Time)
Signup and view all the flashcards
Study Notes
- Lecture 2 covers C basics
- C program structure
- Syntax
- Data types
- Variables
C Program Structure
- C source code programs are organized as a sequence of functions
- A function contains a logical sequence of statements
- A statement may call another function that must be declared or defined before being called
- Executable programs must contain the
main()
function - Program execution will always begin from the
main()
function - Executable programs are organized as a sequence of function blocks of machine code
C Program Model Structure
- The model incudes:
- preprocessor directives
- global variables
- function declarations
- main() with arguments and statements
- function definitions
C Program Example
stdio.h
is a preprocessor directive includedint a
is a declaration of a global variableint add (int, int)
is a function declarationint main()
is the start of block function defintiona=1
assigns the value of 1 to aprintf("a+b=%d\n", add(a, b))
is a function, that when, called outputsa+b=3
to terminalreturn 0
indicates the end of block function definitionreturn x+y
is the function body for theadd(int x, int y)
functionreturn x-y
is the function body for theminus(int x, int y)
function- A large C program is decomposed into function header files, header implementation files, and a main function file (aka driver program file)
Basic Syntax
- C has 5 types of elements: symbol, keyword, expression, statement, function
Basic Symbols
//
lines indicate comments that compilers will ignore/* ... */
blocks indicate comments that compilers will ignore#
indicates the preprocessor;
terminates a statement,
separates items in a list- Parenthesis
()
denotes a function's argument and parameter list, and also groups algebraic expressions - Curly brackets
{}
define the scope of a program block
Keywords
- C has 32 reserved keywords
- Basic data types (9):
char
,int
,float
,double
,short
,long
,signed
,unsigned
,void
- Define data types (4):
typedef
,struct
,union
,enum
- Modifiers (6):
const
,auto
,static
,extern
,volatile
,register
- Flow control (11):
if
,else
,switch
,case
,default
,goto
,for
,while
,do
,break
,continue
- Function (2):
return
,sizeof
Expressions
- Uses infix notation, consisting of constants, variables, operators, and parenthesis
- Example
(1 + 2) * 3
,1==2
,(1==1) && (2!=1)
Statements
- A statement is a command or instruction to the C compiler
- Declaration, assignment, condition, function call, flow control are statement types
- Statements are organized into blocks (program blocks), a sequence of statements scoped by
{}
.
Function Syntax
- The function declaration/header syntax is
returndatatype function_name(argument type list);
- The function defintion/implementation synthax is
returndatatype function_name(argument type and name list) {}
- The function call syntax is
function_name(parameter list)
Data Types
- A data type defines how a type of data value is represented in programming and computers.
- Data types also defines how many bytes and what bit pattern are used to represent the value in memory
- Data Types also defines what and how operations are applied to the data values in programming and computers.
- Data types defines how data values are represented and operated in programming and computers
Primary Data Types
- C provides primary data types (primitive, basic data types) which are defined by keywords
- Keywords are
char
,int
,short
,long
,float
,double
,signed
,unsigned
- Arithmetic operations (+, -, *, /) are defined for primary types
- The modular operation
%
is defined for non-floating primary datatypes - Each of the primary data types has a corresponding bit pattern in representation and operations
Secondary Data Types
- C provides methods to build secondary data types (derived, extended) using primary data types and the keywords
typedef
,struct
,union
, andenum
- Each data type has a size, i.e., the number of bytes to stores the value of the type in memory
- Each data type has a defined valid value range
- The size of some data types is platform dependent
Data Type Size Examples
- char type has size 1, using 1 byte (or 8 bits), with a value range from -128 to 127
- int type has 2 bytes in old 16 systems but has 4 bytes in 32 or 64-bit systems
- The default size for the int type is 4 bytes
Size and Range of Datatypes
DATA TYPE / Keyword | SIZE IN BYTES | RANGE |
---|---|---|
char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
signed char | 1 | -128 to 127 |
int | 4 (2) | -231+1 to +231-1 (-32768 to 32767) |
unsigned int | 4 (2) | 0 to 232-1 (0 to 65535) |
signed short int | 4 (2) | -231+1 to +231-1 (-32768 to 32767) |
signed int | 4 (2) | -231+1 to +231-1 (-32768 to 32767) |
short int | 2 (4) | -32768 to 32767 (-231+1 to +231-1) |
unsigned short int | 4 (2) | 0 to 232-1 (0 to 65535) |
long int | 4 (8) | -231+1 to +231-1 (-263+1 to +263-1) |
unsigned long int | 8 (4) | 0 to 264-1 (0 to 4294967295) |
signed long int | 8 (4) | -263+1 to +263 (-2147483648 to 2147483647) |
float | 4 | 3.4E-38 to 3.4E+38 |
double | 8 | 1.7E-308 to 1.7E+308 |
long double (C99) | 10 | 3.4E-4932 to 1.1E+4932 |
Char Data Type
- Presents characters by an integer defined by the American Standard Code for Information Interchange (ASCII)
- i.e, A is coded as 65, a is coded as 97, 0 is coded as 48.
- Each addressable memory cell holds 8 bits (1 byte)
- char type size is 1 byte and uses one addressable memory cell
- The ASCII code of character A is 65
- 65 in a binary representation is 100 0001
- A is stored in memory cell as 0100 0001
Int and Unsigned Int Variables
- int type value ranges from -2147483647 = -2^31 +1 to 2147483647 = 2^31 - 1
- int type bit pattern uses 4 bytes (32-bits)
- the leftmost bit represents the sigh: 0 for positive and 1 for negative
- The rest of the bits (31 bits) represent the absolute value in base 2 (binary format)
- unsigned int type value ranges from 0 to 4294967295 = 2^32 -1
- unsigned int type uses a bit pattern of 4 bytes or 32 bits, with 32 bites that represent the value in base 2
How Int Types are Stored
- Data types sized greater than 1 require a contiguous memory cells (called memory block) to store the value of the type
- Big-endian stores the most significant byte in the lowest addres cell
- Little-endian stores the least significant byte in the lowest address cell.
- Little-endian is commonly used
- int type size is 4, which utilizes and needs 4 memory cells
Float and Double Types
- float uses 4 bytes for single precision floating point numbers
- The bit pattern and operations are specified by IEEE 754 standard
- double uses 8 bytes for double precision floating point numbers
- The bit pattern and are specied by IEEE 754 standard
Variables
- A variable is a name identifier used in a source code program to represent a data value of a certain type
- A variable is assigned a memory block with relative address by the compiler
- Instructions are used to set and to get the values to a memory address
- A variable is instanced at runtime with an absolute address of a memory block
- A Variable is an identifier of a data value in a program that is allocated relative memory during compile time and actual memory blocks at runtime
C Variables
- The must be declared with a type and name in a scope before being used within the scope
- The variable declaration tells the compiler to assign a memory block with a relative address
- A variable assignment statment tells compilers to generate instructiongs for writing values to the memory block
- Using a variable tells compilers in an expression to generate instructions to read values from the memory block
- Variables should be initiated before used in expressions
- Variable names must start with a letter, and can be followed by letters, underscores, and numbers
- C variable names are case-sensitive
Variable Scope and Declaration
- Variables have to be desclared before a variable is used
- A scope is the area where a variable is declared
- Scopes can be nested such that one cope can be an inner scope of another
- A Variable declared before an innder scope can be used in the inner scope and vice versa.
- Types of Variablies:
- Global declared ouside ant scope, usable anywhere
- Local declared in a scope block embraced by {}, only usable in the scope block, i.e. in a function
Literals
- Refer to those constant values assigned to variables in programming
- The compiler recognizes the data types of a literal and convents to its bit patterns
- Pre-processors (#define) define a literal string via macro
- Macros can be used during preprocessing steps, being then replaced by their corresponding string
#define PI 3.1415926
float r = 4;
float area = PI*r*r;
float cf = 2*PI*r;
float f = 2.4e-5; // 2.4e-5 = 0.000024
Variable Assignments
char c
assigns 1 byte memory space for char variable cint a
assigns 4 byte memory spaces for int variables afloat f
assigns value by generating instruction that stores 2 to a's memory at runtimec = 'a'
compilers instruction that stores 97(base10) to memory of cf = 1.41
compiler convert 1.41 to a 31 bit single precision number and store at f's memory
Initializing Variables
int a = 12;
initializes int a = 12int a = 014;
initializes a = 014 for Oct 14int a = 0xC;
initializes a = 0xc for Hex_ number Cchar c = 'a';
Initializing a char variable with a char valuefloat f = 1.41;
, list separated by a comma is variables are of the same type
Sizeof Operator
- The sizeof() Operator is an unary operator used to get the sizes of data types, and can determine the memory size of a variable
- For example
sizeof(char)
returns 1sizeof(a)
returns 4, the size of int type
Constants
- Constants are fixed data values in a program, using a constant variable to present the values
- Constant varibles are declared and initialized by the const keyword
- The compiler does not allow changing values of a constant variable that has been declared and initialized
- i.e.,
const float pi = 3.1415926;
- Compiler prevents assigning value to 3.14, like
pi = 3.14
after declaring the value with const keyword
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore variable declaration, scope, and literals in C. Understand the use of #define, const, and data types, including char and int. Practice examples and learn about integer representation.