Thread: How to initialize string one by one in legal way

  1. #1
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14

    How to initialize string one by one in legal way

    Below is the 100% correct codes:

    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
        char qs[][100]={
            {"\n1. The Best Tech Website is \n (1). My Website (2). Your Website"},
            {"\n2. Who are you? \n (1). I dont know (2). Wait! I really dont know"},
            {"\n3. Do you need something? \n (1). Yes (2). No"},
            {"\n4. Are you Mad? \n (1). May be (2). Yes for sure"},
            {"\n5. You need Doc buddy \n (1). Oh! Yea (2). OK! but Who cares"},
        };
        int ans[]={1,2,1,1,2};
        int choice;
        int score=0,a=0;
        for(a=0;a<5;a++)
            {
            puts(qs[a]);
            printf("\nEnter the Ans ");
            scanf("%d",&choice);
            if(ans[a]==choice)
            {
                puts("\nCorrect Ans");
                score=score+1;
            }else{
                puts("\n Wrong Answer");
            }
    
    
        }
        printf("\nyour score is=%d",score);
        getch();
    }
    Hi guys will somebody explain below initialization code:

    Code:
        char qs[][100]={
            {"\n1. The Best Tech Website is \n (1). My Website (2). Your Website"},
            {"\n2. Who are you? \n (1). I dont know (2). Wait! I really dont know"},
            {"\n3. Do you need something? \n (1). Yes (2). No"},
            {"\n4. Are you Mad? \n (1). May be (2). Yes for sure"},
            {"\n5. You need Doc buddy \n (1). Oh! Yea (2). OK! but Who cares"},
        };
    I have some specific questions regrading above code-
    1. Why we used [] and [100] ? cant we simply use one of them either [] or [100] ? because i know we can initialize string like

    Code:
    char qs [ ] = "textishere";
    
    2. Why we used set brackets so many times ?

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    Code:
    char qs [ ] = "textishere";
    This will initialize a single dimensional array, with the characters in your string plus a null character to end the string so the length will be 10.

    Code:
    char qs[][100]={
        {"\n1. The Best Tech Website is \n (1). My Website (2). Your Website"},
        {"\n2. Who are you? \n (1). I dont know (2). Wait! I really dont know"},
        {"\n3. Do you need something? \n (1). Yes (2). No"},
        {"\n4. Are you Mad? \n (1). May be (2). Yes for sure"},
        {"\n5. You need Doc buddy \n (1). Oh! Yea (2). OK! but Who cares"},
    };
    This initializes a two dimensional array which in this case will effectively be a 5 element array where every element is in itself an array of 100 chars which will be initialized with your strings.
    Last edited by Golf7; 09-10-2015 at 07:20 AM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You don't need so many braces. C ignores braces that aren't the initializer pair.

    Code:
    int example[] = { {{{4}}}, {{3}}, {6}, 12 } ;

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    Quote Originally Posted by Golf7 View Post
    the length will be 10.
    Make that 11, I miscounted the number of characters in your string which is actually 10 characters long itself. With the null character added to the end, the array length will be 11.

  5. #5
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14
    Thanks everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-26-2012, 02:40 PM
  2. initialize a string to nothing?
    By towed in forum C Programming
    Replies: 3
    Last Post: 12-16-2010, 03:15 AM
  3. Casting *string to *char legal?
    By Programmer_P in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2010, 12:53 AM
  4. Is this legal..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-02-2003, 11:28 PM