Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 999439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:21:09+00:00 2026-05-16T07:21:09+00:00

I am beginner for programming.I referred books of C programming,but i am confused. 1.)

  • 0

I am beginner for programming.I referred books of C programming,but i am confused.

1.) What’s the difference betweent printf and gets?
I believe gets is simpler and doesn’t have any formats?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:21:10+00:00Added an answer on May 16, 2026 at 7:21 am

    printf

    The printf function writes a formatted string to the standard output. A formatted string is the result of replacing placeholders with their values. This sounds a little complicated but it will become very clear with an example:

       printf("Hello, my name is %s and I am %d years old.", "Andreas", 22);
    

    Here %s and %d are the placeholders, that are substituted with the first and second argument. You should read on the man page (linked above) the list of placeholders and their options, but the ones you’ll run into most often are %d (a number) and %s (a string).

    Making sure that the placeholder arguments match their type is extremely important. For example, the following code will result in undefined behavior (meaning that anything can happen: the program may crash, it may work, it may corrupt data, etc):

    printf("Hello, I'm %s years old.", 22);
    

    Unfortunately in C there is no way to avoid these relatively common mistakes.

    gets

    The gets function is used for a completely different purpose: it reads a string from the standard input.

    For example:

    char name[512];
    printf("What's your name? ");
    gets(name);
    

    This simple program will ask the user for a name and save what he or she types into name.

    However, gets() should NEVER be used. It will open your application and the system it runs on to security vulnerabilities.

    Quoting from the man page:

    Never use gets(). Because it is
    impossible to tell without knowing the
    data in advance how many characters
    gets() will read, and because gets()
    will continue to store characters past
    the end of the buffer, it is extremely
    dangerous to use. It has been used to
    break computer security. Use fgets()
    instead.

    Explained in a more simple way the problem is that if the variable you give gets (name in this case) is not big enough to hold what the user types a buffer overflow will occur, which is, gets will write past the end of the variable. This is undefined behavior and on some systems it will allow execution of arbitrary code by the attacker.

    Since the variable must have a finite, static size and you can’t set a limit of the amount of characters the user can type as the input, gets() is never secure and should never be used. It exists only for historical reasons.

    As the manual suggested, you should use fgets instead. It has the same purpose as gets but has a size argument that specifies the size of the variable:

    char *fgets(char *s, int size, FILE *stream);
    

    So, the program above would become:

    char name[512];
    printf("What's your name? ");
    fgets(name, sizeof(name) /* 512 */, stdin /* The standard input */);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very much still a beginner at programming but I have come across the
I'm a beginner at CUDA programming and am wondering if anyone knows of any
I am very beginner With Programming...(unfortunately) I want to remove Any added QueryString To
hi im a beginner in programming and network development. i have a question regarding
I am a complete, total beginner in programming, although I do have knowledge of
Beginner to assembly programming for x86. I have a simple asm file which I
This might be an overly simple question, but I am a beginner at programming
hi im a beginner to programming and have been stuck on this task for
hey i am a beginner in programming.so it may sound stupid. but i really
hello i'm beginner for programming I've got a homework. googled it but couldnt find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.