C Notes
C Notes
/t = tab.
/a = alert. just makes the computer make a beep.
(') = Single quotation marks is for single character.
(") = Double quotation marks is for string.
===================================================================================
===================================================================================
=========
comments
------------------------------------------------------------------
// = one line comment.
/*stuff*/ = multi-line comment.
===================================================================================
===================================================================================
=========
converstion character.
-------------------------------------------------------------------
printf("Hello %s", "World"); = the %s works as a place holder. %s stands for string
printf("I ate %i apples", 4); = the %i stands for integer, which is basically a
whole number.
printf("I ate %d apples", 69); = %d specifies the type of variable as decimal.
printf("I have %f brain cells left", 2.1321341443); = the %f stands for float,
which is a decimal number.
printf("I have %.2f brain cells left", 2.1321341443); = the %.2f shows just 2
decimal places.
===================================================================================
===================================================================================
=========
variables
-------------------------------------------------------------------
int age;
age = 16;
or
int age = 16;
-----------------------------
printf("I'm %i years old", age);
===================================================================================
===================================================================================
=========
arrays
---------------------------------------------------------------------
\0 = this is a sting terminator, which tells the memory where does the string ends.
char name[7] = "string"; = this alocate a list in memory for each character. "It's
called an array" for Eg: 's' takes space 0 from the alocated space,
't' takes space 1 from the allocated space... and the last space is for the
sting terminator. note that you do not need to to spacify a number if the sting is
on the
same line.
strcpy(name, "water"); = changes the value of the array.
name[2] = 'z'; = changes the chracter based on the number. for eg: string became
stzing. the single qoutation is used for a single character and the double is used
for multi-
ple characters.
===================================================================================
===================================================================================
=========
headers
---------------------------------------------------------------------
#include <stdio.h> = tells the compiler to include the standerd input/output
library, before compileing.
#include <stdlib.h> = same but with the standerd library. the backets means search
the defult place where the headers at
#define CAR "buggati" = this is called a constent, which is basically just an
unchangable variable. this can also be used as a with %s... "note that it's better
to make it all
capital latters so that you can know what is it from a variable."
you can also make a new file and end it with .h to make constants in.
the "" means search the same foulder that our code at.
printf("i wanna buy a %s", CAR); = that is how to use a constent. "it works the
same as a variable, but the value cannot be changed".
===================================================================================
===================================================================================
===========
scanf
---------------------------------------------------------------------
char name[20];
int age;
printf("what is your name? \n");
scanf("%s", name);
printf("and how old are you %s? \n", name);
scanf("%d", age);
printf("hello %s! you are %d years old!", name, &age); == don't forget the "&"
arrays don't need it.
char firstName[20];
char lastName[20];
printf("what is your name: \n");
scanf("%s %s", firstName, lastName);
printf("\nHello, %s %s.", firstName, lastName);
===================================================================================
===================================================================================
===========
Math Operators
-----------------------------------------------------------------------
+ = plus
- = subtract
/ = devide
* = multiplie
% = the remainder of deviation
int watermelons = 50
printf("i ate %d watermelons", watermelons + 19); = i ate 69 watermelons
note that multiplication and deviation get's calculated first. = 4 + 2 * 6 == 16.
using () will calculate what's in the peranthathese first.
balance *= 1.1; = balance =balance * 1.1;
===================================================================================
===================================================================================
===========
float avgProfit;
int PriceOfPumpkin = 10;
int sales = 59;
int daysWorked = 7;
avgProfit = ((float) priceOfPumpkin * (float)sales) / (float)daysWorked; = the
(float) changes the number to float just for this occasion.
===================================================================================
===================================================================================
===========
if statements |note that you can also
compare characters EG: C is bigger then A and B "basically what comes after is
bigger" |
----------------------------------------------------------------|------------------
-----------------------------------------------------------------------------------
----------|
if(69 > 10){
printf("hello"); = if 69 is greater then 10 print hello, if not don't print
anything. don't forget the {} !!!
there is also the >= means if greater or equal, <= smaller or equal, == if
equal, ! means not. for eg: !<, !> or != .
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
if(777 > 142){
printf("i love cocaine");
if(777 <= 68){ = this is a nested if statement. which means an
if inside an if. also if the first "if" was false it will skip the nested "if"
statement.
printf("don't do drugs kids!");
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
else{ printf("pwend"); } = if the "if" statement is false, print the else
statement.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
if (665 => 666){printf("i have diarrhea"); }
else if (665 < 666){printf("nevermind"); } = the "else if" statement means that if
the "if" statement doesn't work/is false, use the "else if" statement.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
if ( (hoursStudied == 0) && (kidsBeatenUp >= 10) { printf("You are a good
student"); } = the "&&" means that the 2 statements where true, print "you are
a...". this wouldn't
work if one was true and the other was false.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
if ( (hoursStudied == 0) || (kidsBeatenUp >= 10) { printf("You are a good
student"); } = the "||" means or. this needs one of the variables to be true.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
(lastName[0] < 'M' ) ? printf("Blue Team") : printf("Red Team") ; = ? means if and
: means else. if character 0 is smaller then M print blue team else print red team.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
===================================================================================
===================================================================================
===========
Increment Operator
-----------------------------------------------------------------------
int tuna = 5;
"tuna++;" or "++tuna" = add one to the variable before or after the oparation.
tuna++ = "tuna * 6;" then "tuna + 1;". while ++tuna = "tuna + 1" then "tuna * 6".
this works with the "tuna--" or "--tuna" aswell.
===================================================================================
===================================================================================
===========
while loop
-----------------------------------------------------------------------
while(amount <= 5) {printf("the amount is less then 5"); amount++ = while the
amount is less then 5 print "the amount is less then 5" then add 1 to the amount.
it won't stop
unless the value reachs 5.