Week 1 - C Flashcards
Briefly define source code and machine code.
- 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.
What is a compiler?
- A compiler is a program that translates computer code written in one programming language into another language.
- The original code is called the source code, and the resulting code is called the object code or the machine code.
- The process of transforming source code into object code or machine code is called compilation.
- A compiler allows a human programmer to write code that can be understood and executed by a specific CPU.
What is the difference between a GUI and a CLI.
- 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
What is the significance of the $ sign in the terminal window?
It means “type commands here”.
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)
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 theprintf
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.
what does #include <stdio.h>
do in C?
-
stdio.h
is Standard Library that provides input and output capabilities for C programs. - In C we call this a “header file”
- It contains more than 40 functions that are used to perform input and output operations. The acronym stdio stands for Standard Input and Output.
- With this piece of code you are telling the program to include other code that has already been written into your own program.
- Among other things it makes the
printf
function avaialble for use in your code.
Why do you need to recompile code?
Whenever you change your source code you need to recreate or update the binary file. This means “recompiling” the code.
Define return value.
Its a value you get back from a function that you can use/read. Its the output value of a function.
What is the code to ask the user for an input with C?
string answer = get_string("What's your name? ");
What is the purpose of the =
sign in C?
- Its the “assignment” symbol.
- It assignes the value on the right to the left of the sign.
What type is used for a variable that is assigned to words?
string
- string is a type for a variable that needs to store a “string” of words.
e.g. string MyName = "cilliers";
What is the purpose of assigning a “type” to a variable?
You are informing the computer what “type” of data is going to be assigned to the variable.
In C how do you combine a value from a variable into a string?
- the
%s
value is used as a placeholder.
printf("hello, %s",anwers);
What is the command for creating a new file named “hello” in C?
$ code hello.c
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.
Write the code to do an if
else
which prints out if X is larger than Y or if its the same.
What does =
mean in C and what do you do when you want to copare if some values are equal?
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.
What is the difference in using "
or '
in C?
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 do you write an OR
condition in C?
||
e.g. if (x = 2 || x =1){ //do something }
What does this code do and how can you simplify it?
~~~
int counter = 5;
counter = counter+1;
~~~
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++;