C Programming Week 2
69 Questions
2 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 NOT a basic data type in C?

  • double
  • const (correct)
  • float
  • char

What is the primary characteristic of a pointer in C?

  • It holds the memory address of another variable. (correct)
  • It is used exclusively for string data.
  • It stores the value of variables directly.
  • It automatically manages memory allocation.

What is the typical size of an 'int' data type in C?

  • 2 bytes
  • 8 bytes
  • 16 bytes
  • 4 bytes (correct)

Which data type allows multiple members to share the same memory location?

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

Which of the following correctly defines a double data type variable?

<p>double area = 50.0; (B)</p> Signup and view all the answers

How does the const keyword affect a variable in C?

<p>It prevents the variable's value from being changed after initialization. (B)</p> Signup and view all the answers

What does the keyword 'void' signify in a function's return type?

<p>The function does not return any value. (C)</p> Signup and view all the answers

Which statement about arrays in C is accurate?

<p>The size of an array is fixed after definition. (C)</p> Signup and view all the answers

Which statement about local variables is true?

<p>They are accessible only within the block they are declared. (B)</p> Signup and view all the answers

Which of the following describes derived data types?

<p>They include types like arrays, pointers, and structures. (D)</p> Signup and view all the answers

What is the maximum size for a 'short' data type typically in C?

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

What is the purpose of the #define directive in C?

<p>To define macros or constants at the preprocessing stage. (C)</p> Signup and view all the answers

What is the purpose of the 'const' keyword in variable declaration?

<p>To declare a variable that cannot be modified. (B)</p> Signup and view all the answers

What is a key requirement of the members of a structure in C?

<p>They can be of different data types. (C)</p> Signup and view all the answers

Which of the following statements about signed and unsigned types is true?

<p>Signed types can hold both negative and positive values, while unsigned types can only hold non-negative values. (D)</p> Signup and view all the answers

Which data type would be appropriate for storing the height of a person in meters?

<p>double (C), float (D)</p> Signup and view all the answers

What is the main difference between variable definition and declaration?

<p>Definition allocates memory, while declaration does not. (D)</p> Signup and view all the answers

What is the scope of a global variable in a C program?

<p>Visible throughout the entire translation unit. (D)</p> Signup and view all the answers

How are variables declared in C?

<p>With a data type and an optional initialization. (C)</p> Signup and view all the answers

Which of the following pairs of identifiers would be treated as different by C due to case sensitivity?

<p>number and Number (A), variable and Variable (D)</p> Signup and view all the answers

Which data type would be best for storing a single character?

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

What is the lifetime of a local variable?

<p>Only during the execution of the function or block. (A)</p> Signup and view all the answers

What must the main function return at the end of program execution?

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

Which of the following correctly describes the purpose of 'int argc' in the main function?

<p>Represents the number of command-line arguments (A)</p> Signup and view all the answers

What does the escape sequence '\n' do in a C program?

<p>Moves the cursor to a new line (C)</p> Signup and view all the answers

What is the correct syntax to declare a main function that accepts command-line arguments?

<p>int main(int argc, char* argv[]) (D)</p> Signup and view all the answers

Which comment style allows for multiple lines in C code?

<p>/* This is a comment */ (B)</p> Signup and view all the answers

What will be the output of the following code: printf("Welcome\nto\nFallTerm!\n");?

<p>Welcome to FallTerm! (D)</p> Signup and view all the answers

What is the role of the backslash '' in escape sequences?

<p>It acts as an escape character (B)</p> Signup and view all the answers

Which of the following is NOT a valid escape sequence?

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

What does the declaration of a variable in C specify?

<p>The type of the variable without allocating memory. (D)</p> Signup and view all the answers

Which of the following correctly describes the scope of global variables?

<p>They exist for the entire program's execution time. (B)</p> Signup and view all the answers

When a local variable is created, when is its memory allocated?

<p>When the function or block is entered. (D)</p> Signup and view all the answers

What is the primary distinction between variable definition and declaration in C?

<p>Definition allocates memory; declaration specifies type and indicates implementation elsewhere. (C)</p> Signup and view all the answers

Which of the following types of variables is accessible throughout the entire program?

<p>Global variables. (A)</p> Signup and view all the answers

What happens to the lifetime of a local variable once the function it belongs to exits?

<p>The variable's memory is deallocated. (B)</p> Signup and view all the answers

What is the purpose of specifying a data type when declaring a variable in C?

<p>To indicate how much memory to allocate for the variable. (D)</p> Signup and view all the answers

How does case sensitivity in C affect variable identifiers?

<p>It allows different usage of similar names with differing cases to represent different variables. (A)</p> Signup and view all the answers

What does the variable 'institutionID' represent in the example provided?

<p>A global variable that can be accessed in any function (D)</p> Signup and view all the answers

Which statement correctly describes 'studentID' in the resizeClass function?

<p>It is a local variable that exists only within the while loop. (C)</p> Signup and view all the answers

What is the significance of data types in C programming?

<p>They dictate the memory allocation and operations allowed on a variable. (C)</p> Signup and view all the answers

In C, how many bytes does a float typically occupy?

<p>4 bytes (32 bits) (B)</p> Signup and view all the answers

Which data type is used to represent real numbers with greater precision in C?

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

What does the keyword 'void' indicate when used in a function declaration?

<p>The function does not return any value. (C)</p> Signup and view all the answers

Which of the following statements about local variables is incorrect?

<p>They can be accessed outside of their defining function or block. (D)</p> Signup and view all the answers

Which statement about basic data types is true regarding their size?

<p>A float generally occupies less memory than a double. (C)</p> Signup and view all the answers

What is the default precision for the %f format specifier in the printf function?

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

What would be the output of the following printf function: printf("x=%4.1f, y=%7.2f", 15.231, 65.875948);?

<p>x=15.2, y=65.88 (B)</p> Signup and view all the answers

In the example with scanf("%3f%4f", &a, &b), what value will be stored in variable 'a' if the input is 5.965.87?

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

What determines the output's right justification in printf when using field width?

<p>The specified field width (A)</p> Signup and view all the answers

How many characters will the output 'b' have if it is formatted using printf with a field width of 4 (e.g., b=5)?

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

Which of the following printf format specifiers correctly formats a floating-point number to display one digit after the decimal point and minimum width of 4?

<p>%4.1f (A)</p> Signup and view all the answers

If the scanf function scans the input string '1234.56', what value will be assigned to 'b' when using scanf("%3f%4f", &a, &b)?

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

Which aspect of control flow in C does not involve looping?

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

What is the primary characteristic of a struct in C?

<p>Groups variables of different types in a single unit (A)</p> Signup and view all the answers

What does the 'long long' data type guarantee in C?

<p>At least 8 bytes of storage (A)</p> Signup and view all the answers

Which of the following statements about pointers is accurate?

<p>They can point to any data type (B)</p> Signup and view all the answers

What is a characteristic feature of a union in C?

<p>Only one member can be accessed at a time (D)</p> Signup and view all the answers

What is the effect of using the 'const' keyword in a declaration?

<p>It defines a variable that cannot be altered after initialization (D)</p> Signup and view all the answers

How are array sizes determined in C?

<p>By the number of elements and the size of each element (A)</p> Signup and view all the answers

What type of variable can a pointer in C hold?

<p>Addresses of any variable type (A)</p> Signup and view all the answers

What is the result of using the '#define' directive incorrectly, as in '#define MAX 10;' with a semicolon?

<p>It generates an error during the compilation process. (A)</p> Signup and view all the answers

What does the 'scanf' function require to store input values in the variables specified?

<p>The address of each variable involved. (D)</p> Signup and view all the answers

What will be the value of the variable 'term' after executing 'scanf("%d:%f", &marks, &term);' with the input '1500:20.5'?

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

How are macros defined with arguments expanded in C?

<p>They replace arguments with passed values during compilation. (C)</p> Signup and view all the answers

