Items in this page:
#include<stdio.h>
int main(void) { int i;float f; printf("Please enter a whole number: "); scanf("%d", &i); printf("Please enter a number "); printf("with a decimal point: "); scanf("%f", &f); printf("The sum of both entered number is = %f\n", i+f); return 0; } | |
#include<stdio.h>
int main(void) { char x[12]; printf("Enter your name with no spaces: "); scanf(" %s", x); printf("Your name is %s.\n", x); return 0; } |
|
#include<stdio.h>
int main(void) { char MyFname[20]; char MyLname[20]; printf("Enter your first and last names: \n"); scanf(" %s %s", MyFname, MyLname); printf("Your name is %s, %s\n", MyFname, MyLname); return 0; } | |
1(press space)2(press enter)3(press tab)4(press enter)
#include<stdio.h>
int main(void) { int i, j, k, l; printf("Enter four integers: "); scanf("%d%d", &i, &j); scanf("%d%d", &k, &l); printf("You entered %d and %d.\n", i, j); printf("You entered %d and %d.\n", k, l); return 0; }
|
|
#include<stdio.h>
int main(void) { char a, b; char c, d; printf("Enter four characters:"); scanf("%c%c", &a, &b); scanf("%c%c", &c, &d); printf("You entered %c and %c.\n", a, b); printf("You entered %c and %c.\n", c, d); return 0; }
|
|
#include<stdio.h>
int main(void) { char a, b; char c, d; printf("Enter four characters:"); scanf(" %c %c", &a, &b); scanf(" %c %c", &c, &d); printf("You entered %c and %c.\n", a, b); printf("You entered %c and %c.\n", c, d); return 0; }
|
|
#include<stdio.h>
int main(void) { char a, b; int i, j; printf("Enter two char-int pairs: "); scanf(" %c %d", &a, &i); scanf(" %c %d", &b, &j); printf("%c:%d:\n", a, i); printf("%c:%d:\n", b, j); return 0; }
|
|
#include<stdio.h>
int main(void) { int i; char t[12]; printf("Enter an integer and a string separated by space:"); scanf("%d %s", &i, &t); printf("i was %d and t was %s: \n", i, t); return 0; }
|
When the & removed from &i, the following Debug error displayed.
When the & removed from &t, the program run smoothly.
|
#include<stdio.h> #define PI 3.14 #define RADIUS "radius"
int main(void) { float flt; printf("Enter the radius: "); scanf("%f", &flt); printf("The %s is %.2f\n", RADIUS, flt); printf("The circumference is: %.2f\n", 2*flt*PI); printf("The area is: %.2f\n", PI*flt*flt); return 0; }
|
|
#include <stdio.h> #define SQUARE(x) x*x #define CUBE(x) x*x*x #define FOURTH(x) x*x*x*x #define FORMAT_I "i is equal to %d\n" #define PRINT_I printf(FORMAT_I, i); // also called a macro
int main() { int i = 5; printf(FORMAT_I, i); i = CUBE(i); printf(FORMAT_I, i); i = FOURTH(5); printf(FORMAT_I, i); return 0; }
For every occurrence of FORMAT_I, the string “i is equal to %d\n” is substituted and in place of CUBE(i),i*i*i is substituted, since i is used in place of x in the #define CUBE(x) x*x*x directive. Similar to FOURTH(5). The previous code can be expanded as shown below.
#include<stdio.h> #define SQUARE(x) x*x #define CUBE(x) x*x*x #define FOURTH(x) x*x*x*x #define FORMAT_I "i is equal to %d\n" #define PRINT_I printf(FORMAT_I, i); // also called a macro
int main() { int i = 5; printf("i is equal to %d\n", i); i = i*i*i; printf("i is equal to %d\n", i); i = 5*5*5*5; printf("i is equal to %d\n", i); return 0; }
|
--------------------------------------------------
We can see that both programs have similar output.
|
|
|
Input the part name: buttons Input price per part: 0.50 How many parts were sold? 5
5 buttons were sold for 1.50 each. The total sales = 2.625, including sale tax. | #include <stdio.h> #define TAX_RATE 0.05 int main() { // dummy initial values char part[20] = "test"; float price = 2.0; int quantity = 4; printf("Input the part name:"); scanf_s("%19s", &part, 20); printf("Input price per part: "); scanf_s("%f", &price, sizeof(float)); printf("How many parts were sold? "); scanf_s("%d", &quantity, 1); printf("\n\t%d %s were sold for %.2f each\n", quantity, part, price); printf("\tThe total sales = %.2f, including sale tax.\n", (quantity*price)+ (TAX_RATE*quantity*price)); return 0; }
|
| |
Input Bob Dylan’s rate and worked hours: 7.00 10 Bob Dylan worked 10 hours The rate of pay was USD7.00 per hour The gross pay was USD70.00 The net pay was USD63.00 Bob Dylan work for Tenouk Enterprise. | #include <stdio.h>
int main() { // dummy initial values char employee[20] = "Bob Dylan"; char company[20] = "Tenouk Enterprise"; float rate = 0.0; int hours = 0; printf("Input Bob Dylan's rate and worked hours: "); scanf_s("%f %d", &rate, &hours); printf("Bob Dylan worked %d hours\n", hours); printf("The rate of pay was USD%.2f per hour\n", rate); printf("The gross pay was USD%.2f\n", rate*hours); printf("The net pay was USD%.2f\n", (rate*hours) - (0.1*rate*hours)); printf("%s work for %s.\n", employee, company); return 0; }
|
// scanf_s() and wscanf_s() functions to read formatted input. #include<stdio.h>
int main( void ) { int i, result; float fp; char c, s[81]; wchar_t wc, ws[81]; printf("Input int, float, char, wide char, string and wide string\n"); result = scanf_s("%d %f %c %C %s %S", &i, &fp, &c, 1, &wc, 1, s, 80, ws, 80); printf("The number of fields input is %d\n", result); printf("The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws); printf("\nInput int, float, char, wide char, string and wide string\n"); result = wscanf_s(L"%d %f %hc %lc %S %ls", &i, &fp, &c, 2, &wc, 1, s, 80, ws, 80); wprintf(L"The number of fields input is %d\n", result); wprintf(L"The contents are: %d %f %C %c %hs %s\n", i, fp, c, wc, s, ws); return 0; }
// Try the following inputs: // 88 88.5 y x Byte characters // 77 71.7 t U Wide characters | |
During your C/C++ building process you can see all the compiler activities. Select Project → your_project_name Properties menu. | |
Expand the Linker folder under Configuration Properties folder. Select General link and on the right window, select Display All Progress Messages (/VERBOSE) of the Show Progress. Then clickApply button. Close theProject Property Pages. | |
![]() | |
Rebuild your program and see progress messages in the Output Window. | |
![]() |