Difference between revisions of "C Programming - Getting Started"

From Computer Science
Jump to: navigation, search
(Variables and Data)
(C Data Types)
Line 66: Line 66:
  
 
===C Data Types===
 
===C Data Types===
Note - # of bytes is on the CS server using gcc.  This varies depending on your system and compiler.
+
Note - # of bytes is in () for on the CS server using gcc.  This varies depending on your system and compiler.
 
* char (1), short (2), int (4), long (8), unsigned (modifier for the integer types), float (4), double (8), long double (16).
 
* char (1), short (2), int (4), long (8), unsigned (modifier for the integer types), float (4), double (8), long double (16).
 
* pointers - memory address
 
* pointers - memory address

Revision as of 13:42, 29 August 2019

This bootcamp is part of the Programming / CS Bootcamps

For a video explaining how to get started with this bootcamp, see https://youtu.be/WYTDuKRXWJQ

Getting started with C programming

  1. Reading - start reading through one of the following (pick one that seems an easy read for you) before you start working on the programming problems.
    1. The C Book - a bit older, suitable for people with just a bit of programming experience
    2. C Programming Tutorial - suitable for people with no previous programming experience
    3. Fresh2Refresh Tutorial - more of a summary, easy to find particular topics
    4. cplusplus.com - contains references for functions, often one of the first search results on google searches for C
    5. MIT C course - with lecture notes and assignments
    6. Cornell C course - with lecture notes and assignments
  2. Work on solving these problems - https://www.hackerrank.com/domains/c. The next section lists problems to work on - from very easy up to more interesting.
  3. Get C installed on your computer - download and install some C compiler/IDE. We use gcc (see https://gcc.gnu.org/install/binaries.html). You could use any compiler/IDE that is C (not C++). Eclipse (see https://www.eclipse.org/downloads/) would be fine. So would MS Visual Studio (see https://visualstudio.microsoft.com/). Or Clang (see http://releases.llvm.org/download.html). Or some in-browser C site, like https://repl.it/languages/c or https://www.onlinegdb.com/online_c_compiler.
  4. If you are a current or incoming ISU student, or an ISU alumni, sign up for the ISU CS Mattermost at https://judy.indstate.edu. Note that you need to use your @sycamores.indstate.edu email address to sign up. Look for the C Programming Channel (direct link is https://judy.indstate.edu/isu-cs/channels/c-programming). When asking about the hackerrank problems make sure to refer to them using the title hackerrank gives them.

Let's see how far you can get with solving the hackerrank problems! Good luck!

List of Problems

Here are problems to work on from the hackerrank set. Note that you should work on more than just this set, but these are ones we are ready to help you solve!

Basic Programming

These problems are all pretty basic - require a single loop, if statement, etc. They are good when you are just getting started with C.

A Bit More Involved

These problems require nested loops, working with arrays, or other things that are the next level of difficulty. Solve all of the Basic Programming problems before starting on these.

And a Bit More

These problems are still a bit more involved. Some require some abstract thinking about the problem, a programming "trick", or other key insight. Note that at this point you are working on problems such that some of the lab assistants in the unix lab may not have solved these problems. This is where you want to be at - you have mastered the basics and are working on problems independently!

After That

If you are able to do all of the problems above, then you don't need us to give you lists of problems any more. You can pick problems to work through on your own. Some suggested places with problems are as follows.

On CS Systems - gcc, g++

For CS courses that use C and C++, the gcc and g++ compilers are normally used. These are already installed on the CS server. If you have a C or C++ program, you compile it by first logging into the CS server using Putty or another terminal program and typing

gcc code.c -o outputName

for C, or g++ for c++. You run the resulting program by typing ./outputName (and then enter/return) at the shell prompt (aka command line). For programs that you are assigned as part of a course, you need to make sure your C/C++ programs compile and run using gcc/g++. Note that programs made using MS Visual Studio or some other compiler will normally not work straight away with gcc/g++ because there are some differences in which functions are included in the header files.

Study Guide

Here we keep some notes on the basics of C for you to study. We do not aim to present this information completely, but only to remind of of things you should have learned reading through one of the above resources.

C Data Types

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

  • char (1), short (2), int (4), long (8), unsigned (modifier for the integer types), float (4), double (8), long double (16).
  • pointers - memory address
  • everything is really an integer (print them as integers)
  • C string - array of characters, "last" character in the string is \0 (literally all 0's in the byte), also can use NULL

Operators

  • Bits - & and, ^ xor, | or, ~ complement
  • Logical - && and, || or, ! not
  • Arithmetic - + - * / %
  • Assignment - =, also = after all bit/arithmetic operations (+=, -=, etc.)

Grouping

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

Statements

  • for - order of ops is - initializer, check condition, body, increment, check condition, ...
  • while - order of ops is - check condition, body, check condition, ...
  • do while - order of ops is - body, check condition, body, ...
  • break / continue
  • switch - only for integer types
  • if / else

Variables, Data, Memory

  • Initialization - is not guaranteed, you must initialize
  • 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 declaring dynamically allocated memory
  • pointers / memory address - & gets the address (aka memory location) of a variable, * gets the value at a given memory address

Functions

  • Parameters are passed by value, use pointers to have modified variables "returned"

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

Command-Line Arguments

  • argc is count, argv is array of C strings

Other Punctuation

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

Standard Library

  • string functions - strlen, strcmp
  • I/O - printf, scanf