t14ACharactersAndStringVariables

t14ACharactersAndStringVariables - Characters and Strings...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Characters and Strings Literals and Variables Dale Roberts www.kite-legends.blogspot.com Fundamentals of Strings and Characters Characters Building blocks of programs Every program is a sequence of meaningfully grouped characters Character constant An int value represented as a character in single quotes An int 'z' represents the integer value of z Strings Series of characters treated as a single unit Can include letters, digits and special characters (*, /, $) String literal (string constant) - written in double quotes "Hello" Strings are arrays of characters String a pointer to first character Value of string is the address of first character Dale Roberts www.kite-legends.blogspot.com Fundamentals of Strings and Characters String declarations Declare as a character array or a variable of type char * Declare char char color = "blue"; char *colorPtr = "blue"; Remember that strings represented as character arrays Remember end with '\0' '\0' color has 5 elements Inputting strings Use scanf Use scanf scanf("%s", word); Copies input into word Copies word Do not need & (because a string is a pointer) Do Remember to leave room in the array for '\0' Remember '\0' Dale Roberts www.kite-legends.blogspot.com Character Pointers String constant acts like a character pointer char *pc = “ABCDE”; char Variable /* declare a character pointer variable */ declare Value 800 731 constant constant constant constant constant constant pc Address Address 731 732 733 734 735 736 800 ‘A’ ‘B’ ‘C’ ‘D’ ‘E’ ‘\0’ 731 731 ‘A’ 732 ‘B’ 733 ‘C’ 734 ‘D’ 735 ‘E’ 736 ‘\0’ char s1 = “abc”; Variable s1[0] s1[0] s1[1] s1[1] s1[2] s1[2] s1[3] s1[3] Address Address 900 900 901 902 903 Value ‘a’ ‘b’ ‘b’ ‘c’ ‘c’ ‘\0’ ‘\0’ Dale Roberts www.kite-legends.blogspot.com Character Pointers Example: char s1 = “abc”; char *s2 = “abc”; f() { s1[1] = ‘y’; s2[1] = ‘y’; s1 = “test”; s2 = “test”; } s2 800 1100 100 CONSTANT MEMORY AREA (READ ONLY) s1 1000 1001 ‘a’ ‘b’ 100 ‘a’ 101 ‘b’ 102 ‘c’ 103 ‘\0’ 104 ‘t’ 105 ‘e’ 106 ‘s’ 107 ‘t’ 108 ‘\0’ 1002 ‘c’ /* OK */ /* wrong (PC is OK)*/ 1003 ‘\0’ /* wrong */ /* OK */ Example: char s3 = “abcdef”; s f1() 2000 1100 { char *s = s3; *s = ‘A’; /* s3[0]=‘A’ */ s = “test”; printf(“%s\n%s\n”,s,s2); } s3 2000 2001 2002 2003 2004 ‘a’ s3[0]=‘A’ ‘b’ ‘c’ ‘d’ ‘e’ ... 2000 2001 2002 2003 2004 ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ Dale Roberts www.kite-legends.blogspot.com Pointer Arrays Syntax: int *pi[3]; int /* pi[0], pi[1], pi[2] */ /* float *pf[3]; float /* pf[0], pf[1], pf[2] */ /* Example 1: Example 2: int i=1, j=2, k=3; int int *pi[3] = {&i, &j, &k}; int Variable Address 80 82 84 100 101 102 Value 1 2 3 80 82 84 char *pc[3]={“ABC”, “DEF”, “GH”}; Variable constant constant constant constant constant constant constant constant constant constant constant Constant pc[0] pc[0] pc[1] pc[1] pc[2] pc[2] Address 90 91 92 93 Const 94 can not 95 96 be 97 changed 98 99 100 200 202 204 Value ‘A’ ‘B’ ‘C’ ‘\0’ ‘D’ ‘E’ ‘F’ ‘\0’ ‘G’ ‘H’ ‘\0’ 90 94 94 98 98 i j k pi[0] pi[1] pi[2] Dale Roberts www.kite-legends.blogspot.com Command-Line Arguments argc and argv and argv In environments those support C, there is a way to pass commandcommandline arguments or parameters to a program when it begin executing. When main is called to begin execution, it is called with two When arguments – argc and argv argc argv argc : The first (conventionally called argc) is the number of commandargc argc line arguments the program was invoked with argv : The second (conventionally called argv) iis a pointer to an array argv s of character strings that contain the arguments, one per string. of Example: if echo is a program and executed on phoenix prompt, such as echo 10 <phoenix:/home/droberts> echo hello world 10 echo argv argc 3 pointer array e c h o \0 h e l l o \0 null w o r l d \0 Dale Roberts www.kite-legends.blogspot.com Command-Line Arguments Example: print out the arguments. ex: hello world print hello main (int argc, char *argv) { int i; int for (i = 1; i < argc; i++) for printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’); printf(“%s%c”, } main (int argc, char *argv) { while (--argc > 0) while printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); printf(“%s%c”, } main (int argc, char *argv) { while (--argc > 0) while printf((argc > 1) ? “%s “ ; “%s\n“, *++argv); printf((argc } Dale Roberts www.kite-legends.blogspot.com ...
View Full Document

This note was uploaded on 12/08/2010 for the course CSE 2017 taught by Professor Kittu during the Spring '10 term at APIIT.

Ask a homework question - tutors are online