#include<stdio.h>
#include<conio.h>
union abc
{
int a;
int x;
float g;
};
struct pqr
{
int a;
int x;
float g;
} ;
void main()
{
union abc b;
struct pqr c;
clrscr();
b.a=10;
textbackground(2);
textcolor(6);
cprintf(" A = %d",b.a);
printf("\nUnion = %d",sizeof(b));
printf("\nStructure = %d",sizeof(c));
getch();
}
Run Code Online (Sandbox Code Playgroud)
我已将此程序保存为virus.cpp.我正在使用Turbo C编译器编译该程序并从Turbo C(Ctrl + F9)运行.
我使用的是Windows 7,我安装了Avira AntiVir病毒系统.
当我试图运行上面的程序时,它会创建一个蠕虫(DOS/Candy).我相信程序没有错.
现在这里有一些特别之处.执行相同的程序,但有以下区别.这里唯一的区别是\n:
#include<stdio.h>
#include<conio.h>
union abc
{
int a;
int x;
float g;
};
struct pqr
{
int a;
int x; …Run Code Online (Sandbox Code Playgroud) 我运行了一个C程序,并在不同的C编译器上获得了不同的输出.以下是我的计划
void main()
{
int i=5;
printf("%d%d%d%d%d",i++,i--,++i,--i,i);
}
Run Code Online (Sandbox Code Playgroud)
ON boarnland c ++ complier o/p is
45545
并在gcc上
45555
是真的依赖于编译器还是依赖于操作系统?
函数调用中的参数从左到右被压入堆栈.评估是从堆栈弹出.并且评估是从右到左,因此结果.
以下是程序的源代码,该程序在给出边时计算三角形的面积.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float s,area;
clrscr();
printf("Enter the lengths of the sides of the triangle:\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area=%f",area);
getch();
}
Run Code Online (Sandbox Code Playgroud)
我使用Turbo C++编译器3.0版来编译程序.当我将边数分别为10,10和10时,我得到的区域为43.301270,这是正确的.但是当我将值插入1,1和1时,程序将该区域设为0.000000,这显然是错误的.此外,当我插入值为3,3和3时,我得到的区域为2.000000,这是错误的.
有谁知道该程序的不稳定行为的原因?怎么纠正?我已将程序上传为Zip文件.
提前致谢.
现在,当我尝试编译时main.cpp,我收到错误,Undefined symbol add(int) in module main.cpp请帮助我!
//main.cpp
#include<iostream.h>
#include "addition.h"
int main()
{
add(4);
return (0);
}
Run Code Online (Sandbox Code Playgroud)
//add.cpp
#include "addition.h"
#include<iostream.h>
void add(int a)
{
cout<<a<<endl;
}
Run Code Online (Sandbox Code Playgroud)
//addition.h
void add(int a);
Run Code Online (Sandbox Code Playgroud) 当我尝试将一个对象与任何具有13或26或7值的元素存储在一起,然后尝试读取此文件时,它只是给出了垃圾值。其他值(例如1、64、78(随机值))则不会发生这种情况。
我分别使用了ofstream和ifstream来保持代码简单。我正在使用旧的编译器(Borland C ++),但是我的朋友们没有遇到此错误。
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
class xyz
{
public:
int x;
};
void main()
{
xyz a;
a.x = 45; // replace by 45 or 78 it works. Replace by 13, 26 it shows garbage values.
ofstream of;
of.open("file.dat", ios::out||ios::binary);
of.write((char*)&a, sizeof(a));
of.close();
xyz b;
ifstream sf;
sf.open("file.dat", ios::in||ios::binary);
sf.read((char*)&b, sizeof(b));
sf.close();
cout<<b.x;
getch();
}
Run Code Online (Sandbox Code Playgroud)
当前输出将是45,但是如果将其替换为13或26,它将返回垃圾值。
为什么namespace std用Turbo C ++ / Borland C ++编写的程序不需要名称空间std 时必须在新的编译器中使用?
这适用于旧的编译器
#include <iostream.h>
int main () {
cout << "Hello Programmers";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我们必须在新的编译器中编写以下给定的程序,而不是在上面的程序中编写,因为上述程序在新的编译器中不起作用。
#include <iostream>
using namespace std;
int main () {
cout << "Hello Programmers";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我是C的初学者,使用Turbo C++编译器(16位).
在我写的软件中,最大答案是大约32000.如果我想要一个大于那个的数字,我会使用long int.
如果我执行以下程序:
#include <stdio.h>
void main()
{
long int x;
x=40000;
printf("%d", x);
}
Run Code Online (Sandbox Code Playgroud)
然后我得到一个错误,即常量值在函数中很长main().
我怎样才能得到超过32000的答案并摆脱这个错误?另外我将%d更改为%ld并使用40000L bt当我使用无符号整数时,我还需要使用'l'和40000 // ??
我已将我的中缀转换为后缀表达式,即
3*(4+5)/10-1 into 345+*10/1-
Run Code Online (Sandbox Code Playgroud)
我们如何从postfix表达式中读取10,因为字符串长度函数只读取1而不是10,并且还有以下情况
3*(4+0)/10-1 into 340+*10/1- how we can differentiate that,
Run Code Online (Sandbox Code Playgroud)
是40是40或四个零,并用相同的情况下,10或一个零
我正在使用turbo c ++
Turbo C++:我想在不改变原始数据的情况下将字符串传递给函数(按值传递).如何在这段代码中实现???
#include<iostream.h>
#include<conio.h>
#include<string.h>
void pass(char g[20])
{
g[0]=g[2];
cout<<"\nPassed: ";
for(int i=0;i<strlen(g);i++)
cout<<g[i];
}
void main()
{
clrscr();
char a[20];
cout<<"\nEnter the Data: ";
cin.get(a,sizeof a);
int len=strlen(a);
pass(a);
cout<<"\nAfter pass: ";
for(int i=0;i<len;i++)
cout<<a[i];
getch();
}
Run Code Online (Sandbox Code Playgroud) 在C++中,如果我们通过控制台(cin)输入一个数字并从零开始一个数字,它将保持八进制数.但是一个变量可以保持两个以上的值吗?
例如,输入以下值:
03 => 3 (Octal)
012 => 10
但是当我们进入时
0180
It give answer in 2 lines when output the variable (cout):
1
80
and 01188 gives
9
88
现在,我知道八进制基数的值从0到7那么这里会发生什么?一个变量存储多于2个值,或者编译器只是吓坏了,因为我们在输入中放了8个?
我正在使用Akki编译器的turbo C 7.