Podcast
Questions and Answers
Which of the following is NOT a basic data type in C?
Which of the following is NOT a basic data type in C?
What is the primary characteristic of a pointer in C?
What is the primary characteristic of a pointer in C?
What is the typical size of an 'int' data type in C?
What is the typical size of an 'int' data type in C?
Which data type allows multiple members to share the same memory location?
Which data type allows multiple members to share the same memory location?
Signup and view all the answers
Which of the following correctly defines a double data type variable?
Which of the following correctly defines a double data type variable?
Signup and view all the answers
How does the const
keyword affect a variable in C?
How does the const
keyword affect a variable in C?
Signup and view all the answers
What does the keyword 'void' signify in a function's return type?
What does the keyword 'void' signify in a function's return type?
Signup and view all the answers
Which statement about arrays in C is accurate?
Which statement about arrays in C is accurate?
Signup and view all the answers
Which statement about local variables is true?
Which statement about local variables is true?
Signup and view all the answers
Which of the following describes derived data types?
Which of the following describes derived data types?
Signup and view all the answers
What is the maximum size for a 'short' data type typically in C?
What is the maximum size for a 'short' data type typically in C?
Signup and view all the answers
What is the purpose of the #define
directive in C?
What is the purpose of the #define
directive in C?
Signup and view all the answers
What is the purpose of the 'const' keyword in variable declaration?
What is the purpose of the 'const' keyword in variable declaration?
Signup and view all the answers
What is a key requirement of the members of a structure in C?
What is a key requirement of the members of a structure in C?
Signup and view all the answers
Which of the following statements about signed and unsigned types is true?
Which of the following statements about signed and unsigned types is true?
Signup and view all the answers
Which data type would be appropriate for storing the height of a person in meters?
Which data type would be appropriate for storing the height of a person in meters?
Signup and view all the answers
What is the main difference between variable definition and declaration?
What is the main difference between variable definition and declaration?
Signup and view all the answers
What is the scope of a global variable in a C program?
What is the scope of a global variable in a C program?
Signup and view all the answers
How are variables declared in C?
How are variables declared in C?
Signup and view all the answers
Which of the following pairs of identifiers would be treated as different by C due to case sensitivity?
Which of the following pairs of identifiers would be treated as different by C due to case sensitivity?
Signup and view all the answers
Which data type would be best for storing a single character?
Which data type would be best for storing a single character?
Signup and view all the answers
What is the lifetime of a local variable?
What is the lifetime of a local variable?
Signup and view all the answers
What must the main function return at the end of program execution?
What must the main function return at the end of program execution?
Signup and view all the answers
Which of the following correctly describes the purpose of 'int argc' in the main function?
Which of the following correctly describes the purpose of 'int argc' in the main function?
Signup and view all the answers
What does the escape sequence '\n' do in a C program?
What does the escape sequence '\n' do in a C program?
Signup and view all the answers
What is the correct syntax to declare a main function that accepts command-line arguments?
What is the correct syntax to declare a main function that accepts command-line arguments?
Signup and view all the answers
Which comment style allows for multiple lines in C code?
Which comment style allows for multiple lines in C code?
Signup and view all the answers
What will be the output of the following code: printf("Welcome\nto\nFallTerm!\n");?
What will be the output of the following code: printf("Welcome\nto\nFallTerm!\n");?
Signup and view all the answers
What is the role of the backslash '' in escape sequences?
What is the role of the backslash '' in escape sequences?
Signup and view all the answers
Which of the following is NOT a valid escape sequence?
Which of the following is NOT a valid escape sequence?
Signup and view all the answers
What does the declaration of a variable in C specify?
What does the declaration of a variable in C specify?
Signup and view all the answers
Which of the following correctly describes the scope of global variables?
Which of the following correctly describes the scope of global variables?
Signup and view all the answers
When a local variable is created, when is its memory allocated?
When a local variable is created, when is its memory allocated?
Signup and view all the answers
What is the primary distinction between variable definition and declaration in C?
What is the primary distinction between variable definition and declaration in C?
Signup and view all the answers
Which of the following types of variables is accessible throughout the entire program?
Which of the following types of variables is accessible throughout the entire program?
Signup and view all the answers
What happens to the lifetime of a local variable once the function it belongs to exits?
What happens to the lifetime of a local variable once the function it belongs to exits?
Signup and view all the answers
What is the purpose of specifying a data type when declaring a variable in C?
What is the purpose of specifying a data type when declaring a variable in C?
Signup and view all the answers
How does case sensitivity in C affect variable identifiers?
How does case sensitivity in C affect variable identifiers?
Signup and view all the answers
What does the variable 'institutionID' represent in the example provided?
What does the variable 'institutionID' represent in the example provided?
Signup and view all the answers
Which statement correctly describes 'studentID' in the resizeClass function?
Which statement correctly describes 'studentID' in the resizeClass function?
Signup and view all the answers
What is the significance of data types in C programming?
What is the significance of data types in C programming?
Signup and view all the answers
In C, how many bytes does a float typically occupy?
In C, how many bytes does a float typically occupy?
Signup and view all the answers
Which data type is used to represent real numbers with greater precision in C?
Which data type is used to represent real numbers with greater precision in C?
Signup and view all the answers
What does the keyword 'void' indicate when used in a function declaration?
What does the keyword 'void' indicate when used in a function declaration?
Signup and view all the answers
Which of the following statements about local variables is incorrect?
Which of the following statements about local variables is incorrect?
Signup and view all the answers
Which statement about basic data types is true regarding their size?
Which statement about basic data types is true regarding their size?
Signup and view all the answers
What is the default precision for the %f format specifier in the printf function?
What is the default precision for the %f format specifier in the printf function?
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);?
What would be the output of the following printf function: printf("x=%4.1f, y=%7.2f", 15.231, 65.875948);?
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?
In the example with scanf("%3f%4f", &a, &b), what value will be stored in variable 'a' if the input is 5.965.87?
Signup and view all the answers
What determines the output's right justification in printf when using field width?
What determines the output's right justification in printf when using field width?
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)?
How many characters will the output 'b' have if it is formatted using printf with a field width of 4 (e.g., b=5)?
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?
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?
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)?
If the scanf function scans the input string '1234.56', what value will be assigned to 'b' when using scanf("%3f%4f", &a, &b)?
Signup and view all the answers
Which aspect of control flow in C does not involve looping?
Which aspect of control flow in C does not involve looping?
Signup and view all the answers
What is the primary characteristic of a struct in C?
What is the primary characteristic of a struct in C?
Signup and view all the answers
What does the 'long long' data type guarantee in C?
What does the 'long long' data type guarantee in C?
Signup and view all the answers
Which of the following statements about pointers is accurate?
Which of the following statements about pointers is accurate?
Signup and view all the answers
What is a characteristic feature of a union in C?
What is a characteristic feature of a union in C?
Signup and view all the answers
What is the effect of using the 'const' keyword in a declaration?
What is the effect of using the 'const' keyword in a declaration?
Signup and view all the answers
How are array sizes determined in C?
How are array sizes determined in C?
Signup and view all the answers
What type of variable can a pointer in C hold?
What type of variable can a pointer in C hold?
Signup and view all the answers
What is the result of using the '#define' directive incorrectly, as in '#define MAX 10;' with a semicolon?
What is the result of using the '#define' directive incorrectly, as in '#define MAX 10;' with a semicolon?
Signup and view all the answers
What does the 'scanf' function require to store input values in the variables specified?
What does the 'scanf' function require to store input values in the variables specified?
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'?
What will be the value of the variable 'term' after executing 'scanf("%d:%f", &marks, &term);' with the input '1500:20.5'?
Signup and view all the answers
How are macros defined with arguments expanded in C?
How are macros defined with arguments expanded in C?
Signup and view all the answers
What value will 'a' and 'b' hold after executing 'scanf("%2d%3d", &a, &b);' with input '6903901'?
What value will 'a' and 'b' hold after executing 'scanf("%2d%3d", &a, &b);' with input '6903901'?
Signup and view all the answers
Which of the following statements is true regarding the usage of the 'const' keyword?
Which of the following statements is true regarding the usage of the 'const' keyword?
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))'?
What is the result of evaluating the macro 'SUM(4, 5)' using the definition '#define SUM(x, y) ((x) + (y))'?
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?
What will the statement 'printf("Max students: %d\n", MAX_STUDENTS);' output given 'MAX_STUDENTS' is defined as 500?
Signup and view all the answers
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 oflong 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.
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.