Thread: printf doesn't print a string

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    printf doesn't print a string

    I can't print a string with printf in compiler, the code works fine in an online compiler. What can be wrong?

    Example of a code just to make you sure that the code is correct:

    Code:
    #include <stdio.h>
    
    int main() {
    	char string1[20];
    	char string2[] = "string literal";
    	printf("Enter a string: ");
    	scanf_s("%s", string1);
    	printf("string1 is %s,\nstring2 is %s,\nstring1 with spaces between characters is:\n",string1,string2);
    	for (int i=0;string1[i]!='\0';i++) {
    		printf("%c ", string1[i]);
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What compiler are you using?

    Does your code compile without errors or warnings?

    Here are the errors/warnings my compiler produces:
    main.c||In function ‘main’:|
    main.c|7|warning: implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]|
    main.c|9|error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode|
    main.c|9|note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code|
    By the way if you want to try to use scanf_s() I suggest you read the documentation for this function and use it correctly.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    Visual Studio 2012. And I "have" to use scanf_s because VS updated and won't accept scanf. What's wrong with it in this code, though?

    This is what I get:printf doesn't print a string-capture-jpg

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Then you should use scanf_s correctly.
    https://msdn.microsoft.com/en-us/lib....aspx#Anchor_3
    Other have classes, we are class

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    WoodSTokk thank you, but how did you understand that the problem is with scanf_s? From the screen shot or the code itself?

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    From the code in your first post.
    We have one time per month the problem between scanf() and scanf_s().
    jimblumberg has also point this out.
    Quote Originally Posted by jimblumberg View Post
    By the way if you want to try to use scanf_s() I suggest you read the documentation for this function and use it correctly.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf doesn't print on screen
    By Roffemuffe in forum C Programming
    Replies: 4
    Last Post: 05-30-2013, 04:21 AM
  2. Why doesn't this print?
    By Matt Holden in forum C Programming
    Replies: 1
    Last Post: 01-21-2013, 03:04 PM
  3. print % in printf
    By Saurabh Mehta in forum C Programming
    Replies: 3
    Last Post: 11-21-2012, 03:44 PM
  4. how to print .03 as 3% in printf?
    By shiyu in forum C Programming
    Replies: 2
    Last Post: 01-31-2003, 12:02 PM
  5. Doesn't print out?
    By cockroach007 in forum C Programming
    Replies: 8
    Last Post: 11-17-2001, 08:30 AM