Items in this page:
Study the following program source codes and the outputs.
Program Example:
|

// this program uses the scanf_s() and wscanf_s() functions
// secure versions, to read formatted input.
#include <stdio.h>
int main(void)
{
int i, result;
float fp;
char c, s[61];
wchar_t wc, ws[61];
printf("Input integer, float, char, wide char, string, wide string\nSeparated by space...\n");
result = scanf_s( "%d %f %c %C %s %S", &i, &fp, &c, 1, &wc, 1, s, 60, ws, 60 );
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 integer, float, char, wide char, wide string, string\nSeparated by space...\n");
result = wscanf_s( L"%d %f %hc %lc %S %ls", &i, &fp, &c, 2, &wc, 1, s, 60, ws, 60 );
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;
}
|
|
/* Assuming this is stdio.h file’s content The beginning of stdio.h Lines of codes The end of stdio.h file */ |
/* Assuming this is stdio.h file’s content The beginning of stdio.h Lines of codes The end of stdio.h file */
int main() { return 0; } |
#include <stdio.h> #define LIMIT 100
void main(void) { char MyLim[LIMIT] = "The beginning of the hard time"; char YourLim[LIMIT] = "Of learning C/C++ programming"; /* other codes */ } |
#include <stdio.h> #define LIMIT 100
void main(void) { char MyLim[100] = "The beginning of the hard time"; char YourLim[100] = "Of learning C/C++ programming"; /* other codes */ } |
#include <stdio.h> #define WIDTH 4.52 #define LENGTH 4.00
void main(void) { printf("The length of your side = %.2f\n", LENGTH); printf("The area is = %.2f\n", LENGTH * WIDTH); } |
#include <stdio.h> #define WIDTH 4.52 #define LENGTH 4.00
void main(void) { printf("The length of your side = %.2f\n", 4.00); printf("The area is = %.2f\n", 4.00 * 4.52); } |
|
|
|
|
The variable address = xxxxxxxx, and the value stored = 10.55
|
printf("The variable address = %p, and the value stored = %.2f", &MyVar, MyVar); |
--------------Output------------ Enter the value for MyVar: 10.25
|
printf("Enter the value for MyVar:"); scanf("%f", &MyVar); |
----------Output---------- What was the sales code? S. |
printf("What was the sales code?"); scanf("%c", &MyChar); |
|
printf("Input an integer and a character:"); scanf("%d %c", &MyInt, &MyChar);
scanf("%c%d", &MyChar, &MyInt);
or
printf("Input a character and an integer:"); scanf("%c %d", &MyChar, &MyInt);
scanf("%d%c", &MyInt, &MyChar);
or
printf("Input an integer and a character:"); scanf("%d%c", &MyInt, &MyChar);
scanf("%c%d", &MyChar, &MyInt);
or
printf("Input a character and an integer:"); scanf("%c %d", &MyChar, &MyInt); |
--------------Output--------------------- Input a character and an integer separated by a space: T 1234
Take note that it is even better to provide an accurate prompt for user. |
printf("Input a character and an integer separated by a space:"); scanf("%c%d", &MyChar, &MyInt); |
--------------------Output------------------ Enter your first and last names separated by a space: Jodie Foster Your name is Foster, Jodie |
printf("Enter your first and last names separated by a space:"); scanf("%s%s", FName, LName); printf("Your name is %s, %s\n", LName, FName); |
printf("Enter a float, then a character, then an integer "); scanf("%f %c %d", &x, &b, &j); printf("The answer of x * j = %.2f, a character entered is = %c.\n", x * j, b); |
#include <stdio.h>
void main(void) { float x = 0.0; char b = 'A'; int j = 0; printf("Enter a float, then a character, then an integer "); scanf("%f %c %d", &x, &b, &j); printf("The answer of x * j = %2.f, a character entered is = %c.\n", x * j, b); }
|
|
We would declare something like this: int MyInt; then,
|
#include <stdio.h>
int main(void) { int i = 10; printf("variable’s name is %c\n", ‘i’); printf("Variable value’s is %d\n", i); printf("It’s address is %p\n", &i); return 0; }
|
----------------------------------------------------------------------
|
#include <stdio.h>
int main(void) { int i = 100; printf("Variable value and its address: %d %p\n", i, &i); i = i + 1; printf("New variable value and its address: %d %p\n", i, &i); return 0; }
|
|
#include <stdio.h>
int main(void) { int i = 10, j = 20; j = j + 1; printf("The address of i is %p\n", &i); printf("The address of j is %p\n", &j); printf("The value of j is %d\n", j); return 0; }
|
|
#include <stdio.h>
int main(void) { int i; printf("Please enter a whole number: "); scanf("%d", &i); printf("You entered %d, and ", i); printf("the twice value is %d\n", 2*i ); return 0; }
|
|