-1
  • The source code file (test.c) copied below has my c code and console output commented out.
  • I am trying to figure out why "Hello" is not printing to console output.
  • I believe it may have something to do with scanf([^\n]) reading in a '\n' from previous line (see lines 14 & 15).
#include <stdio.h>
#include <string.h>
    
#define MAX_LEN 16
    
int main(){
    char ch;
    char s[MAX_LEN]; 
    char sen[MAX_LEN];
    scanf("%c", &ch);
    scanf("%s", s);
  
    scanf("\n");
    scanf(" %[^\n]", sen);
    scanf("%*c");

    printf("%c\n", ch);
    printf("%s\n", s);
    printf("%s\n", sen);
    printf("sen[15] = %c\n", sen[15]);
    printf("string length = %lu\n", strlen(sen)); 

    return 0;
}

Output

user@MacBook-18 c_the_hard_way % ./test
C
Hello
My name is Mikey
C
    
My name is Mikey
sen[15] = y
string length = 16
  • I already looked at this link https://stackoverflow.com/questions/30065675/what-does-scanf-nc-mean . It helped describe what scanf(%[^n]) and scanf(%*c) do, although I still couldn't get "Hello" to print on line 43. – MichaelJordan Jul 19 '20 at 14:55
  • 1
    please do not attach code as image, include it inside a formatted block in your question – L.Grozinger Jul 19 '20 at 14:55
  • Showing code directly, not as link and not as picture, is what we expect of a [mre]. – Yunnosch Jul 19 '20 at 15:37
  • Please describe what you expect `scanf("\n")` to achieve. – Yunnosch Jul 19 '20 at 15:38
  • I apologize, I am new to the platform. – MichaelJordan Jul 19 '20 at 15:48
  • scanf("\n") takes in leading whitespace characters from stdin. Although, I believe the newline is needed to allow for next scanf(^\n) call to start reading input from stdin until the user enters a newline. – MichaelJordan Jul 19 '20 at 15:53
  • When I added scanf("\n") then I was able to get the char array sen[MAX_LEN] to print. Before adding scanf("\n"), I wan unable to get that array to print to stdout. – MichaelJordan Jul 19 '20 at 16:02

2 Answers2

0

The problem is here:

#define MAX_LEN 16

The string length given:

My name is Micky

Has the length of 16 without a null-terminator. Change the MAX_LEN to 17.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
0

If we make a couple of small changes, we can see why Hello is not being printed:

#include <stdio.h>
#include <string.h>
    
#define MAX_LEN 16
    
int main(){
    char ch;
    char s[MAX_LEN]; 
    char sen[MAX_LEN];
    scanf("%c", &ch);
    scanf("%s", s);
  
    scanf("\n");
    scanf(" %[^\n]", sen);
    scanf("%*c");

    printf("%c\n", ch);
    printf("%s\n", s);
    printf("%s\n", sen);
    // The null terminator from the second string is getting written
    // to the start of the storage for "Hello"
    printf("s[0] = %d\n", (int) s[0]);
    // Let's try to print "Hello" now that we know what is happening
    printf("%s\n", &s[1]);
    printf("sen[15] = %c\n", sen[15]);
    printf("string length = %lu\n", strlen(sen)); 

    return 0;
}

Output

C
Hello
My name is Mikey
C

My name is Mikey
s[0] = 0
ello
sen[15] = y
string length = 16

Here, we see that the start of Hello is being overwritten by the null terminator from My name is Mickey.

Run this code online

Note

The language lawyers will be coming out of the woodwork to say that what you are experiencing is "undefined behavior" and that the output above is not guaranteed to be the same. However, in this case, the compiler implementations are similar enough that we get the same output.

  • Thank you. This makes a lot of sense. I appreciate your response and explanation. I am curious why you needed to cast s[0] to an integer? Why not just leave it as string or char? I feel this is basic question, although I am new to this, so please have mercy. – MichaelJordan Jul 19 '20 at 16:37
  • @MichaelJordan I cast it to an integer, because I'm not 100% certain what it is, and I want to know its numerical value. If it is not an ASCII character, and I try to print it as a character, I might get an unprintable character, which would result in no output on my terminal. –  Jul 19 '20 at 21:02