Skip to main content

25 posts tagged with "C++"

View All Tags

Why size of an empty C++ class not zero?

· One min read

To ensure that the addresses of two different objects will be different C++ compiler will allocate one byte of memory. The below sample code explains clearly. Because of this behaviour the addresses of class X objects are not equal even though there is no data present in the class X.

class X {

};

class Y {

};

void main() {
X objX1, objX2; Y objY;

cout<<"size of objX1: "<<sizeof(objX1)<<endl; //Output: 1
cout<<"size of objY: "<<sizeof(objY)<<endl; //Output: 1

//Addresses
cout<<"Address of objX1: "<<&objX1<<endl;
cout<<"Address of objX2: "<<&objX2<<endl;

cout<<"Address of objY: "<<&objY<<endl;

if(&objX1 == &objX2) {
cout<<"Never Happens";
}
}

C FAQ 4 to 7: Simple Programs

· One min read

4. Swap of two numbers  with out temp​

#include "stdio.h";

int main()
{
int a,b;
a=10;
b=20;
printf("Before swapingn");
printf("%dt%dn",a,b);

/* normal temp
int temp;
temp=a;
a=b;
b=temp;

//sol:-1
a=a+b;
b=a-b;
a=a-b;

//sol:-2
a=ab;
b=a/b;
a=a/b;/

//sol:-3
//a^=b^=a^=b;

a= a^b;
b= a^b;
a= a^b;
printf("%dt",a);
printf("After swapingn");
printf("%dt%dn",a,b);
return 0;
}

Read More

Tags:

Write a code to Outputs its own code

· One min read

FILE macro is a Predefined Macros : The name of the current source file. FILE expands to a string surrounded by double quotation marks.

fgetc: Read a character from a stream. returns an integer putchar: Writes a character to a stream

# include "stdio.h"
//Program that outputs its own code

int main () {
FILE *fp; char c;
fp = fopen(__FILE__,"r");

while((c=getc(fp))!= EOF) {
printf("%c",c);
}
fclose(fp);
}
Tags:

Change the name of the Debug Version of a DLL or Exe

· One min read

Its very useful to name the Debug version of the exe or DLL with letter "D" appended in-order to avoid confusion between release and debug version of binaries

Steps:

  1. Create a console application with name "MyProject"

  2. To change the output file name in debug configuration

place D as shown in below figure

Project Project Properties -> Linker -> General -> Output File

Release Version: MyProject Application Name: c:MyProjectReleaseMyProject.exe Debug Version: MyProject Application Name: c:MyProjectDebugMyProjectD.exe

C FAQ

· One min read
  1. How to write a code to count number of characters/Spaces/numbers/special characters present in a sentence.  sentence ="1. GOD IS GREAT !!!"

2. How to insert space between each character in a sentence. sentence ="GOOD MORNING" newsentence = "G O O D  M O R N I N G"

Click here to read complete Article

Tags: