Week 1 - C Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Briefly define source code and machine code.

A
  • Source code is the version of software as it is originally written by a human in plain text.
  • It is a group of instructions a programmer writes using computer programming languages.
  • The source code is is later turned into machine code, also called object code, that a computer can understand and execute.
  • Machine code is a computer programming language consisting of binary or hexadecimal instructions which a computer can respond to directly.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a compiler?

A
  1. A compiler is a program that translates computer code written in one programming language into another language.
  2. The original code is called the source code, and the resulting code is called the object code or the machine code.
  3. The process of transforming source code into object code or machine code is called compilation.
  4. A compiler allows a human programmer to write code that can be understood and executed by a specific CPU.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between a GUI and a CLI.

A
  • The command-line interface (CLI) and graphical user interface (GUI) are two different ways for users to interact with an operating system.
  • The key difference between the CLI and GUI is that the interaction with CLI is based on issuing commands.
  • In contrast, the interaction with a GUI involves visual elements, such as windows, buttons, etc.
  • Both interface types have advantages and disadvantages, but their purpose is the same.

Attached example of a CLI

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

What is the significance of the $ sign in the terminal window?

A

It means “type commands here”.

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

What is the syntax in C for displaying text in the console and what does each part mean? (the console is the terminal window or te cli window where the program outputs results)

A

printf("some text\n");

  • printf - the command to print text. “f” is for “formatted”. It allows you to print output with some formatting included.
  • the () curley braces are for specifying what the parameters will be for the function. In this case the printf function expects some input like text to print.
  • " - Every time you write text you need to quote it. like "hello, world"
  • \n - Means formatting for a newline. the \ is an “escape” character, which means that the next part of text is not part of the string of words.
  • ; - This is the end of the statement.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what does #include <stdio.h> do in C?

A
  1. stdio.h is Standard Library that provides input and output capabilities for C programs.
  2. In C we call this a “header file”
  3. It contains more than 40 functions that are used to perform input and output operations. The acronym stdio stands for Standard Input and Output.
  4. With this piece of code you are telling the program to include other code that has already been written into your own program.
  5. Among other things it makes the printf function avaialble for use in your code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why do you need to recompile code?

A

Whenever you change your source code you need to recreate or update the binary file. This means “recompiling” the code.

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

Define return value.

A

Its a value you get back from a function that you can use/read. Its the output value of a function.

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

What is the code to ask the user for an input with C?

A

string answer = get_string("What's your name? ");

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

What is the purpose of the = sign in C?

A
  1. Its the “assignment” symbol.
  2. It assignes the value on the right to the left of the sign.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What type is used for a variable that is assigned to words?

A
  1. string
  2. string is a type for a variable that needs to store a “string” of words.

e.g. string MyName = "cilliers";

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

What is the purpose of assigning a “type” to a variable?

A

You are informing the computer what “type” of data is going to be assigned to the variable.

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

In C how do you combine a value from a variable into a string?

A
  • the %s value is used as a placeholder.
printf("hello, %s",anwers);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the command for creating a new file named “hello” in C?

A

$ code hello.c

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

Exercise - In VS Code:
1. Create a new C file called “helloWorld”
2. Write a program that will print “hello world” to the command line internface.

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

Write the code to do an if else which prints out if X is larger than Y or if its the same.

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

What does = mean in C and what do you do when you want to copare if some values are equal?

A

The = sign is an assignment character. It assignes the value on the variable on the left.
If you want to compare values you need to write the coparitor == which evaluates if the value on the left is the same as the value on the right.

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

What is the difference in using " or ' in C?

A

When dealing with strings or multiple lines of characters you need to use " when deliberately dealing with single characters you need to use '. Single characters are “char” types as opposed to “string” types.

It is meant to ensure that the computer knows you are working with a char.

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

How do you write an OR condition in C?

A

||

