473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String is getting printed even after the size of array is exceeded

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[5];
cout<<"Enter your name";
cin>>name;
cout<<"Your name is"<<name;
getch();
}

In the above program the size of the array of the variable name is 5. which means the variable cant store more than 5 characters.
which also means
If I give the string "LINISH"
It should only print LINIS
But while the program is running, Even if I type a 10 characters string, It is getting printed Completely..why???

Kindly be kind enough to go through the below program also:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[5];
char game[5]
cout<<"Enter your name";
cin>>name;
cout<<"Enter your favorite game";
cin>>game;
cout<<name"Loves"<<game;
getch();
}

In this program when I input a string for the variable name,It is getting printed completely, irrespective how many characters are there in the string.
But If the string (which is input to the second variable that is game )holds more than 5 characters. the input of the first variable(name) is getting disturbed..why??/

look at the below cited output to be more clear about my doubts.
OUTPUT NO:1

Enter your name:LINISHFRANCIS (Note that the input holds more than five chars)
Enter your game:GOLF(input is less than five chars)

LINISHFRANCIS loves GOLF(Two inputs are getting printed comopletely)

OUTPUT NO:
Enter your name:LINISHFRANCIS (Note that the input holds more than five chars)
Enter your game:FOOTBALL(input is more than five chars)

ALL loves FOOTBALL [Note that "ALL" is the last three letters of FOOTBALL

I am using TurboC++ for windows 7

Thanks in advance
Mar 4 '15 #1
5 1475
weaknessforcats
9,208 Expert Mod 8TB
There are no array controls in C or C++. There are no string controls either. So, a string will print until a \0 is encountered regardless of how much memory is involved. Try it sometime. Omit the \0 and print the string and see what happens.

An array is just a piece of allocated memory. You can store a 100 byte string in an array of 5 bytes. Of course, the last 95 bytes is laying outside the array memory and has just trashed something else and when your program uses those bytes it crashes with a memory corruption error.

Your experience with the string and printf is called indeterminate results. Maybe it works and maybe it doesn't.

You are the one managing memory. Not the compiler.

You also might read: http://bytes.com/topic/c/insights/77...rrays-revealed
Mar 4 '15 #2
Thank you much.....the first half of my question is answereds clearly...i really wonder if you could answer the second half too..
Mar 4 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
It's like I said: Indeterminate behavior.

I compiled your code and ran it on Visual Studio 2013 and I get: LinishFrancislovesfootball followed by a runtime crash that says the memory in the area of "games" is corrupt.
Mar 4 '15 #4
Is it?... I am using tubo C++ version 3 and yours is visual studio.Each one is responding one type for the same coding. So it must be depending on the software used to run the program I guess.anyways thanks a lot.
Mar 5 '15 #5
weaknessforcats
9,208 Expert Mod 8TB
Each compiler is its own implementation of C. So it's not unexpected that different compilers generate different code.
Mar 5 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mohan | last post by:
Hi All, I am using web browser control in a VB 6.0 client server application to print reports. I am writing the report into a HTML file and displays the report to the user using the code,...
2
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
2
by: steve | last post by:
I am setting up a huge database in mysql, and I get the above error in Linux. I believe it is related to the size of one of my tables, which is 4,294,966,772 bytes in size. Can someone help. How...
8
by: Peter Ballard | last post by:
Hi all, I've got a C program which outputs all its data using a statement of the form: putchar(ch, outfile); This has worked fine for years until it had to output more than 2GB of data...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
9
by: eastcoastguyz | last post by:
I wrote a simple program to continue to create a very large file (on purpose), and even though there is plenty of disk space on that device the program aborted with the error message "File Size...
5
by: frankleggett | last post by:
Hi Please excuse my ignorance. I am trying to declare an array with a variable amount but I'm having no luck. this is what I've tried Dim Number as string X = Val(Number) dim Array(X) as...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
0
by: gdetre | last post by:
Dear all, I'm trying to get a large, machine-generated regular expression (many thousands of characters) to work in Python on a Mac (running Leopard), and I keep banging my head against this...
5
Siddarth777
by: Siddarth777 | last post by:
I have written a simple RPC program for a simple increment operation on an integer using RPCGEN. Code is getting compiled fine without any errors, when i call the client after starting the server,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.