Lecture 2
42 Questions
0 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

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?

  • 20 (correct)
  • 5
  • 15
  • 10

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?

<p>Literals are constant values that are directly embedded in the code. (B)</p> Signup and view all the answers

Which of the following demonstrates the correct usage of the #define preprocessor directive for creating a literal string in C?

<p><code>#define STRING_LITERAL &quot;example&quot;</code> (A)</p> Signup and view all the answers

Which of the following statements best describes the difference between using #define and const for defining constants in C?

<p><code>const</code> creates a variable in memory that cannot be changed, while <code>#define</code> performs text substitution during preprocessing. (D)</p> Signup and view all the answers

Which of the following data types can store the largest positive integer value?

<p>unsigned long int (D)</p> Signup and view all the answers

If a variable of type char is assigned the value 65, what will it represent?

<p>The character 'A' (C)</p> Signup and view all the answers

Given the declaration int a = 014;, what is the decimal value of a?

<p>12 (B)</p> Signup and view all the answers

What is the primary difference between signed char and unsigned char?

<p><code>signed char</code> can represent negative numbers, while <code>unsigned char</code> cannot (B)</p> Signup and view all the answers

What is the primary purpose of the sizeof operator in C?

<p>To calculate the memory space, in bytes, occupied by a data type or variable. (C)</p> Signup and view all the answers

Consider the following code snippet:

const float pi = 3.14159; pi = 3.14;

What will happen when this code is compiled?

<p>The code will not compile due to an attempt to modify a <code>const</code> variable. (A)</p> Signup and view all the answers

Assuming a system where int is represented using 4 bytes, what is the range of values that a signed int can hold?

<p>-2147483648 to 2147483647 (B)</p> Signup and view all the answers

Which data type is most suitable for storing decimal numbers with a high degree of precision?

<p>double (A)</p> Signup and view all the answers

Which declaration will reserve the largest amount of memory?

<p>It depends on the compiler (B)</p> Signup and view all the answers

How many distinct values can an unsigned char variable represent?

<p>256 (C)</p> Signup and view all the answers

Which of the following is a characteristic of the float data type?

<p>It has a smaller range than <code>double</code> (B)</p> Signup and view all the answers

What is the amount of memory used by long double data type?

<p>10 bytes (A)</p> Signup and view all the answers

Which of the following is NOT a type of C statement?

<p>Compilation (C)</p> Signup and view all the answers

What is the primary purpose of a data type in C?

<p>To define how data values are represented and operated. (D)</p> Signup and view all the answers

Which of the following is NOT a primary data type in C?

<p>struct (C)</p> Signup and view all the answers

Which of the following best describes the execution starting point of a C program?

<p>The <code>main()</code> function. (D)</p> Signup and view all the answers

Modular arithmetic operation (%) is defined for which of the following data types in C?

<p>int (D)</p> Signup and view all the answers

Considering platform dependency, which of the following statements is most accurate regarding the int data type size in C?

<p>The size of <code>int</code> can vary depending on the system architecture. (D)</p> Signup and view all the answers

What is the correct order of sections in a typical C program structure?

<p>preprocessor directives, global variables, function declarations, main(), function definitions (A)</p> Signup and view all the answers

What does the return data type specify in a function declaration?

<p>The type of data the function will return after execution. (B)</p> Signup and view all the answers

Which element is NOT considered one of the fundamental elements of C syntax?

<p>Class (D)</p> Signup and view all the answers

What is the primary purpose of a function declaration in C?

<p>To inform the compiler about the function's existence, return type, and parameters before it is used. (C)</p> Signup and view all the answers

What is the purpose of curly braces {} in C programming related to statements?

<p>To group a sequence of statements into a block. (C)</p> Signup and view all the answers

Given the declaration char myChar;, what is the valid value range that myChar can hold?

<p>-128 to 127 (D)</p> Signup and view all the answers

In C, what is the role of 'preprocessor directives'?

<p>They are instructions to the compiler to perform certain tasks before compilation, such as including header files. (C)</p> Signup and view all the answers

Which of the following is true regarding expressions in C?

<p>They can contain a combination of constants, variables, and operators. (D)</p> Signup and view all the answers

How is a large C program typically organized into multiple files?

<p>Divided into function header files, function implementation files, and a main function file. (A)</p> Signup and view all the answers

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;
}

<p><code>15</code> (C)</p> Signup and view all the answers

What is the range of values that can be represented by an unsigned int data type?

<p>From 0 to 4294967295 (A)</p> Signup and view all the answers

Which of the following statements correctly describes how an int type is stored in memory?

<p>The most significant byte is stored in the lowest address cell in big-endian. (D)</p> Signup and view all the answers

Given the hexadecimal representation 0xAB12CD34 of an integer, how would it be stored in memory (starting from address 1000) in a little-endian system?

<p>Address 1000: 34, 1001: CD, 1002: 12, 1003: AB (B)</p> Signup and view all the answers

According to the IEEE 754 standard, which of the following data types uses 8 bytes to store floating-point numbers?

<p><code>double</code> (C)</p> Signup and view all the answers

What is the primary role of a compiler with respect to variables?

<p>Assign a memory block with a relative address to the variable. (B)</p> Signup and view all the answers

At which stage is a variable assigned an absolute address in memory?

