Module 5: Introduction TO C And C++

Google+ Pinterest LinkedIn Tumblr +

MODULE 5

————————————————-
#include
//for system()
#include


{
C++ codes…
}
————————————————-
should be changed to:
————————————————-
#include
//use C++ wrapper to call C functions from C++ programs…
#include
using namespace std;


{
C++ codes…
}
————————————————-
In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore.
It should be C++ header, so that it comply to the standard.
In older Borland C++ compiler this still works, but not proper any more…
and for standard C/C++ the portability should be no problem or better
you read Module23 at http://www.tenouk.com/Module23.html to get
the big picture…For C codes, they still C codes :o)
=========================================================================
=========================================================================

//Using the integer conversion specifiers
#include
#include

int main()
{
printf(“Various format for integer printing\n”);
printf(“————————————-\n”);
printf(“%d\n”, 455);
printf(“%i\n”, 455); //i same as d in printf()
printf(“%d\n”, +455);
printf(“%d\n”, -455);
printf(“%hd\n”, 32000);
printf(“%ld\n”, 2000000000L);
printf(“%o\n”, 455);
printf(“%u\n”, 455);
printf(“%u\n”, -455);
//-455 is read by %u and converted to the unsigned
//value 4294966841 by 4 bytes integer
printf(“%x\n”, 455);
printf(“%X\n”, 455);

system(“pause”);
return 0;
}

——————————————————————————-

//Printing floating-point numbers with
//floating-point conversion specifiers

#include
#include

int main()
{
printf(“Printing floating-point numbers with\n”);
printf(“floating-point conversion specifiers.\n”);
printf(“Compare the output with source code\n\n”);
printf(“1. %e\n”, 1234567.89);
printf(“2. %e\n”, +1234567.89);
printf(“3. %e\n”, -1234567.89);
printf(“4. %E\n”, 1234567.89);
printf(“5. %f\n”, 1234567.89);
printf(“6. %g\n”, 1234567.89);
printf(“7. %G\n”, 1234567.89);

system(“pause”);
return 0;
}

———————————————————————————

//Printing strings and characters
#include
#include

int main()
{
char character = ‘A’;
char string[] = “This is a string”;
char *stringPtr = “This is also a string”;

printf(“———————————\n”);
printf(“—Character and String format—\n”);
printf(“———————————\n\n”);
printf(“%c <–This one is character\n”, character);
printf(“\nLateral string\n”);
printf(“%s\n”, “This is a string”);
printf(“\nUsing array name, the pointer to the first array’s element\n”);
printf(“%s\n”, string);
printf(“\nUsing pointer, pointing to the first character of string\n”);
printf(“%s\n”, stringPtr);

system(“pause”);
return 0;
}

———————————————————————————-

//Using the p, n, and % conversion specifiers
#include
#include

int main()
{
int *ptr;
//pointer variable
int x = 12345, y;

ptr = &x;

//assigning address of variable x to variable ptr
printf(“\nUsing the p, n, and %% conversion specifiers.\n”);
printf(“Compare the output with the source code\n”);
printf(“———————————————–\n\n”);
printf(“The value of pointer ptr is %p\n”, ptr);
printf(“The address of variable x is %p\n\n”, &x);
printf(“Total characters printed on this line is:%n”, &y);
printf(” %d\n\n”, y);

y = printf(“This line has 28 characters\n”);

printf(“%d characters were printed\n\n”, y);
printf(“Printing a %% in a format control string\n”);

system(“pause”);
return 0;
}

———————————————————————————

//Printing integers right-justified
#include
#include

int main()
{
printf(” Printing integers right-justified.\n”);
printf(“Compare the output with the source code\n”);
printf(“—————————————\n\n”);
printf(“M\n”, 1);
printf(“M\n”, 12);
printf(“M\n”, 123);
printf(“M\n”, 1234);
printf(“M\n\n”, 12345);
printf(“M\n”, -1);
printf(“M\n”, -12);
printf(“M\n”, -123);
printf(“M\n”, -1234);
printf(“M\n”, -12345);

system(“pause”);
return 0;
}

———————————————————————————

//Using precision while printing integers,
//floating-point numbers and strings
#include
#include

