Podcast
Questions and Answers
In C, what is the primary role of data types in programming?
In C, what is the primary role of data types in programming?
- To specify the kind of values that can be stored and the operations that can be performed. (correct)
- To determine the size of the program's source code.
- To automatically optimize the program for different operating systems.
- To define the speed at which the program executes.
Which of the following is NOT a basic data type in C?
Which of the following is NOT a basic data type in C?
- int
- float
- string (correct)
- double
Which of the following is true regarding unsigned
integers in C?
Which of the following is true regarding unsigned
integers in C?
- They are automatically used for all integer variables.
- They can only represent non-negative values (zero and positive). (correct)
- They have a smaller range than `signed` integers.
- They can represent both positive and negative values.
What is the minimum size of a short
integer type in C, according to the standard?
What is the minimum size of a short
integer type in C, according to the standard?
If the qualifier is omitted, what is the default sign of integer types in C?
If the qualifier is omitted, what is the default sign of integer types in C?
What is the purpose of the ASCII code?
What is the purpose of the ASCII code?
How many different characters can be represented using the basic ASCII code?
How many different characters can be represented using the basic ASCII code?
In C, if two floating-point numbers are equal up to the seventh decimal place, but differ in the eighth decimal place, how are they generally considered?
In C, if two floating-point numbers are equal up to the seventh decimal place, but differ in the eighth decimal place, how are they generally considered?
Which of the following is NOT a characteristic of an expression in C?
Which of the following is NOT a characteristic of an expression in C?
Which of the following is NOT a type of operand in C?
Which of the following is NOT a type of operand in C?
What is the main characteristic of a constant in C?
What is the main characteristic of a constant in C?
If a number exceeds the maximum representable value for the unsigned long long
type, what happens in C?
If a number exceeds the maximum representable value for the unsigned long long
type, what happens in C?
If a constant integer is assigned without a suffix and its value is within the range of int
, how does the compiler typically store it?
If a constant integer is assigned without a suffix and its value is within the range of int
, how does the compiler typically store it?
How can you specify that an integer constant should be stored as unsigned long
in C?
How can you specify that an integer constant should be stored as unsigned long
in C?
How are all floating point constants treated, if they do not have a suffix?
How are all floating point constants treated, if they do not have a suffix?
What suffix is used to explicitly declare a floating point constant as type float
?
What suffix is used to explicitly declare a floating point constant as type float
?
How are character constants stored?
How are character constants stored?
Which of the following describes a character escape sequence?
Which of the following describes a character escape sequence?
What is the character that represents 'horizontal tab'?
What is the character that represents 'horizontal tab'?
What is a string literal in C?
What is a string literal in C?
Which of the following statements is true regarding variables in C?
Which of the following statements is true regarding variables in C?
What does declaring a variable achieve?
What does declaring a variable achieve?
Which of the following represents the correct syntax for declaring multiple variables of the same type in C?
Which of the following represents the correct syntax for declaring multiple variables of the same type in C?
What happens if you try to modify a variable declared with the const
qualifier?
What happens if you try to modify a variable declared with the const
qualifier?
What is a function's role?
What is a function's role?
In the expression 3.0 + sqrt(4.0)
, what will happen to value of sqrt(4.0)
?
In the expression 3.0 + sqrt(4.0)
, what will happen to value of sqrt(4.0)
?
Which of the following is NOT a standard function in the C math library?
Which of the following is NOT a standard function in the C math library?
What happens if one tries to mix integers and floats?
What happens if one tries to mix integers and floats?
What is the role of the unary minus operator?
What is the role of the unary minus operator?
What is the result of dividing two integers?
What is the result of dividing two integers?
What does the modulo operator (%) do?
What does the modulo operator (%) do?
What happens in C when an integer expression exceeds the maximum possible value for its type?
What happens in C when an integer expression exceeds the maximum possible value for its type?
What is the effect of an integer overflow?
What is the effect of an integer overflow?
What is the purpose of increment and decrement operators?
What is the purpose of increment and decrement operators?
What is the difference between prefix and postfix increment/decrement operators?
What is the difference between prefix and postfix increment/decrement operators?
What determines the order in which operations are performed in an expression?
What determines the order in which operations are performed in an expression?
Which of the following operators has lowest precedence?
Which of the following operators has lowest precedence?
What is implicit type conversion in C?
What is implicit type conversion in C?
In implicit type conversion, what type promotion will take place if there are two different operands with the types short and int?
In implicit type conversion, what type promotion will take place if there are two different operands with the types short and int?
What operator performs explicit type conversion?
What operator performs explicit type conversion?
Flashcards
¿Qué es una Variable?
¿Qué es una Variable?
A named location in memory that can store data of a specific type during program execution.
¿Qué son Tipos Enteros?
¿Qué son Tipos Enteros?
Data types that can represent whole numbers and their negatives (e.g., int, short, long).
¿Qué son Tipos Flotantes?
¿Qué son Tipos Flotantes?
Data types that can represent numbers with fractional parts (e.g., float, double).
¿Qué son Calificadores signed y unsigned?
¿Qué son Calificadores signed y unsigned?
Signup and view all the flashcards
¿Qué es el Código ASCII?
¿Qué es el Código ASCII?
Signup and view all the flashcards
¿Qué son Constantes?
¿Qué son Constantes?
Signup and view all the flashcards
¿Qué es una Constante de Tipo Cadena?
¿Qué es una Constante de Tipo Cadena?
Signup and view all the flashcards
¿Qué son Funciones Estándar de C?
¿Qué son Funciones Estándar de C?
Signup and view all the flashcards
¿Qué son Operadores?
¿Qué son Operadores?
Signup and view all the flashcards
¿Qué es Sobreflujo?
¿Qué es Sobreflujo?
Signup and view all the flashcards
¿Qué son Operadores de Incremento y Decremento?
¿Qué son Operadores de Incremento y Decremento?
Signup and view all the flashcards
¿Qué es Precedencia de Operadores?
¿Qué es Precedencia de Operadores?
Signup and view all the flashcards
¿Qué es Conversión ImplÃcita de Tipos?
¿Qué es Conversión ImplÃcita de Tipos?
Signup and view all the flashcards
¿Qué es Conversión ExplÃcita de Tipos?
¿Qué es Conversión ExplÃcita de Tipos?
Signup and view all the flashcards
¿Qué son Operandos?
¿Qué son Operandos?
Signup and view all the flashcards
¿Qué es una Expresión?
¿Qué es una Expresión?
Signup and view all the flashcards
¿Qué es una Función?
¿Qué es una Función?
Signup and view all the flashcards
¿Qué es una Llamada a la Función?
¿Qué es una Llamada a la Función?
Signup and view all the flashcards
¿Cuáles son los Tipos Enteros en C?
¿Cuáles son los Tipos Enteros en C?
Signup and view all the flashcards
¿Qué es un sobreflujo entero?
¿Qué es un sobreflujo entero?
Signup and view all the flashcards
¿Qué son tipos flotantes en C?
¿Qué son tipos flotantes en C?
Signup and view all the flashcards
Study Notes
Data Types and Expressions in C
- Programs process input data to produce output data.
- Programming languages define data types that users can employ in their programs.
- A data type defines the set of values that a data of that type can assume and the set of operations that can be performed on data of that type.
Data Types in C
- Basic data types include integer and floating-point types.
- Derived data types include arrays, pointers, structures, and unions.
- Pointers, structures and unions aren't covered in the course.
Integer Types in C
- Integer types are used to represent a subset of integers.
- Each integer type covers a range of integers and requires a certain number of bytes for storage.
- Integer types are char, short int (or simply short), int, long int (or simply long), and long long int (or simply long long).
Integer Types in C- Qualifiers
- The qualifiers
signed
andunsigned
can be applied to all integer types. unsigned
integers are always positive or zero.signed
integers can be positive, zero, or negative.char
,signed char
, andunsigned char
are treated as distinct types and should be used accordingly when storing numbers.- If the sign qualifier is omitted for other integer types, the integer is
signed.
Integer Types in C- Standard Size
- The C standard only defines the size of
char
; sizes of other integer types depend on the compiler implementation. char
is always 1 byte (8 bits).short
is at least 2 bytes (16 bits).int
is greater than or equal toshort
.long
is at least 4 bytes (32 bits).long long
is at least 8 bytes (64 bits).long long
is greater than or equal tolong
.
Integer Type Ranges in C
- Ranges for integer types with and without sign:
- 8 bits unsigned: 0 to 255
- 8 bits signed: -128 to 127
- 16 bits unsigned: 0 to 65,535
- 16 bits signed: -32,768 to 32,767
- 32 bits unsigned: 0 to 4,294,967,295
- 32 bits signed: -2,147,483,648 to 2,147,483,647
- 64 bits unsigned: 0 to 18,446,744,073,709,551,615
- 64 bits signed: -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
Integer Types in GCC
- For the GCC compiler, integer types have the following sizes:
char
: 1 byte (8 bits)short
: 2 bytes (16 bits)int
: 4 bytes (32 bits)long
: 4 bytes (32 bits)long long
: 8 bytes (64 bits)
Characters in C
- To store text in a computer, each character is encoded as an integer.
- These codes can be stored in variables of any integer type, commonly
int
orchar
.
Characters in C - ASCII
- One of the most used forms of encoding is known as ASCII Code.
- ASCII is the acronym of an institution that establishes standards to exchange information on communication and computation devices.
- Each character is represented using 7 bits, allowing for (2^7) = 128 different characters.
Floating-Point Types
- Floating-point types in C represent a subset of real numbers.
- Each floating-point type covers a range of real numbers and requires a certain number of bytes for storage.
- The basic floating-point types in C are float (single precision), double (double precision), and long double (extended precision).
Floating-Point Types - Standard Size
- Similar to integers, the size of floating-point types depends on the compiler implementation.
- Double and long double types have at least the same size, range of values, and level of precision as the float type.
Floating-Point Types - IEEE 754
- Most C compilers use the IEEE 754 standard for
float
anddouble
types. - Float: 4 bytes, Range: 3.4E-38 to 3.4E+38, Precision: 7 digits.
- Double: 8 bytes, Range: 1.7E-308 to 1.7E+308,Precision: 15 digits.
- Long Double: 16 bytes, Range: 2.805842e-313 to 2.805842e-313, Precision: 18 digits.
- Precision indicates the minimum difference between two numbers for them to be considered distinct.
Floating-Point Types -Precision
- For example, in the case of floating-point numbers, two numbers are considered equal if they are identical up to the seventh digit and differ only from the eighth digit onwards.
- Not all real numbers can be accurately represented in floating-point types, even if their values fall within the range of one of them.
Expressions
- Data processing involves combining data in some way, resulting in other data.
- Expressions are used to specify how data should be combined.
- An expression is a combination of operands and operators.
- Operands represent values and operators indicate the operations which should be performed to obtain a result.
Operands
- There are three types of operands in C:
- Constants
- Variables
- Function calls
Constants
- A value that is not modified during program execution.
- Constants in a C program can be of the following types:
- Integer
- Floating-point
- Character
- String
Integer Constants
- Integer constants are integer numbers within the range of 0 to the maximum value allowed by the
unsigned long long
type. - Negative constants are represented as unsigned constants with a unary minus operator
- When a number is bigger, a overflow occur without warning.
- Numbers within the range of
char
,signed char
,unsigned char
,short
,unsigned short
, orint
types are stored asint
by the compiler.
Other Number Constants
- Numbers exceeding the
int
range are stored in the smallest-range integer type that can contain them, indicated by a suffix. U
forunsigned
,L
forlong
,UL
forunsigned long
,LL
forlong long
,ULL
forunsigned long long
- Add a suffix of desired type, to store the number as a larger range as an integer.
Floating-Point Constants
- Floating-point constants contain a decimal point, an exponent, or both
- All floating-point constants are of type
double
unless they have the suffixF
orf
forfloat
orL
,o
orl
forlong double.
Character Constants
- Character constants are stored as integers
- The integer represents a code of character used by the compiler and is commonly the ASCII code
Character Constants
- Character constant can be written as a character delimited by apostrophes (e.g., 'a', 'Z') or using an escape sequence delimited by apostrophes.
- Escape sequences involve characters preceded by a backslash () or hexadecimal digits preceded by the characters \x.
Escape Sequences
Sequence | Code ASCII | Mnemonic | Meaning |
---|---|---|---|
\a | 7 | BELL | Beep |
\b | 8 | BS | Backspaces |
\f | 12 | FF | Page break |
\n | 10 | LF | Line break |
\r | 13 | CR | Carriage return |
\t | 9 | HT | Horizontal tab |
\v | 11 | VT | Vertical tab |
\ | 92 | Backslash | |
' | 39 | Apostrophe | |
" | 34 | " | Quotation marks |
? | 63 | ? | Question mark |
\xHHH | \xHHH | Hexadecimal value, one to three digits |
Constant Expressions
- A constant expression is an expression where all its operands are constants.
- Constant expressions are evaluated by the compiler and replaced with their value during compilation.
- For example, program translates a constant expression like
4 + 4
into `8``
String Type Constants
- Strings are sequences of characters delimited by quotation marks.
- Character sequences can contain escape sequences
Variables
- A variable is a data element whose value can change during program execution.
- Programs can contain variables of all data types
- All variables must be declared before use.
- Declaration informs the compiler of a variable's existence, name, and data type.
- Declaration also tells the compiler to reserve memory space for the variable.
Variable Declaration
- The syntax for declaring a variable is: type name1[ = exp1][, name2[ = exp2]...]
name1
,name2
, ... are variable names, of data type type- Variables can be initialized at declaration. The exp1, exp2, are expressions whose values initialize
name1
,name2
.
Variable Declaration - Example
- Examples of variable declarations:
int a, b, c, x = 10;
double l, result, product = 8.75;
char response;
Const Qualifier
- The
const
qualifier can be applied to any variable declaration to prevent its value from changing, effectively making it behave like a constant.- e.g.
const int x = 8; //Declares an integer constant called x with value 8
- e.g.
- A variable with the
const
qualifier must be initialized upon declaration. - Attempting to modify a
const
variable will result in a compiler error.
Function Calls
- A function is a subprogram that performs a specific task and returns a value upon completion.
- The call to the function is the order for the subprogram to run
- Calling a function involves writing its name within an expression. When the expression is evaluated, the program replaces the function call with the value returned by the function.
Function Calls - Example
- The expression
3.0 + sqrt(4.0)
contains a call to thesqrt
function from the C standard library, which returns the square root of its argument. - The call to the function is
sqrt(4.0)
. - The program returns the result with the value returned by the function:
3.0 + 2.0 results in
sqrt(4.0)
Some Standard C Library Functions
Function | Description |
---|---|
double atan(double x) | Calculates arctangent of x |
double cos(double x) | Calculates cosine of x |
double exp(double x) | Calculates e to the power of x |
double fabs(double x) | Calculates the absolute value of x |
double log(double x) | Calculates the natural logarithm of x |
double pow(double x, double y) | Calculates x to the power of y |
double sin(double x) | Calculates sine of x |
double tan(double x) | Calculates tangent of x |
int toupper(int c) | Converts c to upper cases |
Arithmetic Operators in C
- C has two arithmetic types: integer arithmetic and floating-point arithmetic.
- Each arithmetic type has its own rules and operates only on data of its own type.
- Integer arithmetic operates only with integers and produces integer results, and same for floating-point types.
- Mixing integers and floating-point numbers (no allowed).
- Operations in both arithmetic types will share the some of the same symbols. Example (+)
- The compiler determines which arithmetic to apply based on the type of operands.
Plus (+) and Minus (-) Operators
- The unary minus operator (-) changes the sign of the operand to which it is associated.
- The results's type matches the operant (integer = integer) (float = float)
- The unary plus operator (+) exists for symmetry but does not perform any action.
Multiplication (*), Division (/), and Modulus (%) Operators
- The multiplication operator (*) yields the product of its operands; if operands are integers, the result is an integer; if operands are floating-point, the result is floating-point.
- The division operator (/) yields the integer part of the quotient if its operands are integers; if the operands are floating-point, the result is floating-point.
- The modulus operator (%) operates only on integer operands and yields the remainder of their division.
Addition (+) and Subtraction (-) Operators
- The addition operator (+), and subtracting operator (-), yield the addition and the difference of their operands, respectively.
- With integer operands return an integer, and with floating-point operands return a floating-point
Overflow
- When a program is running, an expression will take a incorrect value.
- Example:
unsigned x = 4000000000;
- Considering integer occupying 4 bytes,
2 * x
doesn't take the 800000 value, so there will be an integer overflow.
- In most cases, if an overflow occurs, it won't stop the execution, but the results are wrong
- Programmers needs to make sure that the values are in range.
Increment and Decrement Operators
- Decrement and increment are only applied to integers variables.
- The increment operator(++) adds 1 to it's operand
- The increment operator(--) subtracts 1 to it's operand
- These operants can be used as suffixes and prefixes (++n or n++)
- Incrementing/ decrementing the operand in 1 will have the same effect.
Increment and Decrement Operators - Sufixes
- If the operant is used as prefixes, the operant is incremented first, it means before.
- If the operant is used as suffixes, the operant is incremented after.
n = 5;
n = 5;
x = n++;
y = ++n;
- In the both examples, n is 6.
x
is 5, andy
is 6.
Precedence and Associativity of Operators
- The order in which operations occur contain more than are operator is given by following rules:
- Expressions that are enclosed in parenthesis are evaluated first.
- The operations between precedent operators is evaluated first.
- Operators that are with the same precedence, are evaluated with a range given by associativity.
Implicit Type Conversion
-
When an operator has operands of different types, those operands will be converted to a type, in an automatic or implicit way, according to these rules:
-
Integer Promotion
- A
char
fromshort
is converted toint
, if the int type can show all the values and the unsigned shot, converts into int, and if not, converts tounsigned int
.
- A
Arithmetic Conversions
- All following rules only is the precedent is does not apply:
- If an certain operant is from the double long value type, the another is converts itself to the
double long
and the result is double long. - If one operant is from the double long value type, the another converts to the
double
value, and the result is the same. - If one is type float, it convert to float, and the results is float.
- If an certain operant is from the double long value type, the another is converts itself to the
Arithmetic Conversions Continued
- The operations on the preceding type are not valid, then the enter promotion is made on both operators, and if one of them is the type unsigned long long, the another converts.
- The above operations are not applicable, then the result is always
unsigned long long
Arithmetic Conversions Continued - Signed/Unsigned
- If one operant, and the other is from a different operant. The result only occurs if one is always on the type of long long.
- One condition needs to be that one has the long type, can show all the possible values, then the type becomes the unsigned long type
Arithmetic Conversions Last Rules
- The rules above are not applied, the operant and the result follows these rules:
- Is an expression that will perform, and if not, then is always long.
- Only, if the other is unsigned and the result is always unsigned if other conditions are not met.
- Is an expression that will perform the operators is and the result is always
Arithmetic Conversions - Result
- In conclusion, an operator that contains a difference type, the operation from the operant with a lower range is going to the operant with the major range
- The conversion does not change the variable or the type, but it will change the type, in a temporal part of the memory (that can be into the RAM or in the CPU) , and the type will be the one to effectuate the Calculus.
Arithmetic Conversion Examples
- These are the results with this expressions.
12 + 3.45 * 2
= Result: 18.90 Un Doble:4 % 3 - sqrt(45.89)
= Result -5.774216 Un doble -(89 / 7) / (95 % 15)
= Result: 2 Un Entero
Explicit Type Conversion
- The operation Cast, will help force the type of the operation to be valid, using syntax:
(type) expression
- Inside this case, expression is going to convert a determinate type to pre establish those rules.
Explicit Type Conversion - Example - OverFlow
- Here's an example when we have to use this convert operations:
- If declared
int x=1000000, y=4000;
- Expression
x *y
will have an overFlow, because400000000
reaches the enter range.
- If declared
Explicit Type Conversion - Example - Solve Overflow
- One solucion, or some operations of cast para change one of the operators to an long type so now there is no overflows:
(long long) x *y
Explicit Type Conversion Example - Average
- Second example, suppose that you are going to average a series for numbers that show ages from student, and you sum of this value in 2 value types:
SumaEdades / nAlumnos =
This show only the integer type. To show a float, perform this(double)SumaEdades / nAlumnos
Assignment Operators
- They replace the value from type variable.
-
+= *= %=
- Operator
=
its for assigning the left, and showing how the expression is to the rights.
Assignment Operators Expressions
nomVar
= expresión
nomVar
is now expresion
showing the type of expression from this example you can write nomVar1 =nomVar2 expesion
Assignment Operators Equivalences
- These are the equivalent from one express to another.
- *=
- = *
- /=
- = /
- +=
- = + *
- -=
- = -
- %=
- = %
- *=
Assignment Operators - Implicit Conversions
- Expressions and the implicit conversions are different from other things such as the type
- *nomVar
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.