Cheat Sheets Flashcards

(45 cards)

1
Q

#include<stdio.h> and int main(){ return(0); } in C programming?

A

This is boilerplate code in C. #include<stdio.h> includes the standard input-output library, and int main(){ return(0); } is the basic structure of a C program where execution begins and returns 0 on successful completion.</stdio.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of the printf function?

A

It is used to show output on the screen.

Example: printf(“Hello World!”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is the scanf function used?

A

It is used to take input from the user with the syntax scanf(“placeholder”, variables).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a comment in C, and what are the two types?

A

A comment is code not executed by the compiler, used by programmers to track code. The two types are single-line comments (e.g., // It’s a single line comment) and multi-line comments (e.g., /* It’s a multi-line comment */).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the basic data types in C?

A

The basic data types are: char (typically a single byte, integer type), int (most natural size of integer for the machine), float (single-precision floating-point value), double (double-precision floating-point value), and void (represents the absence of type).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are escape sequences, and give examples?

A

An escape sequence is a sequence of characters starting with a backslash that doesn’t represent itself inside a string literal. Examples include: \a (Alarm or Beep), \b (Backspace), \f (Form feed), \n (Newline Character), \r (Carriage return), \t (Tab), \ (Backslash), ' (Single quote), " (Double quote), \? (Question mark), \nnn (Octal No.), \xhh (Hexadecimal No.), and \0 (Null character to terminate a string).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are conditional instructions used for in C?

A

Conditional statements are used to perform operations based on some condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the if statement syntax.

A

The if statement has the syntax: if (condition) { /* code */ }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does an if-else statement work?

A

An if-else statement executes code based on a condition: if (condition) { /* code / } else { / Code */ }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe the if else-if statement.

A

The if else-if statement allows for multiple conditions: if (condition) { // Statements; } else if (condition) { // Statements; } else { // Statements }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a switch-case statement?

A

It allows a variable to be tested for equality against a list of values (cases).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the syntax for a switch-case statement?

A

The syntax is: switch (expression) { case constant-expression: statement1; statement2; break; case constant-expression: statement; break; … default: statement; }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of iterative statements?

A

Iterative statements facilitate programmers to execute any block of code lines repeatedly and can be controlled as per conditions added by the programmer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does a while loop function?

A

It allows execution of statements inside the block of the loop until the condition of the loop succeeds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a do-while loop, and how does it differ from a while loop?

A

A do-while loop is an exit-controlled loop. It is similar to the while loop, but its body is executed at least once even if the expression is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain the for loop in C.

A

A for loop is used to iterate statements or a part of the program several times. It is frequently used to traverse data structures like arrays and linked lists. The syntax is for (int i=0; i < count; i++) { /* code */ }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the function of the break statement?

A

The break keyword inside a loop is used to terminate the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does the continue statement do?

A

The continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Why are functions used in C programming?

A

Functions are used to divide an extensive program into smaller pieces. They can be called multiple times to provide reusability and modularity to the C program.

20
Q

What is the syntax for a function definition?

A

The syntax is return_type function_name(data_type parameter…) { //code to be executed }.

21
Q

What is recursion in programming?

A

Recursion is when a function calls a copy of itself to work on a minor problem. A function that calls itself is known as a Recursive function.

22
Q

What is a pointer in C, and how is it declared?

A

A pointer is a variable that contains the address of another variable. It is declared using the syntax datatype *var_name;.

23
Q

What is an array in C, and how are its elements accessed?

A

An array is a collection of data items of the same type. It is declared as data_type array_name [array_size];, and an element is accessed as int variable_name = array[index];.

24
Q

What is a string in C, and how is it declared?

A

A string is a 1-D character array terminated by a null character (‘\0’). It is declared as char str_name[size];.

25
What is the gets() function used for?
It allows you to enter a multi-word string using gets("string");.
26
What is the puts() function used for?
It is used to show string output using puts("string");.
27
What is the strlen() function used for?
It is used to calculate the length of the string using strlen(string_name);.
28
What is the strcpy() function used for?
It is used to copy the content of a second string into the first string passed to it using strcpy(destination, source);.
29
What is the strcat() function used for?
It is used to concatenate two strings using strcat(first_string, second_string);.
30
What is the strcmp() function used for?
It is used to compare two strings using strcmp(first_string, second_string);.
31
What is a structure in C?
The structure is a collection of variables of different types under a single name. Defining a structure means creating a new data type.
32
What is the syntax for defining a structure?
The syntax is: struct structureName { dataType member1; dataType member2; };.
33
What is the typedef keyword used for in C?
The typedef function allows users to provide alternative names for primitive and user-defined data types.
34
How is typedef used with structures?
It can be used to create an alias for a structure, for example: typedef struct structureName { dataType member1; dataType member2; } new_name;.
35
What is a FILE pointer used for in file handling?
A FILE pointer, declared as FILE *filePointer;, is used for handling file I/O operations (read/write/append) in C.
36
How do you open a file in C?
A file is opened using fopen, for example: filePointer = fopen(fileName.txt, w).
37
What is the fscanf() function used for?
It is used to read the content of a file.
38
What is the fprintf() function used for?
It is used to write content into the file.
39
What does fgetc() function do?
It reads a character from a file opened in read mode and returns EOF on reaching the end of the file.
40
What does fputc() function do?
It writes a character to a file opened in write mode.
41
How do you close a file in C?
A file is closed using fclose(filePointer);.
42
What is the malloc() function used for in dynamic memory allocation?
malloc() stands for 'Memory allocation' and reserves a block of memory with the given amount of bytes. Its syntax is ptr = (castType*) malloc(size);.
43
What is the calloc() function used for?
calloc() stands for 'Contiguous allocation' and reserves 'n' blocks of memory with the given amount of bytes. Its syntax is ptr = (castType*) calloc(n, size);.
44
What is the free() function used for?
It is used to free the allocated memory using free(ptr);.
45
What is the realloc() function used for?
If the allocated memory is insufficient, realloc() can change the size of previously allocated memory for efficiency purposes. Its syntax is ptr = realloc(ptr, x);.