int main()
{
int i = 873;
float f = 123.94536;
char s[] = “Happy Birthday”;
printf(“Using precision while printing integers,\n”);
printf(” floating-point numbers, and strings.\n”);
printf(“Compare the output with the source code\n”);
printf(“—————————————-\n\n”);
printf(“Using precision for integers\n”);
printf(“\t%.4d\n\t%.9d\n\n”, i, i);
printf(“Using precision for floating-point numbers\n”);
printf(“\t%.3f\n\t%.3e\n\t%.3g\n\n”, f, f, f);
printf(“Using precision for strings\n”);
printf(“\t%.11s\n”, s);

system(“pause”);
return 0;

}

———————————————————————————–

//Right justifying and left justifying values
#include
#include

int main()
{
printf(“Right justifying and left justifying values.\n”);
printf(” Compare the output with the source code.\n”);
printf(“——————————————–\n\n”);
printf(“sdcf\n\n”, “hello”, 7, ‘a’, 1.23);
printf(“%-10s%-10d%-10c%-10f\n”, “hello”, 7, ‘a’, 1.23);

system(“pause”);
return 0;
}

———————————————————————————–

//Printing numbers with and without the + flag
#include
#include

int main()
{
printf(“Printing numbers with and without the + flag.\n”);
printf(” Compare the output with the source code\n”);
printf(“———————————————\n\n”);
printf(“%d\n%d\n”, 786, -786);
printf(“%+d\n%+d\n”, 786, -786);

system(“pause”);
return 0;
}

———————————————————————————–

//Printing a space before signed values
//not preceded by + or –
#include
#include

int main()
{
printf(“Printing a space before signed values\n”);
printf(” not preceded by + or -n\n”);
printf(“————————————–\n\n”);
printf(“% d\n% d\n”, 877, -877);

system(“pause”);
return 0;
}

———————————————————————————–

//o, x, X, and any floating-point specifier
#include
#include

