C Data Types and Expressions

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

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?

  • int
  • float
  • string (correct)
  • double

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?

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

If the qualifier is omitted, what is the default sign of integer types in C?

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

What is the purpose of the ASCII code?

<p>To define a standard for representing characters as integers. (C)</p> Signup and view all the answers

How many different characters can be represented using the basic ASCII code?

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

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?

<p>Equal, due to the precision of <code>float</code>. (C)</p> Signup and view all the answers

Which of the following is NOT a characteristic of an expression in C?

<p>It always modifies the value of the operands. (C)</p> Signup and view all the answers

Which of the following is NOT a type of operand in C?

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

What is the main characteristic of a constant in C?

<p>Its value cannot be modified during program execution. (D)</p> Signup and view all the answers

If a number exceeds the maximum representable value for the unsigned long long type, what happens in C?

<p>The value wraps around (overflows) without warning. (C)</p> Signup and view all the answers

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?

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

How can you specify that an integer constant should be stored as unsigned long in C?

<p>Append the suffix 'UL' to the number. (D)</p> Signup and view all the answers

How are all floating point constants treated, if they do not have a suffix?

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

What suffix is used to explicitly declare a floating point constant as type float?

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

How are character constants stored?

<p>As integers representing the character code. (B)</p> Signup and view all the answers

Which of the following describes a character escape sequence?

<p>A method to include special characters in strings or character constants. (B)</p> Signup and view all the answers

What is the character that represents 'horizontal tab'?

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

What is a string literal in C?

<p>A sequence of characters enclosed in double quotes. (B)</p> Signup and view all the answers

Which of the following statements is true regarding variables in C?

<p>Declaring a variable involves specifying its type and reserving memory space. (A)</p> Signup and view all the answers

What does declaring a variable achieve?

<p>It informs the compiler of the variable's name and type and reserves memory. (B)</p> Signup and view all the answers

Which of the following represents the correct syntax for declaring multiple variables of the same type in C?

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

What happens if you try to modify a variable declared with the const qualifier?

<p>The compiler will generate an error. (C)</p> Signup and view all the answers

What is a function's role?

<p>To perform a specific task and return a value. (A)</p> Signup and view all the answers

In the expression 3.0 + sqrt(4.0), what will happen to value of sqrt(4.0)?

<p>It will call the <code>sqrt</code> function and return <code>2.0</code> (B)</p> Signup and view all the answers

Which of the following is NOT a standard function in the C math library?

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

What happens if one tries to mix integers and floats?

<p>They will be converted to one type. (D)</p> Signup and view all the answers

What is the role of the unary minus operator?

<p>To change the sign of an operand. (C)</p> Signup and view all the answers

What is the result of dividing two integers?

<p>The integer part of the division. (D)</p> Signup and view all the answers

What does the modulo operator (%) do?

<p>Returns the remainder of a division. (C)</p> Signup and view all the answers

What happens in C when an integer expression exceeds the maximum possible value for its type?

<p>An overflow occurs, leading to unexpected or incorrect results. (B)</p> Signup and view all the answers

What is the effect of an integer overflow?

<p>It leads to erroneous or incorrect results. (D)</p> Signup and view all the answers

What is the purpose of increment and decrement operators?

<p>Add or subtract 1 from integer variables (D)</p> Signup and view all the answers

What is the difference between prefix and postfix increment/decrement operators?

<p>Prefix operators modify the variable before it is used in an expression. (C)</p> Signup and view all the answers

What determines the order in which operations are performed in an expression?

<p>Operator precedence and associativity. (C)</p> Signup and view all the answers

Which of the following operators has lowest precedence?

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

What is implicit type conversion in C?

<p>A conversion automatically performed by the compiler. (A)</p> Signup and view all the answers

In implicit type conversion, what type promotion will take place if there are two different operands with the types short and int?

<p>short will be converted to int (A)</p> Signup and view all the answers

What operator performs explicit type conversion?

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

Flashcards

¿Qué es una Variable?

A named location in memory that can store data of a specific type during program execution.

¿Qué son Tipos Enteros?

Data types that can represent whole numbers and their negatives (e.g., int, short, long).

¿Qué son Tipos Flotantes?

Data types that can represent numbers with fractional parts (e.g., float, double).

¿Qué son Calificadores signed y unsigned?

Keywords (signed or unsigned) that specify if an integer type stores both positive and negative values or only positive values.

Signup and view all the flashcards

¿Qué es el Código ASCII?

A coding standard where each character is represented by a unique 7-bit integer.

Signup and view all the flashcards

¿Qué son Constantes?

Values in a program that remain constant during execution.

Signup and view all the flashcards

¿Qué es una Constante de Tipo Cadena?

A constant that consists of a sequence of characters enclosed in double quotes.

Signup and view all the flashcards

¿Qué son Funciones Estándar de C?

Built-in functions available in C that perform common mathematical operations.

Signup and view all the flashcards

¿Qué son Operadores?

Symbols that perform operations on operands, like addition, subtraction, multiplication, etc.

Signup and view all the flashcards

¿Qué es Sobreflujo?

A situation where the result of an arithmetic operation exceeds the maximum value that can be stored.

Signup and view all the flashcards

¿Qué son Operadores de Incremento y Decremento?

Operators that increment or decrement a variable's value by one.

Signup and view all the flashcards

¿Qué es Precedencia de Operadores?

The order in which operators are evaluated in an expression.

Signup and view all the flashcards

¿Qué es Conversión Implícita de Tipos?

The default or automatic conversion of data types by the compiler during operations.

Signup and view all the flashcards

¿Qué es Conversión Explícita de Tipos?

The manual conversion of data types using the cast operator.

Signup and view all the flashcards

¿Qué son Operandos?

The components or values on which operators act in an expression.

Signup and view all the flashcards

¿Qué es una Expresión?

A combination of operands and operators that evaluates to a single value.

Signup and view all the flashcards

¿Qué es una Función?

A subprogram performing a specific task and returning a value.

Signup and view all the flashcards

¿Qué es una Llamada a la Función?

The act of invoking a function to execute its code.

Signup and view all the flashcards

¿Cuáles son los Tipos Enteros en C?

char, short int, int, long int, and long long int

Signup and view all the flashcards

¿Qué es un sobreflujo entero?

When a program tries to store a value that is too large or too small for the data type.

Signup and view all the flashcards

¿Qué son tipos flotantes en C?

Numbers with decimal points or exponents; float, double, and long double

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 and unsigned 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, and unsigned 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 to short.
  • long is at least 4 bytes (32 bits).
  • long long is at least 8 bytes (64 bits).
  • long long is greater than or equal to long.

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 or char.

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 and double 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, or int types are stored as int 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 for unsigned, L for long, UL for unsigned long, LL for long long, ULL for unsigned 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 suffix F or f for float or L, o or l for long 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
  • 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 the sqrt 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, and y 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 from short is converted to int, if the int type can show all the values and the unsigned shot, converts into int, and if not, converts to unsigned int.

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.

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;
    • Expressionx *y will have an overFlow, because 400000000 reaches the enter range.

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.

Quiz Team

Related Documents

More Like This

C Programming Data Types Explained
16 questions
Data Types in Programming Languages
48 questions
Use Quizgecko on...
Browser
Browser