新的c ++有一些问题 [英] new at c++ have some questions

查看:79
本文介绍了新的c ++有一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试学习C ++并且有一些问题。我得到一个

分段错误,无论是什么,当我尝试这个简单的程序时。


基本上该程序假设打印五 ;。我一直在printf上阅读

,我想我发现了%s符号的错误。它说%s是

假设打印字符串但它不起作用!


谢谢你!

#包括< stdio.h>


#define is =

#define start main()


start

{

int x是5;


printf(" x的值是%s \ n", x);

}

解决方案

wintaki写道:





我正在尝试学习C ++并有一些问题。当我尝试这个简单的

程序时,我得到了一个

分段错误,无论如何。


基本上是程序假设打印五。我一直在读取printf上的b $ b,我认为我发现了%s符号的错误。

它说%s假设打印字符串但它不起作用!


谢谢你!


#include< stdio.h>


#define is =

#define start main()



如果你想编写C ++,首先要抛弃那种宏。他们

不会给语言添加任何清晰度,他们只会让你更难理解。


start



宏解析,main()不是C ++。 int main()是。


{

int x是5;


printf( x的值是%s \ n,x);

}



现在,当然你得到一个段错误。 %s应该打印一个

*字符串*。

你提供的是* int *。阅读printf的文档

(完全!)。

干杯,

-

IR


wintaki写道:





我想尝试学习C ++并提出一些问题。当我尝试这个简单的程序时,我得到了一个

分段错误,无论是什么。



这意味着你的程序试图访问不属于

的内存。


基本上该程序假设打印五。我一直在printf上阅读

,我想我发现了%s符号的错误。它说%s是

假设打印字符串但它不起作用!



这是你程序中的一个错误。 %s确实用于打印字符串。问题

是你不提供字符串,而是提供int。由于printf没有输入

安全,所以它没有注意到这个问题。相反,它只是尝试从实际存储int的位置读取字符串



#include< stdio.h>


#define is =

#define start main()



那些应该是什么除了混淆代码之外还好吗?


start

{

int x是5;


printf(" x的值是%s \ n",x);

}




Rolf Magnus写道:


wintaki写道:





我正在尝试学习C ++并有一些问题。当我尝试这个简单的程序时,我得到了一个

分段错误,无论是什么。



这意味着你的程序试图访问不属于
的内存。



这肯定不是我想做的。我不想访问

别人的记忆。我的目标是从5开始并打印

字符串版本。据我所知,字符串是一个文本值,并且

" 5"是一个int值。


>


基本上该程序打算打印五。我一直在printf上阅读

,我想我发现了%s符号的错误。它说%s是

假设打印字符串但它不起作用!



这是你程序中的一个错误。 %s确实用于打印字符串。问题

是你不提供字符串,而是提供int。由于printf没有输入

安全,所以它没有注意到这个问题。相反,它只是尝试从实际存储int的位置读取字符串



#include< stdio.h>


#define is =

#define start main()



那些应该是什么除了混淆代码之外还好吗?



ok我会停止那些,我只是测试它,因为我是新来的

#define并认为能够自定义是很好的C ++到我的

喜欢。但我明白你的观点。


然后新程序是

int main()

{

int x = 5;


printf(值为%s \ n,x);

}

但我仍有同样的问题。说的是我不能用%s使用%s

?但是,我如何获得文本值?我想

写一个简单的程序,取x值并用文字打印。

i认为%s将值转换为文本?


感谢


Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple program.

Basically the program is suppose to print "five". I have been reading
on printf and I think I found a bug with the %s symbol. it says %s is
suppose to print the string but it does not work!

Thanks be to you!
#include <stdio.h>

#define is =
#define start main()

start
{
int x is 5;

printf("the value of x is %s\n", x);
}

解决方案

wintaki wrote:

Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple
program.

Basically the program is suppose to print "five". I have been
reading on printf and I think I found a bug with the %s symbol.
it says %s is suppose to print the string but it does not work!

Thanks be to you!
#include <stdio.h>

#define is =
#define start main()

If you want to write C++, first ditch that kind of macros. They
don''t add any clarity to the language, they just make it harder to
understand.

start

Macros resolved, main() is not C++. int main() is.

{
int x is 5;

printf("the value of x is %s\n", x);
}

Now, of course you get a segfault. %s is supposed to print a
*string*.
What you provide it is an *int*. Read printf''s documentation
(completely!).
Cheers,
--
IR


wintaki wrote:

Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple program.

It means that your program attempts to access memory that doesn''t belong to
it.

Basically the program is suppose to print "five". I have been reading
on printf and I think I found a bug with the %s symbol. it says %s is
suppose to print the string but it does not work!

It''s a bug in your program. %s is indeed used to print strings. The problem
is that you don''t provide a string, but an int. Since printf isn''t type
safe, it won''t notice the problem. Instead, it just tries to read a string
from where actually an int is stored.

#include <stdio.h>

#define is =
#define start main()

What are those supposed to be good for, other than obfuscate the code?

start
{
int x is 5;

printf("the value of x is %s\n", x);
}



Rolf Magnus wrote:

wintaki wrote:

Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple program.


It means that your program attempts to access memory that doesn''t belong to
it.

this is certainly not what i want to do. i do not wish to access
someone else''s memory. my goal is to start with 5 and have it print
the string version. as I understand it a string is a text value, and a
"5" is an int value.

>

Basically the program is suppose to print "five". I have been reading
on printf and I think I found a bug with the %s symbol. it says %s is
suppose to print the string but it does not work!


It''s a bug in your program. %s is indeed used to print strings. The problem
is that you don''t provide a string, but an int. Since printf isn''t type
safe, it won''t notice the problem. Instead, it just tries to read a string
from where actually an int is stored.

#include <stdio.h>

#define is =
#define start main()


What are those supposed to be good for, other than obfuscate the code?

ok I will stop with those, I was just testing it out since I am new to
#define and thought it was nice to be able to "customize" C++ to my
liking. but I see your point.

But then the new program is
int main()
{
int x = 5;

printf("the value is %s\n", x);
}
but i still have the same problem. are saying is that I can not use %s
with an int value? but then how do I get a text value? I wanted to
write a simple program that took the x value and printed it in words.
i thought %s converted the value to text?

thanks


这篇关于新的c ++有一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