Thread: How to declare and initialize string variables in C?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    4

    How to declare and initialize string variables in C?

    I remember briefly touching C in school about 4 years ago, and hearing that C cannot print strings directly, but rather only as an array of characters. Is that true? How do I get the following code to work then:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //SamsVariables.c
    
    int main(){
        
        int windSpeed = 301;
        printf("The fastest tornado wind speed ever recorded was %d mph.\n",windSpeed); 
        
        int windSpeed2 = 215;
        str hurricaneName = "Hurricane Patricia";
        
        printf("The hurricane with the highest ever wind speeds was %s, with wind speeds of %d mph.\n",hurricaneName,windSpeed2);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by UncleBazerko View Post
    Code:
        str hurricaneName = "Hurricane Patricia";
    Code:
    char hurricaneName[] = "Hurricane Patricia";
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by UncleBazerko View Post
    I remember briefly touching C in school about 4 years ago, and hearing that C cannot print strings directly, but rather only as an array of characters. Is that true? How do I get the following code to work then:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //SamsVariables.c
    
    int main(){
        
        int windSpeed = 301;
        printf("The fastest tornado wind speed ever recorded was %d mph.\n",windSpeed); 
        
        int windSpeed2 = 215;
        str hurricaneName = "Hurricane Patricia";
        
        printf("The hurricane with the highest ever wind speeds was %s, with wind speeds of %d mph.\n",hurricaneName,windSpeed2);
    }
    A C "String" is a "Null terminated array of type char"!

    Code:
    char *hurricaneName = "Hurricane Patricia";
    This creates a pointer to a null terminated constant string. The constant string hurricaneName points to cannot be changed.

    Code:
    char hurricaneName[] = "Hurricane Patricia";
    This creates an array large enough to hold a mutable copy of the constant string plus the null byte, and is initialized to a copy of the constant string.

    Code:
    char hurricaneName[100] = "Hurricane Patricia";
    This creates an array 100 bytes long to hold a mutable copy of the constant string plus the null byte, and is initialized to a copy of the constant string. The remainder of the bytes are set to null bytes.

    I hope this clears up your confusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to declare these variables
    By jim_0 in forum C Programming
    Replies: 11
    Last Post: 02-14-2014, 02:32 AM
  2. Declare many Variables
    By Engineer1 in forum C Programming
    Replies: 16
    Last Post: 08-21-2013, 01:22 PM
  3. cannot initialize structure variables
    By baxy77bax in forum C Programming
    Replies: 8
    Last Post: 09-13-2012, 07:44 AM
  4. declare and initialize struct members
    By Mr.Bit in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 11:57 AM
  5. Initialize const variables in constructors
    By mvgian in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2006, 09:40 AM

Tags for this Thread