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 9011625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:47:08+00:00 2026-06-16T02:47:08+00:00

#include <stdio.h> int main(){ char array[2]; array[0] = ‘q’; array[1] = ‘a’; printf(%s,array); return

  • 0
#include <stdio.h>

int main(){

char array[2];
array[0] = 'q';
array[1] = 'a';
printf("%s",array);

return 0;
}

if you ask me this code should not work. printf prints array[2] like string but it’s not a string. When i execute it, it works perfectly. Can you explain why?

  • 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-06-16T02:47:10+00:00Added an answer on June 16, 2026 at 2:47 am

    When i execute it, it works perfectly.

    You just got (un)lucky: your code exhibits undefined behavior, because it lets the printf‘s %s parameter run off the end of the sequence of characters that is not null-terminated.

    A string in C is a sequence of char, which must have an extra character with the value 0, called the null terminator. Here is a way to make your code work without undefined behavior:

    char array[3];
    array[0] = 'q';
    array[1] = 'a';
    array[2] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This part of my code works fine: #include <stdio.h> int main(){ //char somestring[3] =
This is my code: #include <stdio.h> #include <stdlib.h> int main() { char c, *data,
#include<stdio.h> #include<string.h> int main() { char a[]=aaa; char *b=bbb; strcpy(a,cc); printf(%s,a); strcpy(b,dd); printf(%s,b); return
#include<stdio.h> int main() { char a[5]=hello; puts(a); //prints hello } Why does the code
hi can anybody tell me the error in this? #include<stdio.h> int main() { char
consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
#include<stdio.h> int main(){ char a[6],*p; a[0]='a'; a[1]='b'; a[2]='c'; a[3]='4'; a[4]='e'; a[5]='p'; a[6]='f'; a[7]='e'; printf(%s\n,a);
Consider the following code: #include <stdio.h> #include <ctype.h> char* Mstrupr(char* szCad); int main() {
Consider following case: #include<stdio.h> int main() { char A[5]; scanf(%s,A); printf(%s,A); } My question
Here is my code #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char

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.