Untitled
Anonymous
plain_text
03/02/2026 3:27 PM
8.6 KB
13
Indexable
#include <stdio.h> // Include me - library of input / output functions
# include <math.h> // Include me - library of mathematical functions
// This is how you comment in C
// Functions!
// In C, we use functions defined by what they return.
int square(int p) { // You put the functions above the main.
return p * p;
}
// We can also have procedures. Procedures don't return a value!
void moo() {
printf("moo");
}
int main() { // Always need a main!
printf("This is how you type!\n");
printf("Hello World\n"); // Put a new line at the end of every print!
/* Note that the code that actually runs is always in the Source Files.
*This is how you do a multi-line comment btw.*/
// Unlike Python, you need to tell C what your variable is. For example...
int x; // define a variable named x of type int
x = 2;
// You can define variables more efficiently also:
int y = 5;
int z = (0);
// You can concatenate words!
printf("%s%s", "Hello ", "Turtles!\n"); // Use of "%s%s" tells the computer were connecting two strings together.
// We can also take user inputs.
// First define a space for it:
char name[50]; // This is a 49 character buffer. (Last character for null terminator, indicating end of string)
printf("Enter your name\n");
scanf_s("%49s", name, 50); // Feeding in 49 letters (%49). Any more and we get an overflow error. The 50 tells us the name was 50 long
printf("Your name is %s\n", name);
// Printing numbers
int a = 0, b = 1, c = 2, d = 3, e = 4;
a = b - c + d * e;
printf("%d\n", a); // Will print 11. %d means a decimal number!
/* For Boolean variables (True or False), we can use
* ints. 0 for false, 1 for true! */
// We can also make arrays!
int myList[3];
myList[0] = 10;
myList[1] = 20;
myList[2] = 30;
printf("The second number in the array is %d\n", myList[1]);
// We can also make 2D arrays!
int twoDArray[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
printf("The third number in second row of array is %d\n", twoDArray[1][2]);
// Say you want an array to be all zeros...
int zeroArray[10] = { 0 };
// And yes, we can do IF statements!
int foo = 1;
int bar = 2;
if (foo < bar) {
printf("foo is smaller than bar.\n");
}
else if (foo == bar) {
printf("foo is equal to bar.\n");
}
else {
printf("foo is greater than bar.\n");
}
// Operators - AND, OR, NOT
// AND = &&
// OR = ||
// NOT = !
// DIVISION
// Integer division: int x = i/j
// Float division: float x = (float)i / j
// When making a string, it's actually an array:
char yourName[] = "Freida Smith";
// And we can print string lengths
printf("%d\n", strlen(yourName)); // So the percentage is the placeholder and the other paramater you put in is what goes in the place holder.
// We can compare strings also:
char hisName[] = "John";
if (strncmp(hisName, "John", 4) == 0) { // Returns 0 if equal. Takes in the two strings to be compared, and their maximum comparison length.
printf("Hello John!\n");
}
else {
printf("You aren't John! Go away! \n");
}
// Lastly, we can concatenate strings
char first[20] = "Hello "; // Ensure this string is big enough to add to the end
char second[] = "World!";
strncat(first, second, 6); // Using 6 since World! is 6 characters. Always make sure it's the right size
printf("%s\n", first);
// For loops
int i;
for (i = 0; i < 10; i++) { // Note: i++ increments i by 1 each time. Essentially i+=1
printf("%d\n", i);
}
// While loops
int n = 0;
while (n < 10) {
printf("%d\n", n);
n++;
}
// Infinite while loops.
int p = 0;
while (1) {
p++;
if (p == 10) {
break; // The break command ends the loop
}
}
// The continue command
int m = 0;
while (m < 10) {
m++;
// Check that m is odd
if (m % 2 == 1) {
continue; // Goes back to the start of the while block
}
// You reach this if the number is even.
printf("The number %d is even.\n", m);
}
// Calling my functions defined above main.
int theResult;
int q = 5;
theResult = square(q);
printf("The square of %d is %d\n", q, theResult);
// Calling my procedure defined above main.
moo();
/* Static and Dynamic Variables - Scope.
* If I have a variable in a function then call that function from main, the value will stay the same and it will
* change back to its normal value after the function has been called. We can prevent this by using the static keyword.
* This means the variable will keep whatever value it has and won't reset back after the function is called.
*/
//-----------------------------------------------------------------------------------------------------------------------
//int runner() {
//int count = 0; <--- This is a normal local variable
//count++;
//return count;
//}
//printf("%d ", runner());
//printf("%d ", runner());
// ^^^ So if this code is called, it will output 11.
//-----------------------------------------------------------------------------------------------------------------------
// But with Static...
//int runner() {
//static int count = 0; <--- Static variable - duration changed
//count++;
//return count;
//}
//printf("%d ", runner());
//printf("%d ", runner());
// ^^^ So if this code is called, it will output 12.
//-----------------------------------------------------------------------------------------------------------------------
// We can also have static functions.
/* By default, in C, functions are global meaning that they can be accessed in the file (source.c) and outside the file
like if you had another source file i.e. source2.c */
// We can make functions static so they can only be accessed inside the file that they are in. See below:
//static void fun() { <--- Just chuck static infront!
//printf("I am a static function.");
//}
// Very lastly, #DEFINE.
#define PI 3.14159
// Now every time you write PI, the programme will sub 3.141592 in - saves mucking about!
// To write to a file, see slide 12 of Lecture 1 - getting started!
return 0; // This is saying that if 0 is returned, all ran well. If 1 was returned, it would be an error.
}
Editor is loading...
Leave a Comment