انتقل إلى المحتوى

مستخدم:Abdullah yousif02

من ويكيبيديا، الموسوعة الحرة

شرح بعض من لغه البرمجه سي (c) string

A string is a series of characters treated as a single unit

A string is an array of characters.

A string may include letters, digits and various special characters such as +, -, *,/,&,$ A string is stored in memory as ASCII code of characters that make up the string appended with ‘\0’ (null character). Capital A is represented by 65 and small a is 97 Capital T is represented by 84 and small t is 116

String literals or string constants: Set of characters written in double quotation marks. Examples: "Ali Mohammed" name "9999 Main Street" street address "(03)5850000" telephone number


char color[5] = "blue"; This will create a 5 element array color containing the characters ‘b’, ‘l’, ‘u’, ‘e’ and ‘\0’ In this case, C puts the ‘\0’ at the end of the string


. Initializer list char color[5] = { ‘b’ , ‘l’ , ‘u’ , ‘e’, ‘\0’ }; In this case, you have to end the string with ‘\0’

3. Character by character char color[5];

     color[0]=‘b’; 
     color[1]=‘l’;
     color[2]=‘u’;
     color[3]=‘e’;
     color[4]=‘\0’; 

In this case, again you have to end the string with ‘\0


By using scanf statement char color[5];

    scanf("%s", color);

color is an array which is a pointer, so the address indicator (&) is not needed with argument color. scanf will read characters until a space, newline or end of file indicator. /*gets(color); reads even the spaces */ In this example, the string should be no longer than 4 characters to leave room for the terminating null character Common programming error:

      char s1[4]= "Hello"; /* array size is not enough */
      char s2[5]="fawaz";  /* array size is not enough */


example

   include<stdio.h>

int main() { char fname[20], midname[3],lname[20]; printf("Enter your first name:"); scanf("%s",fname); printf("Enter your middel name:"); scanf("%s",midname); printf("Enter your last name:"); scanf("%s",lname);

printf(" your full name is :\n %s %s %s", fname, midname, lname); }