<p>At runtime (D)</p> Signup and view all the answers

Which of the following best describes a variable in the context of programming?

<p>An identifier that represents a data value of specific type, allocated memory at compile time and instanced at runtime. (C)</p> Signup and view all the answers

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?

<p>0x19, 0x00, 0x00, 0x00 (D)</p> Signup and view all the answers

Flashcards

Variable Declaration

Declares a variable's type and name within a specific region of code where it is valid.

Variable Assignment

Assigns a specific value to a variable, storing that value in the reserved memory location.

Variable Scope

The region of code where a variable can be accessed and used.

Global Variables

Variables declared outside of any function or block, accessible throughout the entire program.

Signup and view all the flashcards

Literals

Constant values directly embedded in the source code.

Signup and view all the flashcards

#define PI

A preprocessor directive that replaces PI with 3.1415926 before compilation.

Signup and view all the flashcards

char c;

Allocates a 1-byte memory space for storing a single character.

Signup and view all the flashcards

sizeof operator

It returns the size, in bytes, of a data type or variable.

Signup and view all the flashcards

Constants (const)

Variables with values that cannot be changed after initialization.

Signup and view all the flashcards

int a = 12;

Declares an integer variable 'a' and initializes it with the value 12.

Signup and view all the flashcards

char

A data type representing a single character.

Signup and view all the flashcards

unsigned char

A char that cannot represent negative values, 0 to 255.

Signup and view all the flashcards

signed char

A char that can represent negative values, -128 to 127.

Signup and view all the flashcards

int

Stores whole numbers (integers).

Signup and view all the flashcards

unsigned int

An int that cannot represent negative values. Range: 0 to 2^32 - 1

Signup and view all the flashcards

Float

Stores single-precision floating point numbers.

Signup and view all the flashcards

double

Stores double-precision floating point numbers.

Signup and view all the flashcards

Sign bit

The leftmost bit in an int data type. 0 positive // 1 negative

Signup and view all the flashcards

C Statement

A command or instruction given to the C compiler.

Signup and view all the flashcards

Program Block

Groups of statements enclosed in curly braces {}.

Signup and view all the flashcards

Function Declaration

Specifies the data type returned by a function, the function's name, and the types of arguments it accepts.

Signup and view all the flashcards

Function Definition

Contains the actual code that the function executes.

Signup and view all the flashcards

Function Call

Invoking a function to execute its code.

Signup and view all the flashcards

Data Type

Defines how data values are represented, how much memory they occupy, and the operations that can be performed on them.

Signup and view all the flashcards

Primary Data Types

Basic data types provided by C, such as char, int, float, and double.

Signup and view all the flashcards

Derived Data Types

Data types created from primary data types using keywords like typedef, struct, union, and enum.

Signup and view all the flashcards

C Program Structure

A C program is structured as a sequence of functions. Execution starts from the main() function.

Signup and view all the flashcards

Preprocessor Directives

Directives that provide instructions to the preprocessor, often used for including header files.

Signup and view all the flashcards

main() Function

The entry point of a C program; execution starts here.

Signup and view all the flashcards

Keywords in C

Reserved identifiers with special meanings in the C language.

Signup and view all the flashcards

Expression

A combination of operators, operands (variables, constants), and parentheses that yields a value.

Signup and view all the flashcards

Int (Integer)

A data type that stores whole numbers, both positive and negative, within a specific range.

Signup and view all the flashcards

Unsigned Int Range

The range of values an unsigned int can store, from 0 to 4294967295 (2^32 - 1).

Signup and view all the flashcards

Big-Endian

Stores the most significant byte at the lowest memory address.

Signup and view all the flashcards

Little-Endian

Stores the least significant byte at the lowest memory address.

Signup and view all the flashcards

Float Type

A single-precision floating-point number, using 4 bytes, as defined by the IEEE 754 standard.

Signup and view all the flashcards

Double Type

A double-precision floating-point number, using 8 bytes, as defined by the IEEE 754 standard.

Signup and view all the flashcards

Variable

A named identifier representing a data value of a specific type in a program.

Signup and view all the flashcards

Variable Allocation (Compile Time)

The process where a variable is assigned a memory block with a relative address by the compiler.

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 included
  • int a is a declaration of a global variable
  • int add (int, int) is a function declaration
  • int main() is the start of block function defintion
  • a=1 assigns the value of 1 to a
  • printf("a+b=%d\n", add(a, b)) is a function, that when, called outputs a+b=3 to terminal
  • return 0 indicates the end of block function definition
  • return x+y is the function body for the add(int x, int y) function
  • return x-y is the function body for the minus(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, and enum
  • 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 c
  • int a assigns 4 byte memory spaces for int variables a
  • float f assigns value by generating instruction that stores 2 to a's memory at runtime
  • c = 'a' compilers instruction that stores 97(base10) to memory of c
  • f = 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 = 12
  • int a = 014; initializes a = 014 for Oct 14
  • int a = 0xC; initializes a = 0xc for Hex_ number C
  • char c = 'a'; Initializing a char variable with a char value
  • float 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 1
  • sizeof(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.

Quiz Team

Related Documents

C Basics Lecture Notes PDF

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.

More Like This

Use Quizgecko on...
Browser
Browser