What value will 'a' and 'b' hold after executing 'scanf("%2d%3d", &a, &b);' with input '6903901'?

<p>a = 69, b = 39 (C)</p> Signup and view all the answers

Which of the following statements is true regarding the usage of the 'const' keyword?

<p>Using 'const' improves code readability, indicating values should remain constant. (A)</p> Signup and view all the answers

What is the result of evaluating the macro 'SUM(4, 5)' using the definition '#define SUM(x, y) ((x) + (y))'?

<p>It results in 9. (A)</p> Signup and view all the answers

What will the statement 'printf("Max students: %d\n", MAX_STUDENTS);' output given 'MAX_STUDENTS' is defined as 500?

<p>Max students: 500 (A)</p> Signup and view all the answers

Flashcards

Array

A collection of elements of the same data type.

Pointer

Holds the memory address of another variable.

Structure (struct)

Groups variables of different types into a single unit.

Union

Members share the same memory location.

Signup and view all the flashcards

Signed/Unsigned

Modifiers for integer types, deciding whether to allow negative or positive values.

Signup and view all the flashcards

Short/Long

Modifiers to change the size of integer types. Modifying storage size.

Signup and view all the flashcards

long long

Extended version of long integers, guaranteeing at least 8 bytes of storage.

Signup and view all the flashcards

const

Keyword to declare a variable whose value cannot be changed after initialization.

Signup and view all the flashcards

Global variable

A variable declared outside any function, accessible throughout the program.

Signup and view all the flashcards

Local variable

A variable declared inside a function, only accessible within that function.

Signup and view all the flashcards

Data Type (int)

Represents whole numbers (integers).

Signup and view all the flashcards

Data Type (char)

Represents a single character.

Signup and view all the flashcards

Data Type (float)

Represents real numbers with a fractional part (single-precision).

Signup and view all the flashcards

Data Type (double)

Represents real numbers with higher precision than float (double-precision).

Signup and view all the flashcards

Data Type (void)

Represents the absence of a type, often used for functions that return nothing.

Signup and view all the flashcards

Variable Scope

The region of a program where a variable can be accessed.

Signup and view all the flashcards

Variable Declaration

Specifies the type of variable and declares its existence, but doesn't allocate memory.

Signup and view all the flashcards

Variable Definition

Allocates memory for a variable and, optionally, gives it an initial value.

Signup and view all the flashcards

Variable Lifetime

The duration during which a variable exists in memory.

Signup and view all the flashcards

Data Type (int, char, float, double)

Specifies the kind of values stored in a variable.

Signup and view all the flashcards

Case Sensitivity (programming)

Programming languages that treat upper and lowercase letters differently.

Signup and view all the flashcards

Main Function

The starting point of a C program. It's the first function executed when the program runs.

Signup and view all the flashcards

Command-Line Arguments

Information passed to a program when it's executed, often used to customize its behavior.

Signup and view all the flashcards

What does return 0; indicate?

It signals that the program executed successfully, typically used at the end of the main function.

Signup and view all the flashcards

Escape Sequence

Special combinations of characters that represent non-printable characters or actions (like a newline).

Signup and view all the flashcards

What is \n?

An escape sequence that moves the cursor to the beginning of the next line, creating a line break.

Signup and view all the flashcards

Variable

A named container that stores a piece of data in a program.

Signup and view all the flashcards

Declare a Variable

To tell the C compiler the type of data a variable will store, but without assigning it a value yet.

Signup and view all the flashcards

Scope

The region of a program where a variable can be accessed.

Signup and view all the flashcards

Lifetime

The duration for which a variable exists in memory.

Signup and view all the flashcards

Variable Initialization

Assigning an initial value to a variable during its declaration or definition.

Signup and view all the flashcards

Case Sensitivity

A programming language feature where capitalization matters. For example, myVar and myvar are considered different variables.

Signup and view all the flashcards

int Data Type

Represents whole numbers (integers) without a fractional part. It's used for counting, indexing, and other operations that require integer values.