int main()
{
int c = 1427;
float p = 1427.0;

printf(“o, x, X, and any floating-point specifiers\n”);
printf(“Compare the output with the source code\n”);
printf(“—————————————–\n\n”);
printf(“%#o\n”, c);
printf(“%#x\n”, c);
printf(“%#X\n”, c);
printf(“\n%#g\n”, p);
printf(“%#G\n”, p);

system(“pause”);
return 0;
}

———————————————————————————–

//Printing with the 0 (zero) flag fills in leading zeros
#include
#include

int main()
{
printf(“Printing with the 0 (zero) flag fills in leading zeros\n”);
printf(” Compare the output with the source code\n”);
printf(“——————————————————-\n\n”);
printf(“%+09d\n”, 762);
printf(” d”, 762);
printf(“\n”);

system(“pause”);
return 0;
}

———————————————————————————–

//Reading integers
#include
#include

int main()
{
int a, b, c, d, e, f, g;

printf(“Reading integers from standard input\n”);
printf(“————————————\n\n”);
printf(“Enter seven integers separated by space: “);
scanf(“%d%i%i%i%o%u%x”, &a, &b, &c, &d, &e, &f, &g);
printf(“The input displayed as decimal integers is: \n”);
printf(“%d %d %d %d %d %d %d\n”, a, b, c, d, e, f, g);

system(“pause”);
return 0;
}

————————————————————————————

//Reading floating-point numbers
#include
#include

int main()
{
float a, b, c;

printf(” Reading floating-point numbers\n”);
printf(“Compare the output with the source code.\n”);
printf(“—————————————-\n\n”);
printf(“Enter three floating-point numbers, separated by space: \n”);
scanf(“%e%f%g”, &a, &b, &c);

printf(“Here are the numbers entered in plain\n”);
printf(“floating-point notation:\n”);
printf(“%f %f %f\n”, a, b, c);

system(“pause”);
return 0;
}

————————————————————————————

//Reading characters and strings
#include
#include

int main()
{
char x, y[20];

printf(“Enter a string: “);
scanf(“%c%s”, &x, y);
printf(“The input was: \n”);
printf(“the character \”%c\” “, x);
printf(“and the string \”%s\”\n”, y);

system(“pause”);
return 0;
}

————————————————————————————–

//input data with a field width
#include
#include

int main()
{
int x, y;

printf(“Enter a six digit integer: “);
scanf(“-%d”, &x, &y);
printf(“The integers input were %d and %d\n”, x, y);

system(“pause”);
return 0;
}

————————————————————————————–

//Reading and discarding characters from the input stream
#include
#include

int main()
{
int month1, day1, year1, month2, day2, year2;

printf(“Enter a date in the form mm-dd-yy: “);
//pad 0 for two fields and discarding the – characters….
scanf(“%d%*c%d%*c%d”, &month1, &day1, &year1);
printf(“month = d day = d year = d\n\n”, month1, day1, year1);
printf(“Enter a date in the form mm/dd/yy: “);
//pad 0 for two fields and discarding the / characters…
scanf(“%d%*c%d%*c%d”, &month2, &day2, &year2);
printf(“month = d day = d year = d\n”, month2, day2, year2);

system(“pause”);
return 0;
}

—————————————————————————————

//concatenating the << operator
#include
#include

int main()
{

cout<<“47 plus 54 is “<<(47 + 54)< cout<<“Welcome to C++ stream\n”;
system(“pause”);
return 0;
}

—————————————————————————————

//printing the address stored in a char * variable
#include
#include

int main()
{
char * string = “pointer testing”;

cout<<“\nThe string is: “< <<“\nValue of (void *) string, the address is: “
<<(void *)string <
system(“pause”);
return 0;
}

—————————————————————————————-

//stream extraction operator
//input from keyboard with cin
#include
#include

int main()
{
int x, y;

cout<<“Enter two integers: “;
cin>>x>>y;
cout<<“Sum of “< cin>>s>>t;

q = 10*(s + t);

cout<<“simple mathematics calculation, just for demo”<<‘\n’;
//using ‘\n’ for newline
cout<<“q = 10(s + t) = “< //using endl for new line
cout<<“That all folks!!”<<“\n”;
cout<<“Study the source code and the output\n”;

system(“pause”);
}

———————————————————————————————

//cout and cin example for function call and array
#include
#include

float simple_calc(float);

void main(void)
{
float x = 3, y[4], sum=0;
int i;

cout<<“Square of 3 is: “< //cout with function call

cout<<“Study the source code and the output”<
for (i=1; i<5; i++)
{
cout<<“Enter arrays’ data #”< cin>>y[i];
sum = sum + y[i];
//array
}
cout<<“Sum of the arrays’ data is: “< cout<<“Press Enter key to quit\n”;

system(“pause”);
}

float simple_calc(float x)
{
float p;
p = (x * x);
return p;
}

——————————————–VC++/VC++ .Net—————————————————

//Printing floating-point numbers with
//floating-point conversion specifiers

#include

main()
{
printf(“Printing floating-point numbers with\n”);
printf(“floating-point conversion specifiers.\n”);
printf(“Compare the output with source code\n\n”);
printf(“1. %e\n”, 1234567.89);
printf(“2. %e\n”, +1234567.89);
printf(“3. %e\n”, -1234567.89);
printf(“4. %E\n”, 1234567.89);
printf(“5. %f\n”, 1234567.89);
printf(“6. %g\n”, 1234567.89);
printf(“7. %G\n”, 1234567.89);
}

————————————————–GCC—————————————————–

/*Using the p, n, and % conversion specifiers*/
/*****************module5.c*******************/
#include
#include

int main()
{
int *ptr;
/*pointer variable*/
int x = 12345, y;

ptr = &x;

/*assigning address of variable x to variable ptr*/
printf(“\nUsing the p, n, and %% conversion specifiers.\n”);
printf(“Compare the output with the source code\n”);
printf(“———————————————–\n\n”);
printf(“The value of pointer ptr is %p\n”, ptr);
printf(“The address of variable x is %p\n\n”, &x);
printf(“Total characters printed on this line is:%n”, &y);
printf(” %d\n\n”, y);

y = printf(“This line has 28 characters\n”);

printf(“%d characters were printed\n\n”, y);
printf(“Printing a %% in a format control string\n”);
return 0;
}

=============================================================================================================.h>.h><<;
(x)<<‘\n’;
.h>.h><<<“>.h>.h>;

.h>.h>;
.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>

  .h>.h><

<;
(x)<<‘\n’;
.h>.h><<<“>.h>.h>;

.h>.h>;
.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>

Share.

About Author

Leave A Reply