Richard AR
Richard AR

Reputation: 59

C program not printing the string that is given as input

I want the output to print the data that we print. but it is not working as expected and the output is not displaying and it is exiting

#include <stdio.h>
    
int main() {
    char name[20], department[3], section[1];
    
    printf("enter the name of the student:");
    scanf("%s", name);
    
    printf("enter your department:");
    scanf("%s", department);
    
    printf("enter the section");
    scanf("%s", section);
    
    printf("Name:%s \n  Department:%s \n  Section: %s  ", name, department, section);
    
    return 0;
}

Upvotes: 0

Views: 929

Answers (5)

chqrlie
chqrlie

Reputation: 145317

Your program has undefined behavior because the arrays are too short and scanf() stores the user input beyond the end of the arrays, especially the last one, section that can only contain an empty string which scanf() cannot read anyway.

Make the arrays larger and tell scanf() the maximum number of characters to store before the null terminator, ie: the size of the array minus 1.

Here is a modified version:

#include <stdio.h>
    
int main() {
    char name[50], department[50], section[50];
    
    printf("enter the name of the student:");
    if (scanf("%49s", name) != 1)
        return 1;
    
    printf("enter your department:");
    if (scanf("%49s", department) != 1)
        return 1;
    
    printf("enter the section");
    if (scanf("%49s", section) != 1)
        return 1;
    
    printf("Name:%s\n  Department:%s\n  Section: %s\n", name, department, section);
    
    return 0;
}

Note that using scanf with a %s conversion requires that each data item be a single word without embedded spaces. If you want name, department and section to accommodate spaces, which is more realistic for anyone besides Superman Krypton A, you would use %[\n] with an initial space to skip pending whitespace and newlines (or fgets() but in another chapter):

#include <stdio.h>
    
int main() {
    char name[50], department[50], section[50];
    
    printf("enter the name of the student:");
    if (scanf(" %49[^\n]", name) != 1)
        return 1;
    
    printf("enter your department:");
    if (scanf(" %49[^\n]", department) != 1)
        return 1;
    
    printf("enter the section");
    if (scanf(" %49[^\n]", section) != 1)
        return 1;
    
    printf("Name:%s\n  Department:%s\n  Section: %s\n", name, department, section);
    
    return 0;
}

scanf(" %49[^\n]", name) means skip any initial whitespace, including pending newlines from previous input, read and store and bytes read different from newline, up to a maximum of 49 bytes, append a null byte and return 1 for success, 0 for conversion failure or EOF is end of file is reached without storing any byte. For this particular call, conversion failure can happen if there is an encoding error in the currently selected locale.

Upvotes: 5

Luis Colorado
Luis Colorado

Reputation: 12708

I will not insist in the reasons of the other answers (that state other problems in your code than the one you are asking for) but I'll limit my answer to the reasons you don't get any output before the first prompt (there's no undefined behaviour before the third call of printf if you have input short enough strings to not overflow the arrays --- the last is impossible as long as you input one char, because to input one char you heed at least space for two)

I want the output to print the data that we print. but it is not working as expected and the output is not displaying and it is exiting

stdio works in linebuffer mode when output is directed to a terminal, which means that output is written to the terminal in the following cases:

  • The buffer is filled completely. This is not going to happen with a sort set of strings.
  • There is a \n in the output string (which there isn't, as you want the cursor to remain in the same line for input as the prompt string)

As there is no \n in your prompts, you need to make printf flush the buffer at each call (just before calling the input routines) You have two ways of doing this.

  • Calling explicitly the function fflush(3), as in the example below:
    printf("enter the name of the student:");
    fflush(stdout);   /* <-- this forces flushing the buffer */
    if (scanf(" %49[^\n]", name) != 1)
        return 1;
  • configuring stdout so it doesn't use buffers at all, so every call to printf forces a write to the standard output.
    setbuf(stdout, NULL); /* this disables buffering completely on stdout */
    /*  ... later, when you need to print something */

    printf("enter the name of the student:"); /* data will be printed */
    if (scanf(" %49[^\n]", name) != 1)
        return 1;

But use this facilities only when it is necessary, as the throughput of the program is degraded if you disable the normal buffering of stdio.

Upvotes: 0

Itati
Itati

Reputation: 303

I watched for a long time and finally found the problem.

This is problem: char section[1];.

You declared the size is too short.

It looks like this after you declared it: section[0] = '\0';.

If you scanf a, the array data like section[0] = 'a';, and then it automatically add '\0' somewhere, so you got a memory leaking.

So replace char section[1]; to char section[2];.

Upvotes: 0

Fares Naoui
Fares Naoui

Reputation: 37

first of all you should respect the size of string ,so you should either convert section to char or increase the size of that string because you have here the problem of '\0' character...

so the rule is : the size of string is the size what you need + 1 for '\0' NULL character

and her is two program i tried to Modification you program for two scenarios :

#include <stdio.h>

int main(){

/// any size you like just respect the NULL character
char name[20],department[4],section[23];

  printf("enter the name of the student:");
   scanf("%s",name);

 printf("enter your department:");
  scanf("%s",department);

 printf("enter the section");
   scanf ("%s",section);

 printf("Name:%s \n  Department:%s \n  Section:%s  ", name,department,section);

  return 0;
} 
  

and case of char :

#include <stdio.h>

int main(){
char name[20],department[4];
char section;

  printf("enter the name of the student:");
   scanf("%s",name);

 printf("enter your department:");
  scanf("%s",department);

 printf("enter the section");
///don't forget this space before %c it is important
        scanf (" %c",&section);

 printf("Name:%s \n  Department:%s \n  Section:%c  ", name,department,section);

  return 0;
} 
  

Upvotes: 0

prnvbn
prnvbn

Reputation: 1027

The problem is that you have not accounted for the null character. It should work with the following.

char name[20] , department[4] , section[2];

The reason this happens is that C requires an extra character for the null character \0 which tells the program when the string ends.

Upvotes: 0

Related Questions