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
  • The Structure of the Program
  • Writing the Name Display Program
  • Getting Started
  • Compiling and Running Your Program
  • Exploring Variations
  • Conclusion

Was this helpful?

  1. Starting to write code

Program: Writing a C Program to Display Your Name

In this section, we'll guide you through the process of writing a simple C program that displays your name. This exercise will help reinforce the basics of C programming and introduce you to the printf() function for output.

The Structure of the Program

Before diving into the code, let's recall the basic structure of a C program:

#include <stdio.h>

int main() {
    // Your code goes here
    return 0;
}

This structure includes the necessary header file (<stdio.h>) and the main function (main()), where the execution of your program begins.

Writing the Name Display Program

Now, let's create a program that prints your name to the console. Here's a simple example:

#include <stdio.h>

int main() {
    // Displaying your name
    printf("My name is [Your Name]\n");

    return 0;
}

Replace [Your Name] with your actual name. The printf() function is used to print text to the console, and represents a newline character, which adds a line break after displaying your name.

Getting Started

  1. Open a Text Editor:

    • Choose a text editor of your preference (e.g., Notepad, Visual Studio Code, Sublime Text) to write your C code.

  2. Create a New File:

    • Create a new file with a .c extension (e.g., display_name.c) to save your C code.

  3. Write Your Code:

    • Copy and paste the provided C code into your file.

  4. Save the File:

    • Save your file.

Compiling and Running Your Program

As mentioned in the previous sections, use the following steps to compile and run your program:

  1. Open a Terminal or Command Prompt:

    • Navigate to the directory containing your C file using the cd command.

  2. Compile the Code:

    • Use the following command to compile your code:

      gcc display_name.c -o display_name
  3. Execute the Program:

    • On Linux:

      ./display_name
    • On Windows:

      display_name.exe
  4. View the Output:

    • The console should display your name as per the printf() statement in your code.

Exploring Variations

Feel free to experiment with variations of the program. You can add additional information, change the formatting, or even prompt the user to enter their name using scanf() for interactive input.

#include <stdio.h>

int main() {
    char name[50];

    // Prompt user for name input
    printf("Enter your name: ");
    scanf("%s", name);

    // Displaying the entered name
    printf("Hello, %s!\n", name);

    return 0;
}

This variation prompts the user to enter their name, which is then displayed with a greeting.

Conclusion

Congratulations! You've successfully written a C program that displays your name. This simple exercise provides hands-on experience with basic input and output operations in C. As you progress in your C programming journey, you'll build on these fundamentals to create more complex and feature-rich applications.

In the upcoming sections, we'll explore additional C programming concepts to broaden your understanding and skills. If you have questions or want to explore specific topics further, feel free to ask or consult additional resources on C programming. Happy coding!

PreviousErrors and WarningsNextStructure of a C Program

Was this helpful?