Since developing a love for STEM, I have always tried to include all the stuffs that I discovered my own and those which has seemed to me very important and also for learning and storming brain. In this pursuit I've added several contents here including those from YouTube, Google, ChatGPT, Gemini, Websites, some books and from the learning stages for my self growth and development. I have tried to include all the contents that I have thought and expressed as well as organized on my own approach up to this day.
If you store an integer variable inside an integer type variable and then print it by changing its format specifier from %d to %c, you will get character, on the contrary if you store a character variable inside an character type variable and then print by changing its format specifier from %c to %d, you will get integer value. Do you know why it happened? It has happened because of the ASCII numbers.
For the first case-
#include <stdio.h>
int main(void){
char ch = 'a';
printf("%d", ch); /*97*/
return 0;
}
For the second case-
#include <stdio.h>
int main(void){
int ch = 97;
printf("%c", ch); /*a*/
return 0;
}
We are going to discuss about string. In C, string is an
array of char type data. In C, we can't
directly deal with string through string data type
like C++ or Java or JavaScript. So
we are to deal with string related operation through char data type.
There is given below how we can declare string in C.
#include <stdio.h>
int main(void){
char name[7] = {'S', 't', 'r', 'i', 'n', 'g', '\0'}; //Way 1
char name[] = {'S', 't', 'r', 'i', 'n', 'g', '\0'}; //Way 2
char name[] = "String"; //Way 3
char name[7] "";
name[] = "String"; //Way 4
return 0;
}
There are something notable here. For \(1\)st case, we declared string inside a pair of curly braces and also can see something
'\0' this is called null character. Obvbiously it takes memory. By declaring this in C, computer can understand that
our declared array of character has been finished before the null character. So it is very important. If we don't write this, computer can print something garbage or it will think
that string is infinite. To avoid these stuffs, its better to write the null character at the end. When computer find the null
character, it stops printing after the null character. For \(2\)nd case, we didn't declare the array size because computer can
understand the array size automatically. For \(3\)rd case, we declared the string variable inside a pair of quoatation mark. And
there is no needed to declare null character because by declaring string this way, computer will automatically understand at the
end, there exists a null character and it stops printing. For the last as well as \(4\)th case, Firstly we declared our variable by its size as well as declared void and then stored the string.
Each of the cases are independent.
Now we are going to see how computer stores string in C.
#include <stdio.h>
int main(){
char name[] = "String";
int size = sizeof(name)/sizeof(name[0]);
printf("Size of the string is %d", size); // 7; 'String' takes 6 bytes of memory and 1 byte memory is for null character
return 0;
}
Now we will see how we can encrypt our message through converting them in ASCII number. It was discussed in Note \(1\). Check it out.
To do that so, we are to add string.h header file. It is used for manipulating string or character type data's operation.
#include <stdio.h>
#include <string.h>
int main(){
char name[] = "String";
int n = strlen(name);
for(int i = 0; i < n; i++){
printf("%d", name[i]); // 83116114105110103
}
return 0;
}
So as we can see, the ASCII format of our String variable is \(83116114105110103\) and that's how we can encrypt any mesasge by
converting to ASCII format and its a little bit harder to decrypt (we'll do soon later by C) because usually it requires
Python script, JavaScript or Java or any other programing lanuguages. Now we'll do the same thing again by not
using string.h header file and by using our conventional method and its very simple but there is hidden something that we are going to explore.
#include <stdio.h>
int main(){
char name[] = "String";
int n = sizeof(name)/sizeof(name[0]);
for(int i = 0; i < n; i++){
printf("%d", name[i]); // 831161141051101030
}
return 0;
}
Did we find any difference between first and second example? Of course there is some difference. Let's go to the structural
difference. Structural difference means we didn't add string.h header file. Instead of this, we have determined
n by our most conventional popular method which is sizeof(name)/sizeof(name[0]). Here null characteris also
taken at the last. The ASCII value of '\0' is \(0\). So it is added lastly where previously our strlen(name)
didn't take null character. Because, strlen() function doesn't count null character whenever it is needed to calculate the
size of the string. strlen() means the string's length. This is a built in function in C. To use that, we must add string.h header
file. So, the final statement is, if we want the original string size, then usually we are go through
sizeof(array)/sizeof(array[0]) and there null character is counted. But when we don't need to work with null character, then we are to
use strlen(array) function in order to avoid the counting of null character.