Difference between revisions of "C Basics"

From Computer Science
Jump to: navigation, search
(Created page with "on the basics of C for you to study. We do not aim to present this information completely, but only to remind of things you should have learned reading. =C Data Types= Note...")
 
(C Data Types)
Line 2: Line 2:
  
 
=C Data Types=
 
=C Data Types=
Note - # of bytes is for on the CS server using gcc.  This varies depending on your system and compiler.
+
Note - # of bytes is for on the CS server using gcc (as of 2022).  This varies depending on your system and compiler.
 
* ''integer data types'': <code>char</code> (1 byte), <code>short</code> (2 bytes), <code>int</code> (4 bytes), <code>long</code> (8 bytes)
 
* ''integer data types'': <code>char</code> (1 byte), <code>short</code> (2 bytes), <code>int</code> (4 bytes), <code>long</code> (8 bytes)
 
** ''signed/unsigned'': integer types are <code>signed</code> by default, use <code>unsigned</code> before any of the above for unsigned.  
 
** ''signed/unsigned'': integer types are <code>signed</code> by default, use <code>unsigned</code> before any of the above for unsigned.  

Revision as of 00:12, 6 September 2022

on the basics of C for you to study. We do not aim to present this information completely, but only to remind of things you should have learned reading.

C Data Types

Note - # of bytes is for on the CS server using gcc (as of 2022). This varies depending on your system and compiler.

  • integer data types: char (1 byte), short (2 bytes), int (4 bytes), long (8 bytes)
    • signed/unsigned: integer types are signed by default, use unsigned before any of the above for unsigned.
    • max/min values: for unsigned types, the value ranges from 0 to 2# of bits-1. For signed types, the value ranges from (-2# of bits -1) to (2# of bits - 1). E.g., char ranges from -128 to 127, while unsigned char ranges from 0 to 255.
  • floating point data types: float (4 bytes), double (8 bytes), long double (16 bytes)
    • precision and max/min values: each floating point type uses 1 bit for the sign, most of the bits for the mantissa (aka significand), and the remainder for the exponent. float uses 23 bits for the significant, resulting in about 7 digits of precision, and 8 bits for the exponent, meaning the exponent can range from -128 to 127 and the maximum value is around 1038. double uses 52 bits for the mantissa and 11 bits for the exponent.
  • pointers - memory address, note that sizeof(char *) is however many bytes a memory address takes up, which in 2022 on the CS server is 8.
  • any type of data in C can be interpreted as an integer and printed that way
  • C string - array of characters, "last" character in the string is '\0' (literally all 0 bitss in the byte), also can use NULL (a synonym for '\0')
  • array - block of memory large enough for multiple int's (or float's, etc.). Declared like int A[10]; In the program, A evaluates to the address of the first element in the array.

Operators

  • Bit operations - & and, ^ xor, | or, ~ complement, << left bit shift, >> right bit shift
  • Logical (Boolean) operations - && and, || or, ! not
  • Arithmetic - + - * / %
  • Assignment - =, also = after all bit/arithmetic operations (+=, -=, &=, etc.)
  • Memory access - & gives the address of a variable, * dereferences (aka gets the value of) a pointer variable
  • Precedence and associativity - see https://en.cppreference.com/w/c/language/operator_precedence

Grouping

  • { } for compound statements
  • ( ) for order of operations / grouping expressions
  • ( , ) for defining functions, calling functions
  • [ ] for indexing arrays

Statements

  • for - for ( initializer ; loop condition ; increment ) loop body statement
  • while - while ( loop condition ) loop body statement
  • do while - runs loop body at least once, before checking the loop condition
  • continue - for the innermost currently executing loop, go back to the beginning of that loop
  • break - for the innermost currently executing loop, break out of the loop and start with the code right after the loop
  • break - used in a switch statement to break out of the switch statement
  • switch - only for integer types (including char's), note that must use break with each case (or multiple case's will run the same code). If none of the cases's given, and if a default is given, then the default is run.
  • if / else

Variables, Data, Memory

  • Initialization - is not guaranteed, you must initialize all variables.
  • Scope - variables exist within the { } they are declared within, globals are declared outside of functions and have scope for the whole file
  • malloc/free - for allocating and freeing dynamically allocated memory (from the heap)
  • pointers / memory address - & gets the address (aka memory location) of a variable, * gets the value at a given memory address, int * declares a pointer variable that stores a memory address
  • array - if declared as int A[10]; then A[i] is the (i+1)st slot in the array, and A (on its own, no brackets) is the address of the first slot in the array
  • Basic 4 types of memory - local variables (kept on "the stack"), global variables (kept in "data"), the program code (kept in "code"), dynamic memory (kept in "the heap")
  • The stack - local variables automatically allocated space, stack pointer keeps track of the top of the stack, at end of } local variables are "free'd" by changing the stack pointer (so next allocated variables will overwrite previous ones). Stack memory is limited (e.g., 1MB), so large arrays and such should be allocated in the heap or as global variables. Stack memory is reused, which is one of the reasons that variables should always be initialized.
  • The heap - big chunk of memory, malloc allocates space, free frees it, it's up to program to keep track of malloc'ed memory. Can be as large as memory.
  • Code - memory that has the program code itself is read/execute-only.
  • Data - global variables are known at compile time, space is allocated when the program is loaded and persists until program is finished, can declare large arrays as globals. Can be larger than the stack, still best practice to store really large things in the heap.

Functions

  • Parameters are passed by value, use pointers to have modified variables "returned"
  • return - returns value from function, stops function

Pre-processor

  • Is executed before compiling - first pre-processor is run through the entire file(s), second the compiler runs
  • #include - it is as if that file were copy/pasted into the present file
  • #define PI 3.14 - when pre-processor is run, all occurrences of PI in the program will be replaced with 3.14, before the compiler is run.

Command-Line Arguments

  • argc is count, argv is array of C strings, argv[0] is the name of the program itself, argv[1] is first actual argument, arguments are all C strings so use atoi or atof if need to convert to int or float.

Other Punctuation

  • comments // to the end of the line or /* multiple lines */

Standard Library

  • string functions - strlen, strcmp, strstr, strtok, atoi, atof, strcpy
  • I/O - printf, scanf, fgetc, getline, fgets, fprintf, fscanf, fputc
  • math functions - (use -lm flag when compiling with gcc), log, log2, log10, sqrt, pow