Signup and view all the flashcards

char Data Type

Represents a single character, like a letter, a digit, or a symbol. It's used for storing and manipulating characters.

Signup and view all the flashcards

float Data Type

Represents real numbers (numbers with fractional parts) with a certain level of precision. It's used for storing and manipulating numbers with decimal points.

Signup and view all the flashcards

double Data Type

Represents real numbers with higher precision than float. It's used for calculations that require greater accuracy.

Signup and view all the flashcards

void Data Type

Represents the absence of a type and is used for functions that don't return any value. It's often used to indicate that the function performs an action but doesn't return a result.

Signup and view all the flashcards

Data Type

Specifies the kind of data a variable can hold. It defines the type and size of the data that can be stored and manipulated within a program.

Signup and view all the flashcards

What is an array?

An array is a collection of elements of the same data type, stored in contiguous memory locations. You access individual elements using an index starting from 0.

Signup and view all the flashcards

What does a pointer store?

A pointer is a variable that stores the memory address of another variable. It allows you to access the data stored at that address.

Signup and view all the flashcards

What is a structure?

A structure is a composite data type that groups variables of different data types into a single unit, called a structure variable. It allows you to organize related data together.

Signup and view all the flashcards

What is a union?

A union is similar to a structure, but all its members share the same memory location. Only one member can be active at a time.

Signup and view all the flashcards

What is the purpose of the const keyword?

The const keyword makes a variable read-only. Once a value is assigned to a const variable, it cannot be changed later.

Signup and view all the flashcards

What is a signed integer?

A signed integer can represent both positive and negative numbers.

Signup and view all the flashcards

What is an unsigned integer?

An unsigned integer can only represent non-negative (positive and zero) numbers.

Signup and view all the flashcards

What is the difference between short and long integers?

The short and long keywords modify the size (number of bytes) used to represent an integer. A short integer uses less memory than a long integer, but can store a smaller range of values.

Signup and view all the flashcards

#define vs const

#define creates a macro that substitutes text during pre-processing. 'const' declares a variable that cannot be modified after initialization.

Signup and view all the flashcards

printf Function

A function in C that prints formatted output to the console. It uses a 'control string' to define the format and variables to be printed.

Signup and view all the flashcards

Control String

A string used in 'printf' to specify the format of the output. It includes placeholders for variables and formatting instructions.

Signup and view all the flashcards

Macro with Arguments

Macros with arguments are like functions that can take inputs and replace text based on those inputs.

Signup and view all the flashcards

Field Width

Specifies the minimum number of characters to be printed for a variable. If the variable's value is shorter, it is right-justified and padded with spaces.

Signup and view all the flashcards

Nesting of Macros

Macros can be nested, meaning one macro can use another macro.

Signup and view all the flashcards

Precision (For Floats)

Refers to the number of digits shown after the decimal point for floating-point numbers. It influences how precise the displayed representation is.

Signup and view all the flashcards

scanf control string

The control string in scanf specifies how the input data should be read and interpreted. It dictates the format of the input.

Signup and view all the flashcards

scanf Function

Reads formatted input from the console. It uses a 'control string' to define how the input should be interpreted.

Signup and view all the flashcards

scanf Example: int & float

Using scanf with %d and %f reads an integer and a floating-point number.

Signup and view all the flashcards

scanf Example: String

Reading a string with scanf uses %s.

Signup and view all the flashcards

Control Flow

The order in which statements are executed in a program. It involves conditionals and loops that determine which code blocks are run.

Signup and view all the flashcards

scanf Example: Field Width Specifier

A number before a format specifier (like %2d) limits the number of characters read.

Signup and view all the flashcards

Conditionals

Code structures that allow different parts of the program to execute based on certain conditions.

Signup and view all the flashcards

What does return 0; signify?

It indicates that the program executed successfully.

Signup and view all the flashcards

Study Notes

Course Information

  • Course Title: C Programming
  • Course Code: CST8234
  • Week: 2
  • Topic: Basics of the language