e.g. 
if (x = 2 || x =1){
//do something
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does this code do and how can you simplify it?
~~~
int counter = 5;
counter = counter+1;
~~~

A

First line initializes a variable of type int named counter and assignes a value of 5.

Second line adds 1 to the counter. Assignment says that the value on the right is assigned to the variable on the left. Meaning counter+1 is equal to 6 and is assigned to counter on the left. Counter is now 6.

To simpify it C allows you to write counter+=1;

Or even, in the case of integers where you just want to increment by 1 you can write counter++;

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

How would you write a “while loop” in C to loop 3 times and print a string? How would you then simplify that?

A
int counter = 3;
while (counter > 0)
{
    printf("mystring /n");
    counter--;
}

**simplification**

for(int x = 0; x <3; x++)
{
    printf("mystring /n");
}
22
Q

Write a “while loop” that wil continue forever.

A
while (true)
{
do the code;
}
23
Q

What is the cli command to display files in a folder and what is special about the * files?

A
ls

Typing ls in CLI wil list all the files in a folder.
Files with the * extension are executable files. a.k.a. programs. when you type ./thefilesname the computer wil execute the program.

24
Q

How do you rename a file in the CLI in Linux?

A
mv nameoffile newnameoffile

mv means move and allows to rename and move

25
Q

how do you change folders in the command line for Linux?

A

cd

You need to add the relative math to the folder. Moving up a filder would be
~~~
cd ..
~~~

moving into down into a folder would be

cd ./myfolder
26
Q

What are the common linux CLI commands?

A

cd - change directory
cp - copy
ls - list files in directory
mkdir - make a new directory (create a folder)
mv - move
rm - remove
rmdir - remove directory/folder

27
Q

Explain what a do while loop does and write an example.

A

A do while loop wil do somthing first, then check the condition and repeat if the condition is not met.

28
Q

How do you write comments in C?

A
// some comment that is one line
29
Q

Write a reusable function that asks the user for an input and returns it. Explain what the definition of the function means.

A
30
Q

Write a function that takes an integer input and prints it, but does not return anything.

A
31
Q

Why can’t you use functions in the main method that are only declared later in the file?

A

C expects things to be included and delclared before the main function when compiled. You will need to declare the function signature at the top of the file and then you can use the function later in the file.

32
Q

How many bits do integers represent and what is the max value based on that?

A

32bits.

32 bits is 4.2billion

Because you also need to be able to represent negative numbers you only have about 2 billion numbers available.

33
Q

What type of value do you need for larger value than an int?

A

Use a long.
Long uses 64 bits.
64 bits is 9quintillion

34
Q

What type do you need to use if you want to use decimal valuables?

A

float
Float is 32 bits.
Float allows decinals.

If you need more you can use a double.

35
Q

How would you cast a value from long to float?

A

float z = (float) x / (float) y

36
Q

With a float, how do you print out only 20 decimal spaces?

A
37
Q

What is floating-point inprescision?

A

when you use a type like float its only 32bit so if the value goes beyond the size the computer will start giving incorrect digits after a certain point. You can use a double for more values,

38
Q

How much space does a “char” take?

A

8 bits

39
Q

Explain what “Long” and “Double” types are.

A
  • Long is a 64bit integer.
  • Double is double precision is a 64bit integer with decimal spaces.
40
Q

What are the string format codes for int, long, float, double, char, string.

A
41
Q

What is the integer data type?

A
42
Q

What is an unsigned Int?

A

You can get 0 to 4 billion positive values.

43
Q

What is a char data type?

A

Ascii maps the positive values to numbers.

44
Q

What is a float data type?

A
45
Q

What is a double data type?

A
46
Q

What is the void type?

A
47
Q

What are the 5 primitive types in C?

A

These data structures can be used to store only a single value. They are the foundation of data manipulation. The primitive data structures in C (also known as primitive data types) include int, char, float, double, and pointers.

47
Q
A
48
Q

What is the bool type?

A

The C programming language does not have a built-in Boolean data type like some other programming languages such as Java or Python. However, in C, the standard practice is to use integers to represent Boolean values, where 0 represents false and any non-zero value represents true.

49
Q

What is a string type?

A

The C language does not have a specific “String” data type, the way some other languages such as C++ and Java do. Instead C stores strings of characters as arrays of chars, terminated by a null byte.

50
Q

How do you create variables?

A