Professional Documents
Culture Documents
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}
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
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
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