Tasks studies - laboratory
#include
directive work?Create a Lab2 folder on your desktop, then open the folder in Visual Studio Code.
A variable declaration looks like this: `[variable type] [variable name]; e.g.:
int variable1;
A variable initialized in this way currently refers to a given location in the computer’s memory where the memory bits are in a “random state”.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) // every program must have a starting point, which by default is the main function.
{
int a;
printf("Value of variable a: %i",a);
return 0;
}
> 2658304
During subsequent program runs, we may get a different result:
> 3563520
To set the bit sequence in memory and thus make our variable store the values defined by us, we use the assignment operator =
.
int a = 3; // The bits for the number 3 will be set appropriately in memory.
printf("Variable value a: %i",a);
Basic variable types and literals that allow you to define them:
int Integer = 432871; // type stores integers in a given range
float FloatingPoint = 3.14; // type stores floating-point numbers in a given range
char character = 'a'; // type stores letters, digits, and special characters.
double FloatingPoint = 32.32; // yp stores floating-point numbers with greater precision than float
Constants are defined by adding const
or #define
before the variable definition. Unlike a dynamic variable, we cannot assign the value of a constant.
int a = 12;
const B = 54;
// changing the value of a is allowed
a = 43;
// assigning a new constant value is not allowed
// B = 66;
Array variables store variables of a specific type in consecutive memory addresses. In this way, we can define strings of characters.
char string[20]; // creating a string variable that stores 20 char variables that we refer to by indexes.
string[0] = 'H'; // assigning the letter 'H' to the first reserved address
string[1] = 'e';
string[2] = 'l';
string[3] = 'l';
string[4] = 'o';
string[5] = ' ';
string[6] = 'C';
printf("%c", string[6]); // Printing a character in the 7th position in the array.
printf("%s",string);
A literal that makes it easier to assign values to subsequent elements of the array (the result of the program will be the same as the previous one):
char string2[20] = "Hello C";
printf("%s",string2);
Interaction with the program - printf()
scanf()
Using the documentation for the printf function, create a task1.exe program that stores the following variables:
name
- first namesurname
- last nameage
- agealbumNumber
- album numberPI_VALUE
- value of the pi number (constant value - constant)and then prints this information to the standard output.
Assign new values to the variables that are the data of your colleagues on your left and right and print these values. (The program should print information about 3 people)
Create the file operators.c and then execute the following commands.
Arithmetic operators:
Symbol | Operation |
---|---|
+ | . |
- | . |
* | . |
/ | . |
% | . |
Create the variable a = 16
and b = 5
. Test the operation of the following operators as follows (example for +):
int a = 16;
int b = 5;
int result1 = a + b;
printf("Result of 16 + 5 = %i",result1);
How else can you write the following code?
float a = 10;
float b = 4;
a = a / b;
printf("%i", a);
Assignment operator Fill in the table.
Symbol | Action |
---|---|
= | . |
+= | . |
-= | . |
*= | . |
/= | . |
%= | . |
Test the operators, write how they work in the comments. Below is an example of using the =
and +=
operators.
int a = 16;
int b = 5;
int result1 = a;
printf("result1 = %i",result1);
result1 += b;
printf("result1 += b = %i",result1);
Comparison operators Fill in the table.
Symbol | Operation |
---|---|
== | . |
!= | . |
< | . |
> | . |
<= | . |
>= | . |
int a = 16;
float b = 16;
bool result = a == b;
printf("a == b - %i",a==b)
Test the above operators. Print the results to the standard output.
Read the documentation for the scanf() function and then create a program that asks the user for the value of number a, then the value of number b and then prints the sum of these two numbers.
Read the documentation for the statements if and switch.
if (/* condition */)
{
/* code */
}
if (/* condition */)
{
/* code */
}else
{
/* code */
}
if (/* condition */)
{
/* code */
}else if (/* condition */)
{
/* code */
}else if (/* condition */)
{
/* code */
}else if (/* condition */)
{
/* code */
}else if (/* condition */)
{
/* code */
}else
{
/* code */
}
Write a program that asks the user to enter a number and then checks if the number is divisible by 2 and prints the appropriate information.
Write a program that asks the user for two numeric values and one character value + - * or /. Using the switch statement, perform calculations depending on the user’s choice.
create an arguments.c file and put the following code in it:
int main(int argc, char const *argv[])
{
printf("argc = %d\n", argc);
printf("argv[0] = %s\n",argv[0]);
printf("argv[1] = %s\n",argv[1]);
printf("argv[1] = %s\n",argv[2]);
return 0;
}
Compile the code into the arguments.exe
run file. Run the code with the command arguments.exe 12 hello "hello world"
Test the code by running the program with different arguments, e.g. arguments.exe 1 2 3 4
(How to read the value “4” from this command?), write in the comment what the argc and argv variables store.
Write a delta.exe program that accepts parameters a b c that will print the delta value in the standard output.
\[\Delta = b^2 - 4ac\]https://learn.microsoft.com/pl-pl/cpp/preprocessor/hash-include-directive-c-cpp?view=msvc-170
https://www.geeksforgeeks.org/data-types-in-c/#:~:text=Different%20data%20types%20also%20have%20different%20ranges%20up%20to%20which%20th ey%20can%20store%20numbers.%20These%20ranges%20may%20vary%20from%20compiler%20to%20compiler.%20Below%20is%20a%20list%20of %20ranges%20along%20with%20the%20memory%20requirement%20and%20format%20specifiers%20on%20the%2032%2Dbit%20GCC%20compiler.
https://learn.microsoft.com/en-us/cpp/c-language/c-operators?view=msvc-170
https://en.wikipedia.org/wiki/C_data_types#Boolean_type