[SPCA] Continue summary

This commit is contained in:
2026-01-06 10:52:34 +01:00
parent 74205e8cf0
commit 13e3210298
20 changed files with 79 additions and 9 deletions

View File

@@ -0,0 +1,17 @@
// This is a line comment
/* this is a block comment */
#include "01_func.h" // Relative import
int i = 0; // This allocates an integer on the stack
int main( int argc, char *argv[] ) {
// This is the function body of a function (here the main function)
// which serves as the entrypoint to the program in C and has arguments
printf( "Argc: %d\n", argc ); // Number of arguments passed, always >= 1
// (first argument is the executable name)
for ( int i = 0; i < argc; i++ ) // For loop just like any other sane programming language
printf( "Arg %d: %s\n", i, argv[ i ] ); // Outputs the i-th argument from CLI
get_user_input_int( "Select a number" ); // Function calls as in any other language
return 0; // Return a POSIX exit code
}

View File

@@ -0,0 +1,43 @@
#include "01_func.h"
#include <stdio.h>
int get_user_input_int( char prompt[] ) {
int input_data;
printf( "%s", prompt ); // Always wrap strings like this for printf
scanf( "%d", &input_data ); // Get user input from CLI
int input_data_copy = input_data; // Value copied
// If statements just like any other language
if ( input_data )
printf( "Not 0" );
else
printf( "Input is zero" );
// Switch statements just like in any other language
switch ( input_data ) {
case 5:
printf( "You win!" );
break; // Doesn't fall through
case 6:
printf( "You were close" ); // Falls through
default:
printf( "No win" ); // Case for any not covered input
}
while ( input_data > 1 ) {
input_data -= 1;
printf( "Hello World\n" );
}
// Inversed while loop (executes at least once)
do {
input_data -= 1;
printf( "Bye World\n" );
if ( input_data_copy == 0 )
goto this_is_a_label;
} while ( input_data_copy > 1 );
this_is_a_label:
printf( "Jumped to label" );
return 0;
}

View File

@@ -0,0 +1,4 @@
#include <stdio.h> // Import from system path
// (like library imports in other languages)
int get_user_input_int( char prompt[] );

View File

@@ -0,0 +1,33 @@
int my_int; // Allocates memory on the stack.
// Variable is global (read / writable by entire program)
static int my_local_int; // only available locally (in this file)
extern const char *var; // Defined in some other file
const int MY_CONST = 10; // constant (immutable), convention: SCREAM_CASE
enum { ONE, TWO } num; // Enum. ONE will get value 0, TWO has value 1
enum { O = 2, T = 1 } n; // Enum with values specified
// Structs are like classes, but contain no logic
struct MyStruct {
int el1;
int el2;
};
int fun( int j ) {
static int i = 0; // Persists across calls of fun
short my_var = 1; // Block scoped (deallocated when going out of scope)
int my_var_dbl = (int) my_var; // Explicit casting (works between almost all types)
return i;
}
int main( int argc, char *argv[] ) {
if ( ( my_local_int = fun( 10 ) ) ) {
// Every c statement is also an expression, i.e. you can do the above!
}
struct MyStruct test; // Allocate memory on stack for struct
struct MyStruct *test_p = &test; // Pointer to memory where test resides
test.el1 = 1; // Direct element access
test_p->el2 = 2; // Via pointer
return 0;
}

View File

@@ -0,0 +1,20 @@
#include <stdint.h>
#include <stdio.h>
int main( int argc, char *argv[] ) {
int data[ 10 ]; // Initialize array of 10 integers
data[ 5 ] = 5; // element 5 is now 5
*data = 10; // element 0 is now 5
printf( "%d\n", data[ 0 ] ); // print element 0 (prints 10)
printf( "%d\n", *data ); // equivalent as above
printf( "%d\n", data[ 5 ] ); // print element 5 (prints 5)
printf( "%d\n", *( data + 5 ) ); // equivalent as above
int multidim[ 5 ][ 5 ]; // 2-dimensional array
// We can iterate over it using two for-loops
int init_array[ 2 ][ 2 ] = {
{1, 2},
{3, 4}
}; // We can initialize an array like this
int empty_arr[ 4 ] = {}; // Initialized to 0
return 0;
}

View File

@@ -0,0 +1,15 @@
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] ) {
char hello[ 6 ] = "hello"; // Using double quotes
char world[ 6 ] = { 'w', 'o', 'r', 'l', 'd', '\0' }; // As array
char src[ 12 ], dest[ 12 ];
strncpy( src, "ETHZ", 12 ); // Copy strings (extra elements will be set to \0)
strncpy( dest, src, 12 ); // Copy strings (last arg is first n chars to copy)
if ( strncmp( src, dest, 12 ) ) // Compare two strings. Returns 1 if src > dest
printf( "Hello World" );
strncat( dest, " is in ZH", 12 ); // Concatenate strings
return 0;
}