CSC 315 Made Easy by B.SC
CSC 315 Made Easy by B.SC
THE CONTENT OF THIS MATERIAL CUT ACROSS THE FIVE TOPICS GIVEN TO US BY MR. UGWU:
1. Preprocessors
2. Comments
3. Variables
4. If-else Statements
5. Function
Study meaning of each term and make sure to read beyond the context of this material. IT DOES NO HARM!!!
#include <stdio.h> //Preprocessor (Header file). Stdio = standard input and output
#include <stdbool.h> // To use boolean
//COMMENTS IN C
//1. Single line, 2. multiline
/* 2. i am a
multi line
comment */
//VARIABLES IN C
//synthax for declaring variables in c:
/*
1. datatype VariableName; FOR VARIABLE DECLARATION.
VariableName = value; TO INITIALISE VariableName
E.g
int myAge; myAge declaration. here int is the data type
myAge = 10; myAge initialization. here 10 is the value
2. datatype variableName = value; //DECLARATION and Initialization
E.g
int yourAge = 11;
*/
//You can set it to decimal point of your choice, maximum usually being 16 decimal places
//Its best practise to use double more than float
//because double is of more higher precision
//using "%s" to output array of characters. i.e something like String. but there is no Str
ing in c
char multipleLetters[] = "I am just an array of Characters not a String";
//mind the double quote ""
printf("\n%s",multipleLetters);
//using "%s" to output array of characters. but in this case some specific amount of chara
cters
char specificmultipleLetters[33] = "I am just an array of Characters not a String";
//mind the double quote "" and number parsed inside []. i need just 33 characters
printf("\n%s",specificmultipleLetters);
bool iAmTrue = true; //To use boolean in c, #include <stdbool.h> must be included at the t
op file
printf("\n%d", iAmTrue);
CONDITIONAL STATEMENTS
//IF STATEMENT
#include <stdio.h>
#include <string.h> //to use string functions e.g
//strcmpi which compare strings and ingore case
int main(){
//IF-ELSE WITH ARRAY OF CHARACTERS
char name[] = "Bsc";
char name1[4];
printf("Enter Your name: ");
scanf("%s",&name1); //Scanf is used to collect inputs without space from users
//e.g your first name.
//use fgets function if there is need to collect inputs with space eg. your fullname
if(strcmpi(name,name1) == 0){
printf("\nYes %s is the name",name);
}else{
printf("Name does not match");
}
if(age == 18){
printf("Age is %d",age);
}else{
printf("Age is not displayed because age is not 18");
}
switch(value){
case 1:
printf("You entered 1\n");
break; //Without break it will keep printing to the last line
case 2:
printf("You entered 2");
break;
default:
printf("Value entered is not valid");
return 0;
LOGICAL OPERATOR IN C
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
int main(){
int age = 15;
bool ageChecker = false;
if (!imGonnaTurnFalse){
printf("\nYeah, you've been turned False");
}else
{
printf("\nFuck, i've been Turned Off by this stupid NOT operator\n\n");
}
return 0;
MATH FUNCTION IN C
#include <stdio.h>
#include <math.h>
int main(){
double A = sqrt(16);
double B = pow(3,3);
int C = round(4.55);
int D = ceil(3.55);
int E = floor(3.55);
double F = fabs(-2);
double G = log(10);
double H = sin(90);
double I = cos(90);
double J = tan(90);
printf("sqrt func %.2lf\n",A);
printf("pow func %.2lf\n",B);
printf("round func %.2d\n",C);
printf("ceil func %.2d\n",D);
printf("floor func %.2d\n",E);
printf("fabs func %.2lf\n",F);
printf("log func %.2lf\n",G);
printf("sin func %.2lf\n",H);
printf("cos func %.2lf\n",I);
printf("tan func %.2lf\n",J);
printf("\nBASIC MATHEMATICAL FUNCTIONS");
}
FUNCTION IN C
#include <stdio.h>
//create a calc function with two parameter with both being of integer type
int calc(int x, int y){
int sum = x+y;
printf("The result of x and y is: %d",sum);
return sum;
// return x+y; works same way
}
int main(){
int x,y;
printf("Enter value for x: ");
scanf("\n%d", &x);
printf("Enter value for y: ");
scanf("%d", &y);
calc(x,y); //FUNCTION CALL. you call functions outside the main function in the main funct
ion
return 0;
//Lets take a look at Function Protoype which checks for misssing arguments
//and also makes the main method come before any other self declared method
}
FUNCTION PROTOTYPE IN C
#include <stdio.h>
//with Function prototype u dont have to input the variable name(s) in the parameter.
//Just the data type is enough
int main(){
char* name = "Flourish De B.sc";
int age = 10;
char gender[] = "Male";
details(name,age,gender);
return 0;
}
void details(char* name, int age,char gender[]){
printf("\nYour name is %s \n", name);
printf("Your age is %d \n", age);
printf("And Your gender is %s", gender);
}