if you want to preview the code that you've already made, you can use "printf" to view it
let's start the lesson.
#include <stdio.h>
void main()
{
printf("Hello World");
getchar();
}
If you start the program, it will output "Hello World"
•Why i use ";" in the end of all the row?.
○Yeah, we should write that after the finish a row.
•And what is "getchar()"?.
○Its for hold the screen that we can read the "Hello World".
○If we didn't use "getchar()", after we run the program, it immediately close the ...
○If you want to see it, just try to delete "getchar()" and you will see what will happen.
if you want to input the output from the keyboard, we use "scanf"
#include <stdio.h>
void main()
{
char name[10];
printf("Enter your name : ");
scanf("%s",name); //I use %s to store char data type
fflush(stdin);
printf("Your name is : %s",name);
getchar();
}
void main()
{
char name[10];
printf("Enter your name : ");
scanf("%s",name); //I use %s to store char data type
fflush(stdin);
printf("Your name is : %s",name);
getchar();
}
And run it, you will ask to input your name from "printf("enter your name");".
You input a word then you press enter, it will ouput what you wrote before.
•What is "char name[10];".
○ "char" is called data type, data type are "char, int, float".
○ "name[10]" it means, data type name has only contains 10 character.
○ We must use sign "[]" if we want to use char data type.
○ You can change "name" to another word.
•scanf("%s", &name) & printf("%s", name);
○ scanf("%s", &name) will save the objects in the memory and the memory will called "name" if you write "name".
○ You can change "name" to another word.
○ printf("%s",name") will show the object that you save in memory called "name"
•What is "%s".
○It is a format typing.
○It use for char data type.
○If you want to use int data type you use "%d" etc, you will know more later.
•fflush(stdin)
○ It remove the buffer in the buffer of I/O
•The green words mean a comments
○ It wont affect the code if you use "//" or "/* (your comment) */
○ "//" just for 1 row
○ "/* is for long comment, it will stop until you write "*/"
I'll give you some exercise,
Click here to check your skills.
Category: | 0 Comments