テ オ 's profile picture

Published by

published

Category: Web, HTML, Tech

C coding basics


I'm bored

C-code Basics

Just the basics of C

Any command ends with semi-colon => ;


Starting point of the program

int main() {   }
int main(void) {}

Ending the program

return 0; }

Libraries

#include <library>

stdlib.h = standard library

stdio.h = input/output library

math.h = for maths

array.h = you won't need here, but it's good to know that it exists



print word

printf("enter letters here\n");

\n = new line


user input (number)

float = ex. 23.455

float n;

scanf("%f", &n);

int = ex. 23

int n;

scanf("%i", &n); 

char = symbols, letters

char n;

scanf("%c", &n);

(only one letter input!)


char = %c
int = %i, %d
float = %f
|| = or
&& = and


Loops

do... while

do
{
code block
} while (condition)

while

while( condition) {
code block
}

for

for( start point; end point; counter) {
code block
}

example

do... while
do {
printf("do you want to repeat this block?\n");
scanf("%c" &n);
} while( n = 'y' || n = 'Y');

while
while(n<12){
printf("Please enter your number\n");
scanf("%i", &n);
}

for
(i++ = i+1)

for(int i=0; i<n; i++) {  
printf("my number is %i", i);
 }
 if

if( condition) {block}

else if(condition 2) {block 2}

else {block 3}

ex.

if(n < 0) {
printf("%f is <0", n);
}

else if( n >0) {
printf("%f is >0",n);
}

else {
printf("%f = 0",n); }


Program Example


#include <stdio.h>

#include <stdlib.h>


int main(void) {

int age;

float number, favnumb;

char letter; 


do {

printf("Please enter your age");

scanf("%i",&age);

while(age <0) {

printf("\nError age must be bigger or equal to 0\n please enter age");

scanf("%i",&age); }


printf("\nwhat is your favourite number?\n");

scanf("%f", &favnumb); 

printf("\n do you want to repeat the process?");

scanf("%c", &letter);

if(letter == 'y') {

printf("let's start again); }

else if(letter =='Y'){

printf("alright, let's start again); }

else{

do {

printf("please enter y or n");

scanf("%c"&letter);

} while(letter != 'y' || letter != 'Y')

} while (letter == 'y' || letter == 'Y')


printf("Goodbye");


return 0; }



 
Sorry if there are any typos or stuff






5 Kudos

Comments

Displaying 0 of 0 comments ( View all | Add Comment )