RELG 456 Lecture Notes - Lecture 1: C Preprocessor, A.Out, Scanf Format String

5 views2 pages
IDE (Windows laptop)
- Option 1: run Ubuntu in a VM
- use VMWare or VirtualBox, doesn't really matter which
- use Visual studio as an IDE within Ubuntu
- to compile: open a terminal, navigate to the folder of your C file, then
run the command gcc filename.c
- compilation creates an a.out folder
- to run: ./a.out
- Option 2: use McGill's provided Linux servers
- SSH into [email protected], use your McGill password and
port 22
- use any IDE on your Windows computer to write your C files
- use FileZilla or the command line to move your C files from your
computer to the server
- compile and the same way as Option 1
Tutorial 1 Powerpoint
Programming in C
- build-in functions from libraries: use #include<header_file_name> at the top
- custom functions for re-used code: same declaration format as usual
- return type
- if a type is specified (i.e. not void), something must be returned
- you can return NULL if you don't actually want to return any
info - function name
- argument type and name (for as many arguments as needed)
NOTE: to learn about most of the topics covered in this tutorial, it's best to
download the code samples from myCourses and run them yourself. Try changing or
removing items and seeing how the output changes.
Basic Output
- no additional notes
Basic Input
Format Specifiers
- when a printf function is run, it scans the string (first argument) from
left to right, and prints the characters
- if it encounters a % sign, the behaviour changes: the character after
the % specifies how the next variable in the list of variables should be printed
- all the arguments after the first (remember, the first is a
string) are variables that get inserted into the string in a manner depending on
the format specifier
- the same occurs for scanf
- in the scanf case, the format specifiers set aside memory for the
input in the input buffer (which is called stdin)
- %d = convert the next value to a signed decimal integer
- %f = convert the next value to a float or double signed decimal
- %c = convert the next value to a char single character
- %s = convert the next value to an array of char sequence of characters
(in other languages, we'd call this a "string")
Manual Look-up
- too look something up in the manual, type "man <func_name>"
Data Types in C
- NOTE: instead of writing out the format of how to declare each data type
here, it's better to learn using properly-formatted resources. I've provided
some links.
- system-defined:
- int, float, double, bool, Short, Long, Long double
- user-defined:
- Struct (short for "structure")
- when variables need to be identified as a group, they can be
grouped under a single structure
- these variables are called member variables of the group
- https://www.tutorialspoint.com/cprogramming/c_structures.htm
- Union
- allows you to store different data types in the same memory
location - a union can have many membes, but only one member can contain a
value at a given time
- https://www.tutorialspoint.com/cprogramming/c_unions.htm
Arrays
- used to create n objects of a partiular data type, occupying memory in a
continuous fashion
- can be either system-defined or user-defined
- e.g. you can have an array of ints, or an array of struct
triangles
- size of the array is defined in [square brackets]
Conditional Statements - usual if-else blocks as in other languages
- https://www.tutorialspoint.com/cprogramming/c_decision_making.htm
Loops - for loops and while loops, same as other languages
- https://www.tutorialspoint.com/cprogramming/c_loops.htm
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Option 1: run ubuntu in a vm. Use vmware or virtualbox, doesn"t really matter which. Use visual studio as an ide within ubuntu. To compile: open a terminal, navigate to the folder of your c file, then run the command gcc filename. c. Option 2: use mcgill"s provided linux servers. Ssh into mcgill. username@mimi. cs. mcgill. ca, use your mcgill password and port 22. Use any ide on your windows computer to write your c files. Use filezilla or the command line to move your c files from your computer to the server. Compile and the same way as option 1. Build-in functions from libraries: use #include at the top. Custom functions for re-used code: same declaration format as usual. If a type is specified (i. e. not void), something must be returned. You can return null if you don"t actually want to return any. Argument type and name (for as many arguments as needed)

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents