Items in this page:
|
// needed for printf()
#include<stdio.h>
// needed for strcpy_s() and their family
#include<string.h>
int main()
{
// declare variables and initialize some of them
// some compiler ask you to initialize all the variables...
// in this case just initialize them with dummy value..
// for example: int x = 0, float y = 0.00 etc.
char q[20], r[20] = "Tom Hanks", s[20] = "Kevin Kostner";
printf("Given: r[20] = \"Tom Hanks\", s[20] = \"Kevin Kostner\"\n");
printf("\nThe operations: strcpy_s(q, 20, s), strcpy_s(s, 20, r), \n"
"strcpy_s(r, 20, \"Sarah\")\n");
// the destination string must be large enough to hold the
// source string, including the terminating null character.
// if the non-secure version is used, such as strcpy()
// compiler may generate warning...
strcpy_s(q, 20, s);
strcpy_s(s, 20, r);
strcpy_s(r, 20, "Sarah");
printf("\nThe results: q = %s, r = %s and s = %s\n", q, r, s);
return 0;
}
A sample output:

Firstly, “Kevin Kostner” is assigned to q, “Tom Hanks” is assigned to s, and then “Sarah” is assigned to r.
Characters have single quotes, strings have double quotes, integers are whole numbers and float contains decimal points. The following statements just numbers but in C/C++ programming they have different meanings. Label the following data types appropriately.
3.1234 = ____________________ Ans: float
"3214.55" = ____________________ Ans: string
'5' = ____________________ Ans: char
1243 = ____________________ Ans: int
Show the output of the following program. See the changes of the variable values during the program execution that is why we call it a variable.
// needed for printf()
#include<stdio.h>
// needed for strcpy_s() and their family
#include<string.h>
int main()
{
// declare variables and initialize some of them
// some compiler ask you to initialize all the variables...
// in this case just initialize them with dummy value..
// for example: int x = 0, float y = 0.00 etc.
int i = 8;
printf("Initially, int i = %d\n", i);
i = i + 1;
printf("i = i + 1 = %d\n", i);
i = i + 2;
printf("i = i + 2 = %d\n", i);
i = i + 3;
printf("At the end of the operation i = i + 3 = %d\n", i);
return 0;
}
A sample output:

Run the following exercises, record the outputs, answer the questions and try to understand why the outcome is like that.
// needed for printf() #include<stdio.h>
int main() { int i = 77; printf("Initially, an integer i = %d\n", i); i = i + 1; printf("Now i = %d\n", i); return 0; }
|
|
// needed for printf() #include<stdio.h>
int main() { int i = 77; i = i + 3; printf("Initially, an integer i = %d\n", i); i = i + i + 1; printf("Now i = %d\n", i); return 0; }
|
|
|
| |
// needed for printf() #include<stdio.h>
int main() { int i = 77, j, k; j = i + 1; k = j + 1; i = k + j; printf("Finally, i = %d\n", i); printf("... and j = %d\n", j); printf("... and k = %d\n", k); return 0; }
|
|
// needed for printf() #include<stdio.h>
int main() { int i = 77; printf("Given i = 77...\n"); printf("i + 3 = %d\n", i + 3); printf("Then, i = %d\n", i); printf("Do it in different way...\n"); i = i + 3; printf("Again, i = %d\n", i); return 0; }
|
|
// needed for printf() #include<stdio.h>
int main() { int i = 12; printf("Initially i = %d\n", i); printf("Then, i * 3 - 4 = %d\n", i * 3 - 4); printf("Different answer? i * (3 - 4) = %d\n", i * (3 - 4)); printf("Another one, i / 2 + 4 * 2 = %d\n", i / 2 + 4 * 2); printf("Similar expression but different answer? i / (2 + 4) * 2 = %d\n", i / (2 + 4) * 2); printf("Finally, i = %d\n", i); return 0; }
* and /;+ and -;()
The priority execution of the expression in C/C++ depends on the operator precedence and is done automatically by compiler if the parentheses/brackets ( () ) are not used. In order to make your code readable or to produce a correct output, it is better to use parentheses/brackets that having the highest priority. So, forget about operator precedence just use brackets.
|
|
// needed for printf() #include<stdio.h>
int main() { printf("Printing an integer: %d\n", 10); printf("Printing a float: %.2f\n", 10); printf("Printing a float: %.2f\n", 10.00); printf("Printing an integer: %d\n", 10.00); return 0; }
|
|
// needed for printf() #include<stdio.h>
int main() { int i = 10; float x = 10.5; printf("Given int i = 10, float x = 10.5\n"); printf("Print as integer: i / 3 = %d\n", i / 3); printf("Print as float: i / 3 = %.2f\n", i / 3); printf("Print as integer: x / 3 = %d\n", x / 3); printf("Print as float: x / 3 = %.2f\n", x / 3); return 0; }
|
|
// needed for printf() #include<stdio.h>
int main() { int i; i = 10; printf("Given int i = 10\n"); printf("Print as int: i / 2 = %d\n", i / 2); printf("Print as int: i / 3 = %d\n", i / 3); printf("Print as float: i / 3 = %.2f\n", i / 3); printf("Print as float: i / 3.0 = %.2f\n", i / 3.0); printf("Print as float: (i + 0.0) / 3 = %.2f\n", (i + 0.0) / 3); printf("Print as int: i * 3 = %d\n", i * 3); printf("Print as float: i * 3.0 = %.2f\n", i * 3.0); return 0; }
|
|
// needed for printf() #include<stdio.h>
int main() { int i = 10; float x = 10.5; // a dummy initial value x = i / 3; // warning, int to float printf("Given int i = 10, float x = 10.5\n"); printf("Print as float: i / 3 = %.2f\n", x); x = i / 3.0; // warning, double to float printf("Print as float: i / 3.0 = %.2f\n", x); return 0; }
|
|
// needed for printf() #include<stdio.h>
int main() { int i = 10; float x = 10.5; i = x / 3; // warning printf("Given int i = 10, float x = 10.5\n"); printf("Print as float: i / 3 = %.2f\n", i); // rubbish printf("Print as int: i = %d\n", i); i = x / 3.0; // warning printf("Print as float: i = %.2f\n", i); // rubbish return 0; }
|
|