Syntax/Code in C
if branch
A branch taken only if an expression is true.
branch
A sequence of statements only executed under a certain condition.
printf();
Format and print data
%d
Integer format character - used to indicate the placement of a variable in text
/* Notes */
Multi-line comment (can be used for single line)
%%
Prints the '%' character.
printf("%d", variableName);
Prints the variable value of an integer
pow(x, y);
Returns the value of x to the power of y
srand()
Seeds the random number generator
// Notes
Sets off a single line comment, does not need to be ended off with anything
reserved word/keyword
Word (or symbol) that has a meaning already defined in the language, CANNOT be used as an identifier
for loop
a loop that iterates a specified number of times
loop
a program construct that repeatedly executes the loop's statements (known as the loop body) while the loop's expression is true; when false, execution proceeds past the loop
while loop
a programming construct used to repeat a set of commands (loop) as long as (while) a boolean condition is true
floating point number (ex. 1.0, 89.2, -0.9993)
a real number with a decimal portion
character literal
a single character that appears in single quotes (') in a program
array
a single variable storing an ordered list of items (known as elements), each being directly accessible
break;
a statement within a loop that causes an immediate exit of the loop
continue;
a statement within a loop that causes the program to jump to the loop condition check
+
addition operator
strcat(userCaption, "string");
adds the specified string onto the end of the first string
#include <time.h>
allows use of time()
character array (ex. variableName[##])
an array whose components are of the type char
identifier
an attribute (or group of attributes) whose value is associated with one and only one entity instance must be a sequence of letters, numbers, and underscores, and START with a letter or underscore
=
assigns values from right side operands to left side operand, variable to be assigned goes on the LEFT
++i
because i = i + 1 is so common in programs, the programming language provides a shorthand version (pre-increment operator)
--i
because i = i - 1 is so common in programs, the programming language provides a shorthand version (decrement operator)
int main() {}
beginning of function named main
arrayName[#]
calls on a specific index of an array
userString[#]
calls upon a specific character index in a string note the first character in the string has an index of 0
arrayName[#] = ##
changes the specified index of the array to the given value
null character
character '\0' that marks the end of a string in C
(type)variableName
converts a variable to another type, specified by the contents of the parentheses
tolower('A');
converts the specified character to lowercase, does not modify an argument - only returns a value
toupper('a');
converts the specified character to uppercase, does not modify an argument - only returns a value
strlcpy(str1, str2, n)
copies the first n characters of str2 into str1
int arrayName[R][C]
creates a 2-D array with R rows and C columns
long long variableName;
creates a 64-bit variable instead of a 32-bit one, for numbers greater than 2B that generate an overflow
const int/double VARIABLE_NAME;
declares a constant variable, will produce an error if future code attempts to modify it
int arrayName[# of elements];
declares an array with a specified number of elements within brackets
int variable;
declares an integer variable
/
division operator NOTE: when dividing an integers, outputs a truncated integer with no decimal places
iteration
each time through the loop's statements
#include <stlib.h>
enables use of the rand() function
\
escape sequence - used to display certain characters such as " ' and \ (ex. \' \" \\)
!=
evaluates to true if the left and right sides are NOT equal
if (condition) { expression; } else if (condition) { expression; } else { expression; }
format for if-else if-else branches
>
greater than operator
>=
greater than or equal to operator
%lf
indicates a double within a string literal
scanf("%d", &(arrayName[#]));
inputs an integer value into the given array at the specified index #
<
less than operator
<=
less than or equal to operator
incrementing the variable
means adding a value to it. (Specifically, the term often means to add 1 to a variable.)
%
modulus operator, outputs the remainder of integer division
switch (variableName) { case 1: code; break; case 2: code; break; default: code; break; }
more clearly represent multi-branch behavior involving a variable being compared to constant values
*
multiplication operator
\n
new line
\0
null character in string, at the end of a string
%.#lf (where # is a natural number)
outputs # number of digits after the decimal point in a double calculation
strlen(variableName)
outputs the number of characters in a string
order of precedence for operators
parentheses logical not (!) arithmetic operators relational operators equality/inequality operators logical AND logical OR
\t
places a tab within a string literal
%lld
prints a long long int within a string literal
%c
prints a single ASCII character
%s
prints a string
#include <ctype.h>
provides access for several functions to work with characters
{index0, index1, ..., indexN}
used to initialize an array with specific values
M_PI
value of pi
return 0;
terminates the main() function, needed at the end of the code
string literal
text in double quotes
#include <math.h>
the math library with several math constants, such as pi
a && b
true when BOTH of its operads are true
a || b
true when at least one of its operands are true
!a
true when its single operand is false
#include <stdio.h>
tells the compiler to include information about the standard input/output library
strcpy(str1, str2)
Copy the contents of str2 into str1
fabs(x);
Takes the absolute value of x, returns the number as a positive float
binary
The ??? number system is base 2, using only bits 0 and 1.
==
The equality operator (sometimes read: "equal equal") is used to compare two values, and returns a Boolean (true/false). Avoid confusion with the assignment operator "=", Note: string and float comparisons sometimes do not behave as expected
if-else branch
The first branch is taken if an expression is true, else the other branch is taken.
comparing floats/doubles
rather than comparing for equality, compare if they are 'close enough' ex. fabs(x - y) < 0.0001;
userString[#] = 'x'
replaces the specified index with the specified character
#include <string.h>
required to be included to use strcpy
rand() % N
restricts the outputs to integer values less than N but greater than 0
strcmp(string1, string2)
returns 0 if two strings are equal returns a non-zero value if strings are unequal (negative if string1 is less than string2, positive if string1 is greater than string2)
rand()
returns a random integer from 0 to RAND_MAX (usually machine-dependent)
time(0)
returns the number of seconds since 1/1/1970
isdigit('5');
returns true if the specified character is a digit 0-9
isalpha('x');
returns true if the specified character is a letter in the alphabet
isspace(' ');
returns true if the specified character is whitespace, including newlines
#.##e# (where #s serve as digits, no limit on the number, and the exponent can be + or -)
scientific notation written in C
compound operators - +=, -=, *=, /=, %=
shorthand way to update a variable
double variable;
stores a floating-point number
char variableName;
stores a single character, surrounded in single quotes when assigned to a variable (ex. 'm' or '*')
for (initialExpression; conditionExpression; updateExpression) { // Loop body } ex) for (int i = 0, i < 5; i = i + 1) { //loop contents }
structure of a for loop (note: the looping variable can be declared within the loop)
-
subtraction or negation operator
while (expression) { //loop body }
syntax of a while loop, statements inside brackets execute as long as the expression evaluates to true; if false, the code runs after the brackets
scanf("%s", variableName);
takes input of a string, NOTE that no '&' is required
sqrt(number);
takes the square root of the value (can be an expression/variable) inside parentheses