-3

What function/declaration can I use to store a value after exiting a program? I want to store my output to a variable and able to use the same value again after terminating the program once. Send help.

Chris
  • 1
  • 1
  • 1
    The program's memory is completely discarded when the process exits, you should probably store the data in a file. Look up how to do that (with `fopen` and `fprintf` for example). – Simon Doppler Feb 08 '20 at 13:12
  • Write to a file and read it at the begining to get the value from previous program exit. – TruthSeeker Feb 08 '20 at 13:13
  • @Chris, you may also want to have a look a [this thread](https://stackoverflow.com/questions/11573974/write-to-txt-file) – Simon Doppler Feb 08 '20 at 13:14
  • Is it possible to write values of a three different variables using printf? I already did the scanning of values. All I can’t do rn is the printing of new values in the .txt file. – Chris Feb 08 '20 at 13:32

1 Answers1

0

The primary facility C provides for storing data beyond program execution is files. This is part of the defined observable behavior of a program:

… At program termination, all data written into files shall be identical to the result that execution of the program according to the abstract semantics would have produced… This is the observable behavior of the program. (C 2018 5.1.2.3 6.)

Standard C provides no facility for maintaining the contents of data storage of the execution environment beyond the end of the program. For the most part, if you want to preserve the value of an object between program executions, you should write it to a file before the program ends and read it from a file after the program starts.

Specific systems may provide extensions that provide other ways of storing data persistently. Shared memory is a common feature, sometimes used to store data for periods longer than a single program execution but not through system reboots.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312