Skip to content

Commit

Permalink
strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nihalxkumar committed Mar 14, 2024
1 parent 59682e8 commit 104fc13
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Learning-C/Strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Strings

Strings, represent arrays of characters and are fundamental for handling text data.

### Initialization

Strings in C can be initialized in a couple of ways:

```c
char x[3] = {'a', 'b', '\0'};
```
Here, '\0' denotes the null character, which terminates the string. It's essential for string manipulation and is represented by ASCII 0.
Alternatively:
```c
char x[3] = "ab";
```

This form automatically appends the null character at the end of the string.

It's crucial to ensure that the size of the array is one more than the number of characters in the string to accommodate the null character properly. Otherwise, it can result in compilation errors such as 'Too many initializers.'

```C
char x[10];
x = "abc";
printf("%s", x[6]);
// This will result in an L-value error
```
Attempting to assign a string directly to an array (x) will result in an L-value error. Instead, strings can only be assigned to pointers in C.
| gets() | scanf() |
| -------------------------------------------------------------------- | ------------------------------------------------------- |
| Accepts only input strings | Accepts input for various data types |
| Recognizes space | doesn't recognize space |
| No need for the '&' symbol | Requires the '&' symbol for input variables |
| Allows input of only one string at a time `gets(x/y) //not possible` | Can accept multiple strings at once `scanf("%s%s",x,y)` |
| <string.h> | <stdio.h> |
Similar distinctions apply to output functions like `puts()` and `printf()`.
`puts()` automatically moves the cursor to the next line after printing the string, whereas `printf()` does not. These functions are essential for outputting strings efficiently in C programs.

0 comments on commit 104fc13

Please sign in to comment.