Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main()
- {
- ///Declaration
- char s1[20]; // just declare the char array. Currently, in each position of the array is in garbadge value.
- printf("In declaration: \n");
- printf("Before input values in character array are: \n");
- for(int i = 0; i<20; i++) printf("%d\n", s1[i]);
- printf("\n.......................\n\n");
- // Here we print the array. we'll notice that every postion will be set garbadge. Here we print the ASCII value of each character.
- /// note that, if we declare char s1[20] this globally, every position of the array will be initialized with Null (means 0 in integer).
- printf("Enter a string as a word: ");
- scanf("%s", s1);
- // %s can take only a word as an input.
- // If we want to take whole line (including space), We must use "gets()". Syntax: gets(s1);
- // both %s and gets() initialize the array upto the characters you've entered from keyboard.
- printf("After input values in character array are: \n");
- for(int i = 0; i<20; i++) printf("%d\n", s1[i]);
- printf("\n.......................\n\n");
- // let's say, you've entered "MIST" as an input.
- // It will take 4 position (from 0 to 3) in that char array. Rest of the positions willa remain garbadge.
- printf("You entered: %s /// printed using printf()\n", s1);
- puts(s1); /// printed using puts()
- printf("\n.......................\n\n");
- // printf("%s",s); and puts(s); both print the character array up to Null characher ('\0').
- ///Definition
- char s2[20] = "MIST";
- // Here declare a char array and Initialize it's first 4 positions (0 to 3)
- printf("In definition values in character array are: \n");
- for(int i = 0; i<20; i++) printf("%d\n", s2[i]);
- printf("\n.......................\n\n");
- // Here we print the array.
- // We will notice that, rest of the positions (4 to 19) will be set NULL (means 0 in integer).
- puts(s2);
- return 0;
- }
Add Comment
Please, Sign In to add comment