#include <stdio.h>// header file library that lets us work with input and output functions like printf// Stands for Standard Input Outputintmain(){printf("HellOOOOOOOOOOOOOO");return0; // return 0 if successful , return 1 if error}
Format specifier
Define type of variable to be displayed
//Declare variablefloat num =4.25;// print floatprintf("Number is %f", num);//Number is 4.250000//print float 2decimal numberprintf("Number is %2.f", num);//Number is 4.25
Float 6 - 7 digits
Double has double the digits 15 -16 digits
Format Specifier
Description
%d
For integers (decimal)
%u
For unsigned integers (decimal)
%o
For octal integers
%x, %X
For hexadecimal integers (lowercase or uppercase letters)
%f
For floating-point numbers (decimal notation)
%e, %E
For floating-point numbers (scientific notation with lowercase or uppercase "E")
%g, %G
For floating-point numbers (use %e or %f, depending on which is more compact)
%c
For characters
%s
For strings
%p
For pointers (prints the memory address)
%n
For writing the number of characters written so far to an integer pointer argument
%i
Same as %d, for integers
%a, %A
For hexadecimal floating-point numbers (lowercase or uppercase letters)
%lu
For long unsigned integers (decimal)
%lld, %ld, %d
For long long integers, long integers, and regular integers respectively
%lldu, %ldu, %du
For unsigned long long integers, unsigned long integers, and unsigned integers respectively
%Lf
For long double floating-point numbers
%%
For printing the % symbol itself
Type conversion
Implicit conversion (automatic)
Done automatically by the compiler when you assign a value of one type to another
Explicit conversion (manual)
Done manually by placing the type in parentheses ( )
Constants
Variable with fixed value that cannot be altered
Operators
alt text
Augmented Assigned Operators
+=
-=
*=
%=
Conditional statements
While loop
Ternary operators
Switch case
break vs continue
Logical operators
Arrays
Strings
In C programming, there is no string variable type. Instead, strings are represented as arrays of characters. Here's an example of how to declare and print a string in C:
Note that the string must be enclosed in double quotes and that the array must be big enough to hold the string, including the null terminator (\\0) at the end.
String Functions
Functions
C provides several built-in functions for working with strings. Here are a few examples:
strlen()
Returns the length of a string (excluding the null terminator).
strcpy() and strncpy()
Copies one string to another. strcpy() copies the entire string, while strncpy() lets you specify the maximum number of characters to copy.
strcat() and strncat()
Concatenates one string onto the end of another. strcat() concatenates the entire string, while strncat() lets you specify the maximum number of characters to concatenate.
strcmp()
Compares two strings and returns an integer indicating their relationship. Returns 0 if the strings are equal, a negative number if the first string is less than the second, and a positive number if the first string is greater than the second.
Single quote vs double quote
In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals.
<string.h>
Include header for string functions
NULL Character , \0
Used to indicate the end of a string
Array of Strings
Scanner (Input)
fgets
Read input from one line
alt text
<math.h>
Math header file
Functions
Function prototype
Declare function without body
Give information to compiler about function
Ensure that call to function is made with correct parameters
Structs
Similar to classes , BUT no methods
Typedef
In C, typedef is used to create an alias for an existing data type. This can be useful for creating shorter or more descriptive names for types, or for creating more abstract types that can be changed more easily in the future.
For example, you could define a new name for the int data type:
Now, you can use myInt instead of int:
You can also use typedef to create a new name for a struct:
Now, you can create a new Student struct like this:
This can make your code more readable and easier to understand, especially when dealing with complex data structures.
Enums
(Enumerations)
User defined values which consist of constant integers
// Automatic conversion: int to float
float myFloat = 9;
printf("%f", myFloat); // 9.000000
// Manual conversion: int to float
float sum = (float) 5 / 2;
printf("%f", sum); // 2.500000
//Cannot change const
const int myNum = 15;
myNum = 10;
//Examples
const int minutesPerHour = 60;
const float PI = 3.14;
//Good naming convention for const
//is ALL UPPERCASE
int YEAR = 2023;
//if else
if (condition1) {
} else if (condition2) {
} else {
}
//for loop
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
//while loop
while (condition) {
// ...
}
//do while loop ( Excecute code once , then repeat if true )
do {
// ...
}
while (condition);
//switch case
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
break; //breaks out of the loop
continue; //breaks out of the current iteration only
&& //and
|| //or
! //not
int myNumbers[] = {25, 50, 75, 100};
//dont need to include size of array as there is a initializer list,
// C will infer the size from the initializer
// SAME AS *int myNumbers[**4**] = {25, 50, 75, 100};*
//2D arrays / Matrix
int matrix[2][3] = {{1,2,3},{4,5,6}}
//2 Rows , 3 Col
char string1[] = "apple";
char string2[] = "banana";
int result = strcmp(string1, string2);
printf("%d", result); // Output: -1
char myStringOne[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
// Add null char for char array to end string
char myStringTwo[] = "hello";
// STRING LITERAL
// C automatically adds the null character to the end of the string.
char names[][10] = {"aloy","kar","tom"};
//same as matrix of characters
//meaning string array of max 10 items
char name[25];
scanf("%s", &name);
//Store the input at address of 'name'
//& gets the *address of*
printf("Your name is %s", name);
//fgets(variable to read to, input size, from where?)
char name[25];
fgets(name,25,stdin);
printf("Your name is %s", name);
//Basic Function
void sayHi(){
printf("HI!");
}
//Function with parameter with return type of int
int add(int num1,int num2){
int res = num1 + num2;
return res;
}
//Declare first without body
double sum(double num1 ,double num2);
int main(){
return 0;
}
double sum(double num1 ,double num2){
//....
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
// generate pseudo random number based on time
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
int n = rand();
printf("%d ", n);
}
return 0;
}
& //AND
| //OR
^ //XOR
~ //complement
<< //Shift left , same as times 2
>> //Shift right , same as divide by 2
int num = 42;
printf("The memory address of num is %p\n", &num);
//Prints the memory address of the variable on the RAM