mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-01-12 01:58:24 +00:00
[SPCA] Restructure, intro to C continued
This commit is contained in:
@@ -13,4 +13,5 @@ int main( int argc, char *argv[] ) {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#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
|
||||
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 )
|
||||
@@ -11,6 +13,7 @@ int get_user_input_int( char prompt[] ) {
|
||||
else
|
||||
printf( "Input is zero" );
|
||||
|
||||
// Switch statements just like in any other language
|
||||
switch ( input_data ) {
|
||||
case 5:
|
||||
printf( "You win!" );
|
||||
@@ -21,17 +24,20 @@ int get_user_input_int( char prompt[] ) {
|
||||
printf( "No win" ); // Case for any not covered input
|
||||
}
|
||||
|
||||
int input_data_copy = input_data;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
32
semester3/spca/code-examples/00_c/02_declarations.c
Normal file
32
semester3/spca/code-examples/00_c/02_declarations.c
Normal file
@@ -0,0 +1,32 @@
|
||||
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)
|
||||
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;
|
||||
}
|
||||
3
semester3/spca/code-examples/00_c/03_operators.c
Normal file
3
semester3/spca/code-examples/00_c/03_operators.c
Normal file
@@ -0,0 +1,3 @@
|
||||
int main( int argc, char *argv[] ) {
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user