You are on page 1of 9

Data Types

Int is used to write whole numbers Example: 40


Int age = 69;

Double is used to write decimals Example: 1.2 , 40.0


Use: double gpa = 3.7;
Char is used for characters Ex: A B C D , John, George
Important note : char can only store one single character unless [] is
used after the command. Also instead of ‘’ , you need to use “”
Example: for single character, Char grade = ‘A’;
For string of characters, Char phrase [] = “John George”;

1
Prin f
Is used to write stuff on screen
To make different lines with same function use \n
Ex: prinf(“Hello\nWorld”);
To print number you need to use format specifier like %d
Ex: printf("My favorite number is %d", 69);
%s is Used for words
Ex: printf("My favorite %s is %d", "number", 69);
Pay attention to the commas
For decimals you need to use %f instead of %d
Ex: printf("My favorite %s is %f", "number", 69.69);
%c is for single character
This lesson is helpful when combined with the fist page’s lesson.
Also you can do basic math such as substraction, addition, division and
multiplication.
Important notes
While using %f you always need to use a decimal or else you won’t get
a answer. So when you need precise answer like 5/4, you need to write
it like printf("%f", 5/4.0); or else you won’t get a answer.
%d will never give you decimals. printf("%d", 5/4); will give you
the answer 1 not 1.25.

2
Functions
With C you can you a lot of mathematical functions such as power to
write 2³ use printf("%f", pow( 2,3) ); note: %d will not give you a answer
To write √25
printf("%f", sqrt( 25) );
{Special note if you’re stupid the numbers shown here as examples can be changed with any number,
just remember to decimals ≠full numbers}

to round numbers up use ceil


printf("%f", ceil(25.7)); will give you 26
ceil will give you the higher number while rounding up and to get
the lower number you need to use floor.
Ex: printf("%f", floor(25.7)); will give you 25
For more math functions just visit
https://www.geeksforgeeks.org/c-library-math-h-functions/

To write comments with codes you must


start with /* and end with */ anything in-between will not be
executed by the program
tip: instead of deleting programs, try commenting them out.

Constants

3
In the case be
int num = 5;
printf ("%d\n", num);
num = 8;
printf ("%d\n", num);
in this case you can change the value of NUM as much as you
want. To make it a constant use const int
you can’t change a constant
Best practice: use all capital for a const function
Constant can also refers to things that can’t be change unless
someone physically changes it. Ex: Hello world in hello world
another Ex: printf(%d", 69); here 69 is a constant

User input
To get user input you need to use printf and scanf commands.
Wich will let you get information from user and print it on the
screen. An example would be
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("you're %d years old", age);
return 0;
here & is pointer {pointers will be discussed in depth later on)

4
scanf does the opposite and scans info. %d is used here because
age is a whole number. This program would look a little different if
we needed a decimal. Then instead if int we’d have to use double.
And if you wanted a character, like a grade then you’d just use
char can use %c. that’s for a single character. But what if you
need a string of characters like someone’s name?
In this case you’d use the same char variable but this time use []
4th bracket. And inside the bracket specify the maximum amount
of characters you want.

Important notes
1. Use %s for character strings
2. You don’t need to use & when using string of characters.
3. Remember to use : semicolon
Important
When using scanf as you see in the picture it on only picks the

5
part before a space is used. It only took Monkey from Monkey D.
Luffy.
To have more than one word or name we need to use fgets
instead of scanf. Example

And now it will pickup the whole name.

Stdin stands for standard input. We’re using it to tell fgets where
we want the info from.
One downside is that it prints out a new line. Beware of this

6
Building a basic Calculator

To build a basic calculator you’d need only two commands


printf and scanf. Example

You need to be careful about commas and remember to write


variables correctly. Other than that it’s pretty easy when you compare
it to do with the knowledge from previous pages.
Now when using int we won’t be able to get decimals
So in-order to get decimals we’d need to use double, and instead of
%d we need %f . but when using scanf we need to use %lf or else
the program won’t be able to scan the number.

7
Mad Libs Game
Mad libs is a word game. In which one acts as the reader and other players
get to choose which punctuation mark should be used while the reader
reads the story. It can be easily programmed in c.

8
9

You might also like