0% found this document useful (0 votes)
26 views7 pages

CSC 315 Made Easy by B.SC

This pdf spans across this topic 1. Preprocessors 2. Comments 3. Variables 4. If-else Statements 5. Function Study meaning of each term

Uploaded by

judeolaboboye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

CSC 315 Made Easy by B.SC

This pdf spans across this topic 1. Preprocessors 2. Comments 3. Variables 4. If-else Statements 5. Function Study meaning of each term

Uploaded by

judeolaboboye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

QUICK READ AND ASSIMILIATION ON CSC 315

COMPOSED BY PETER TOPE BLESSING (B.SC) – 07014312797

NB: NOT TO BE USED FOR EXAMINATION MALPRACTICE. CONSEQUENCES ARE ON YOU!!!

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

//1. i am a single line comment

/* 2. i am a
multi line
comment */

//Main method that returns an integer (0)


int main(){

//DATA TYPES SPECIFIER


/*
short "%d"
int "%d"
long long int "%lld"
float "%f"
double "%lf"
char "%c"
Array of char[] "%s"
bool "%d"
NB: BY DEFAULT ALL THESE DATATYOES ARE SIGNED
*/

//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;
*/

//using "%d" to output smallNum


short smallNum = 30000; //short is used for not large positve/negative number
printf("%d",smallNum);

//using "%d" to output largeNum


int largeNum = 300000000; //int is used for large positve/negative number
printf("\n%d",largeNum);

// \n is just to add new line to our code.

// using "%lld" to output largerNum


long long int largerNum = 30000000000; //long long int is used for larger
//postive/negative number
printf("\n%lld",largerNum);

//using "%f" to output floating number


const float PI = 3.142; //const is constant. PI value cannot be changed
printf("\n%f", PI);

//using "%lf" to output slightly large floating number


double circumference = 30.7896547;
printf("\n%lf",circumference);

//Generally both float and double usually results in 6 decimal places.


//use below format to get desired output

//using "%lf" to output slightly large floating number


double _2Decimalcircumference = 30.7896547;
printf("\n %.2lf",_2Decimalcircumference); //"mind the .2 before %lf"

float twoDecimalcircumference = 30.7896547;


printf("\n %.2f",twoDecimalcircumference); //"mind the .2 before %f"

//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 "%c" to output single character


char singleLetter = 'a'; //mind the single quote ''
printf("\n%c",singleLetter);

//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);

return 0; //return 0 terminates the main method

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");
}

//EQUIVALENT TERNARY OPERATOR to the above if-else statement


(strcmpi(name,name1) == 0)? printf("\nYes %s is the name",name):printf("\nName does not ma
tch");
//IF-ELSE WITH NUMBER
int age;
printf("\nEnter Your Age: ");
scanf("%d",&age);

if(age == 18){
printf("Age is %d",age);
}else{
printf("Age is not displayed because age is not 18");
}

//USING SWITCH. SWITCH WORKS SAME WAY AS IF ELSE STATEMENT


int value;
printf("\nFor value, Enter 1 or 2 ");
printf("\nENTER VALUE: ");
scanf("%d",&value);

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;

//AND (&&) OPERATOR. both must be true


if (age >= 0 && ageChecker){ //You can add more && as possible
printf("Both are true");
}else if(age != 0 && ageChecker == 0){
printf("\nOne or both is/are wrong. AND operator making sure i'm both True");
}else{
printf("\nNothing is happening here");
}

//OR (||) OPERATOR

char firstLetterOfMyName = 'F';


char firstLetterOfMyAliasName;
printf("\nEnter First Letter of my alias name: ");
scanf("%c",firstLetterOfMyAliasName);
firstLetterOfMyAliasName = toupper(firstLetterOfMyAliasName);
if (firstLetterOfMyName == 'F' || firstLetterOfMyAliasName == 'B') //One or both must be t
rue
{
printf("\nYeah that is right. OR operator got the best of me.");
}else{
printf("\nYou can't get this printed");
}

//NOT (!) OPERATOR

bool imGonnaTurnFalse = true;

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

// Function is also methods. dont get confused!


// self declared functions usually come before the main method

#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

void details(char[],int,char[]); //Function prototype-Checks for missing argument


//and gives error if argument parsed is not complete or overcomplete

int main(){
char* name = "Flourish De B.sc";
int age = 10;
char gender[] = "Male";

// Without function prototype details(name,age) with one missing parameter (gender);


// will compile but will display unwanted characters where gender is supposed to be at.

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);
}

I WISH YOU SUCCESS AMIGO!

You might also like