C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page

Was this helpful?

  1. Dynamic Memory Allocation

Program: User Input String

In this C programming challenge, you will create a program that allows the user to input a text string. The program will utilize dynamic memory allocation to handle variable-sized input strings. Follow the requirements below:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int limit;

    // Get the limit of the string from the user
    printf("Enter the limit for the string: ");
    scanf("%d", &limit);

    // Allocate memory for the string dynamically
    char *inputString = (char *)malloc((limit + 1) * sizeof(char)); 
    // Adding 1 for the null terminator

    // Check if memory allocation was successful
    if (inputString == NULL) {
        printf("Memory allocation failed. Exiting program.\n");
        return 1; // Exit with an error code
    }

    // Prompt the user to enter the string
    printf("Enter the string: ");
    scanf(" %[^\n]", inputString);

    // Print the entered string
    printf("You entered: %s\n", inputString);

    // Release the dynamically allocated memory
    free(inputString);

    return 0; // Exit successfully
}

Explanation

  1. The program prompts the user to enter the limit for the string and reads the input using scanf.

  2. Dynamic memory is allocated for the string using malloc, taking into account the specified limit. The + 1 is for the null terminator '\0'.

  3. A check is performed to ensure that the memory allocation was successful. If not, the program exits with an error message.

  4. The user is prompted to enter the string, and scanf with the format specifier %[^\n] is used to capture the entire line of text.

  5. The entered string is then printed.

  6. Finally, the dynamically allocated memory is released using free.

This program demonstrates the use of dynamic memory allocation for handling user input strings with a flexible size limit.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

Previousmalloc, calloc, and reallocNextMemory Deallocation

Was this helpful?