Objectives

  • Write simple C programs
  • Use I/O statements
  • Understand fundamental data types
  • Write simple decision-making statements
  • Learn about control specifiers
  • Understand macros
  • Introduction to arrays

Basics of the C Language

  • The main function is the entry point of a C program.
  • Using other files and libraries (e.g., #include <stdio.h>).
  • C program structure: preprocessor directives, function definitions, statements and expressions.
  • Variables
  • Control Flow (conditionals and loops)
  • Functions

Typical C Program Structure

  • Preprocessor directives: lines beginning with # (e.g., #include).
  • Function definitions: including the main function and other functions.
  • Statements and expressions: actual code to perform operations.

Example of a Typical C Program

#include <stdio.h>
int add(int a, int b) {
  return a + b;
}
int main() {
  int result = add(5, 3);
  printf("The result is %d\n", result);
  return 0;
}

#include Directive

  • Used to include the contents of a file within another file.
  • Useful for including libraries (e.g., stdio.h) and user-defined header files.
  • Syntax:
    • #include <filename.h>: include from a standard library.
    • #include "filename.h": include from a local directory.

How #include Works

  • Preprocessor stage: replaces the #include directive with the contents of the specified file before compilation.
  • Inclusion of content: the compiler processes included content as part of the original file.

Avoiding Multiple Inclusions

  • Use include guards (e.g., #ifndef, #define, #endif) to prevent multiple inclusions of the same header file.

The main Function (Example)

#include <stdio.h>
int main() {
  printf("Welcome to C!\n");
  return 0;
}

Variables

  • Variables in C store data.
  • They must be declared before use with a data type.
  • Example: int age = 25;, char initial = 'A';.
  • Different data types: int, char, float, double.

Variables Definition vs Declaration

  • Definition: allocates memory and initializes a variable.
  • Declaration: declares a variable without allocating memory.

Scope and Lifetime of Variables

  • Global variables: accessible throughout the entire program.
  • Local variables: accessible only within the function or block where they are declared.

Data Types

  • int: whole numbers
  • char: single characters
  • float: single-precision floating-point numbers
  • double: double-precision floating-point numbers

Derived Data Types

  • void: absence of type
  • array: collection of elements of the same type.
  • pointer: holds memory address of another variable.
  • struct: groups variables of different types.
  • union: similar to structure, but members share memory.

Modified Data Types

  • signed/unsigned int: specify whether integers can be negative or not.
  • short/long int: modify integer size.
  • long long: an extended version of long int.

const Keyword

  • const variables have values that cannot be changed after initialization.
  • Useful for defining constants (e.g., configuration parameters).

Macro

  • Used to define macros or constants during the preprocessing stage.
  • The #define directive is used to substitute text in the code with a literal value.
  • Example: macro #define MAX_VALUE 100.

const vs #define

  • const: has type safety, allocates memory.
  • #define: no type safety, no memory allocation.

Control Specifiers in scanf and printf

  • scanf reads input from the user, printf displays output.
  • Control strings specify input/output formats.
  • Use format specifiers (e.g., %d for integers, %f for floats, %s for strings).

Functions

  • Functions are fundamental building blocks in a C program.
  • Function syntax: <return type> <function name>(<parameters>).
  • Functions are reusable code blocks.

For Loop:

  • Used for fixed iterations

While Loop:

- Used for iterations based on a condition

Do-while:

 - The loop body is executed at least once.

Logical Operators

  • &&: logical AND, ||: logical OR, !: logical NOT

Relational Operators

- `==`, `!=`, `<`, `>`, `<=`, `>=`: Used to compare values and create conditions.

Ternary Operator

  • A shorthand for if-else statements.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Description

This quiz covers the fundamentals of C programming language, focusing on writing simple C programs, understanding I/O statements, and fundamental data types. It also introduces control flow and arrays, providing essential skills for coding in C. Test your knowledge on these basic concepts with this quiz.

Use Quizgecko on...
Browser
Browser