The following code is a program that does nothing.
#include <stdio.h>
int main(){
// write your codes
return 0;
}
#include <stdio.h>.int main(){...}, we write some codes.// denotes a comment, not a program.return 0; in the main function means successful end of the program.#include <stdio.h>
int main(){
printf("sample text 1 \n");
printf("sample text 2 \n");
return 0;
}
printf function, text is shown in our display." is required for text in printf function, e.g., "sample text".\n is for line break.; is required except for curly brackets {...} and <...>.; results in a error.\n results in not occurring a line break.10, -1, 0, 1, 5, 10 ... are tntegers.#include <stdio.h>
int main(){
int a = 1;
int b = -2;
printf("a is %d \n", a);
printf("b is %d \n", b);
printf("a and b is %d %d", a, b);
return 0;
}
#include <stdio.h>
int main(){
int a = 1, b = -2;
printf("a is %d \n", a);
printf("b is %d \n", b);
printf("a and b is %d %d", a, b);
return 0;
}
int a = 1;, the integer variable a is prepared and substituting 1 into a.int a = 1, b = -2; for preparing two integer variables.printf function, write %d to displaying location and set the integer variable on the end of printf function, e.g., printf("%d", a);.printf function, write printf("%d %d", a, b);. The first %d correspond to a and the second %d correspond to b.int, the minimum integer value is -2147483648 and the maximum integer value is 2147483647. Substituting the out of this range into int variable results in unstable.#include <stdio.h>
int main(){
int abc = 1;
int a_bc = 2;
int a_0 = 3;
printf("%d %d %d", abc, a_bc, a_0);
return 0;
}
a-z and A-Z can be allowed as variable name._ can be allowed.+, -, *, and # as variable's name.xyz8 is allowed, but 8xyz is not allowed.int 8xyz = 5; into your code, check for occurring a error. We cannot set numbers to the first character of variable name.int a#a = 3; into your code, check for occurring a error. We cannot use symbols excpet the underscore.The following is code for basic arithmetic operations.
#include <stdio.h>
int main(){
int a = 5;
int b = -3;
int c0, c1, c2, c3;
c0 = a + b;
c1 = a - b;
c2 = a * b;
c3 = a / b;
printf("a + b = %d \n", c0);
printf("a - b = %d \n", c1);
printf("a * b = %d \n", c2);
printf("a / b = %d \n", c3);
return 0;
}
+, -, *, / represent addition, subtraction, multiplication, and division, respectively./ only considers the integer part, even if it is a real number.a/b = 5/(-3) = -1.666..., but c3 is -1.Like mathematics, the program calculates the inside of brackets as priority.
#include <stdio.h>
int main(){
int a = 5;
int b = -3;
int c = 2;
int d;
//comment: 16 - 4 = 12
d = (a - b) * (a + b) - c * (a + b);
printf("(a - b) * (a + b) - c * (a + b) = %d \n", d);
return 0;
}
The following is code for using real numbers like 1.2, 0.4 and -2.9.
#include <stdio.h>
int main(){
double a = 5.2;
double b = -3.7;
double c0, c1, c2, c3;
c0 = a + b;
c1 = a - b;
c2 = a * b;
c3 = a / b;
printf("a + b = %lf \n", c0);
printf("a - b = %lf \n", c1);
printf("a * b = %lf \n", c2);
printf("a / b = %lf \n", c3);
return 0;
}
double instead of int.%lf in printf function./. See printf("a / b = %lf \n", c3);pi = 3.14159265359....#include <stdio.h>
int main(){
double pi = 3.14159265359;
printf("pi = %lf \n", pi);
printf("pi = %.4lf \n", pi);
printf("pi = %.2lf \n", pi);
printf("pi = %.0lf \n", pi);
return 0;
}
%lf: 6 digits (default) = 3.141593%.4lf: 4 digits = 3.1415%.2lf: 2 digits = 3.14%.0lf: 0 digits = 3Like mathematics, the program calculates the inside of brackets as priority.
#include <stdio.h>
int main(){
double a = 5.0;
double b = -3.0;
double c = 32.0;
double d;
//comment: 16/32 + 5 = 5.5
d = (a - b) * (a + b) / c + a;
printf("(a - b) * (a + b) / c + a = %lf", d);
return 0;
}
/./.#include <stdio.h>
int main(){
char text[100] = "I have a pen.";
printf("Text: %s", text);
return 0;
}
text[100], the variable text can be prepared, and it can be allowed maximum 100 characters.printf function, using %s.#include <stdio.h>
int main(){
printf("Input string: ");
char text[100];
scanf("%s", text);
printf("Your input string: %s \n", text);
return 0;
}
scanf function is for input value from keyboard.scanf("%s", text);, we can input value to variable text.text is characters, we use %s in scanf function.The following is code for input from keyboard and substituting it into int-type variable.
#include <stdio.h>
int main(){
printf("Input: ");
int a_val;
scanf("%d", &a_val);
printf("Your input: %d \n", a_val);
return 0;
}
scanf("%d", &a_val); we can input value from keyboard to int-type variablea_val.& is required.a_val is integer, we use %d in scanf function.The following is code for input from keyboard and substituting it into double-type variable.
#include <stdio.h>
int main(){
printf("Input: ");
double a_val;
scanf("%lf", &a_val);
printf("Your input: %lf \n", a_val);
return 0;
}
scanf("%lf", &a_val); we can input value from keyboard to double-type variablea_val.& is required.a_val is double, we use %lf in scanf function.| Variable type | Initialization | Show on display | Input from keyboard |
|---|---|---|---|
| Integer | int a | print(%d, a) | scanf("%d", &a) |
| Double (real number) | double a | print(%lf, a) | scanf("%lf", &a) |
| Characters | char a[100] | print(%s, a) | scanf("%s", a) |
Create following programs and submit them.
Submission: Please submit following materials.