Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Confusing in c , allocating memory automitically

Status
Not open for further replies.

baby_1

Advanced Member level 1
Joined
Dec 3, 2010
Messages
415
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
4,277
hello
why this code works fine when i type name more than 10 character (for example 20 character ?)
i wrote it in Dev c++ compiler


Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>

char baby[10];


int main(void)
{    
     printf("Please Enter your Name:");
     gets(baby);
     printf("Hello Mr.%s",baby);
     gets(baby);  
     
}

if i program it in a PIC micro controller how is works?
 

Simply because the extra characters that are stored in baby[] after the allocated 10 are not overwriting anything important (at the moment).

Put more variables in and it might overwrite them.

There is nothing to stop your code storing more in the baby[] string than the allocated 10 - the 10 just tells the compiler how many storage units to allocate initially (and permanently - it does not increase that allocated amount if you try to use more in the code). If you do store more than was initially allocated, there is no control over what you might overwrite, or when.
 
  • Like
Reactions: baby_1

    baby_1

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top