C language chapter 1

 PRO C LANGUAGE COURSE


Introduction to the course

This is a high-level C language course where I will teach you everything in the c language. It is divided into the Several chapters and after all chapter’s concept, we will do some practice and questions and quizzes during the lesson. After you will learn the full language we will do 5 projects so be ready to start your journey.

What is programming?

You are in start in programming so you will have to understand that what is programming? Let’s answer the question.

Example you want to talk with your friend, or you must give him order so you will tell him in your language. If you must talk with the computer so you cannot communicate directly with the computer, so you must tell him with his own language computer language. For this reason, we must learn some specific languages. For example, Python, C, C++, C#, and Java. The apps you have seen in your daily life like Google, Amazon etc. are written in some specific programming languages. So, the answer to this question is it is a procedure to communicate with a computer.

 

 

Installation setup in C language

Before we write the code, we will download the IDE (Integrated Development Environment) and compiler.

We will use Visual Studio code to write our code and download MinGW for our compiler.

To download VS code or Visual Studio code go to this link

https://code.visualstudio.com/download

Click on this link to download VS code then you will reach at this page

 


Click on the button of your operating system If  you are in windows so click on Windows, if you are in Linux then click on the Linux, If you are in Mac then click on the Mac.

After you will see a page



You have some text there so read it and then click on the “I agree with this” option

After you will see this window



And click on Next. After click on the install option

Congratulations you have downloaded the VS code.

Create a folder for your C language programs

Do right click inside the folder and click on Open with code with the visual studio icon

Now we must download MinGW compiler. First search MinGW on Google and click on the first link of Source Forge and click on download. After you will find a page. Click on download. When your MinGW is downloaded so you click it in gcc c compiler. After this you have to set the path. To set path I want also to improve your AI skills so you go to ChatGPT and give him this prompt.

"Hello I want to set path of mingw in my computer for C programming give me a video for it of tell me the steps".

 The C language

 

 C language is a high-level language that was made by Dennis Ritchie in 1972 to make Unix operating system.

C language is very fast.

C language is used to make system-based applications like OS, Linux etc.

It is an advantage of learning C language if you have learned C language so the complex languages like C++, Java or JavaScript will be easier to learn many things are same as functions arrays are same in other languages, but the syntax is different. For example, to printf(); statement in C but we use Count in C++.

C is also used in Data Structures and Algorithm.

 

Basics of C

Our first program

 // Our first program

#include <stdio.h>

int main(){
    printf("Hello World\n");
    return 0;
}

Test this program

Variables

Variables are like container for example when you must store something so you will pack it in a box or container. Like an intuitive understanding.



Yepboi buy a PS5. The shopkeeper first pack it and give it to Yepboi. He went to home and unboxed it. After unboxing he play game. That is also same in the variables. Variable is a box in which we store values and after we do the operations that are needed to be done. The value can be Integer, float, character. For example: I declared a variable and initialized it at 2 and declared another variable and initialized it at 3. Then I want to do their sum so first I will open the box where 2 and 3 are stored then do their sum and return 5.

The syntax of declaring variable is follows.

 int a = 34;

In this we create a variable and store 34 in it.

Declaring and initializing

The meaning of declaring is that to name any variable, In the above example we declare a which is a variable. Initializing a variable means that storing the value in the box or Variable. For example, a = 34 means that a is initialized as 34, The value of a is 34, a consist of 34, It can be explained as many things. We have learned variables in many details. Now we must do some operations with these variables



Pro tip: You can use ChatGPT to clear your queries like if something I do not explain you so you can use ChatGPT. In the operations of the variables, we will use format specifier so I will not tell you all the format specifiers so you can give the ChatGPT this prompt You can also ask the meaning of first program you have written

Hello ChatGPT give me the names of all format specifiers used mostly in the C language.

Now back to the topic.

Data types

Data types are the types of data that are to be stored. Example integer, float, character.

These are major data types these are written in major formats

For example, int for integer. Float for floating point number, char for a single character

Here is the version of declared data type

int i = 5; // It stores an integer.
    float f = 4.5; // It stores a decimal number.
    char c = 'A'; // It stores character.

Format specifier

 The format specifier are used to represent any data type you can use ChatGPT to know about format specifier. I will tell you few for till we are working. After you grow in the c programming we will learn about more format specifiers. Here is the format specifier for integer, float, and character that we will use.

    int i = 5; // It stores an integer.
    float f = 4.5; // It stores a decimal number.
    char c = 'A'; // It stores character.
    printf("%d", i); // format specifier for Integer
    printf("%f", f); // Format specifier for float
    printf("%c", c); // format specifier for charachter

You can run this program but ensure it that it is inside the main() function do not ask that what is main I tell you that you can use ChatGPT to know about basic structure. After the completion of the chapter I will tell you about basic structure of C language.

Before you start I want to give you about the snippet of VS code that will make it easier to write your code

This snippet is made for the C language boilerplate.

We will talk about snippets after the lesson.

In the format specifier after what is after the double quotes. This is the definition of the variable

Now we will do a challenge

The question is do sum of 2 and 5.

First, we will make a snippet for C language boiler plate.

Before you make a boiler plate snippet. I would like to answer the question

The answer before you must copy your first program

Then you have to make 2 variables and initialize your first variable as 2,  and second variable as 5.

Then you have to use %d format specifier to print the integer sum and write the definition of the format   specifier That is.

first_variable + second_variable

Now I will show you the program

 #include <stdio.h>


int main(){
    int a = 2;
    int b = 5;
    printf("The sum of 2 and 5 is %d", a+b);
    return 0;
}

Now we will make a snippet the procedure to make a snippet is go to the setting option and click on it. Like this

It is located at bottom left corner of the visual studio code

 


After this you must select your language but you are working on C language so you will click on C like this



After this you will see a default page I have already make the snippet so I will see another page


Copy this snippet in your VS code snippet

{
    // Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
    // same ids are connected.
    // Example:
    "C boilerplate": {
        "prefix": "c boiler",
        "body": [
            "#include <stdio.h>\n",
            "int main(){",
            "\t$0",
            "\treturn 0;",
            "}",

        ],
        "description": "The C language boilerplate"
    },
    "myprintf": {
        "prefix": "myprintf",
        "body": [
            "printf(\"$0 \\n\");\n",

        ],
        "description": "The C language boilerplate"
    }
}

Then when you type c boiler than C boiler plate code will be provide and typing myprintf you will get a printf()

Taking input from the user

Now you will take the input from the user to store values for example When you want to take an auto so you go to there bur when you ask him the price so he say that as you wish so it means that the variable where price is stored is not initialized he is asking you the value of the price so you deal him with a possible price and he stores the value of price. Same is like the variables you do not initialize the variable and ask from the user to store its value here is the syntax.



Now let’s take an example you must store a value of a variable by the scanf() function.

#include <stdio.h>

int main(){
    int a;
    printf("Enter the value of a:  \n");
    scanf("%d", &a); /* & opreator to store it's value or address.
     What is address you will learn about this in pointers but for this time you should remember that it is
     the syntax of storing the value of variable it is important to write &*/
    printf("%d", a);
    return 0;
}

Challenge for you: Now you must solve the previous question about the sum you have to do it using scanf() function by taking the input from the user. You can also use ChatGPT to solve the queries

KEYWORDS

Keywords are the defined words that are understood by the compiler you have learned many keywords but I will not tell you all the keywords so that you can ask this prompt from ChatGPT.

“Hey ChatGPT tell me the names of all keywords that are used in C language”.

To learn that what is the memory that int, float, char is taking in your computer so you can ask this prompt from ChatGPT

“Yo ChatGPT tell me the memory of int, char, and float in my computer”.

Comments

Comments are made for user. When you are explaining something in a program to fellow programmers. Like I want to understand and explain you the use of any thing I write some comments. I know it will not make you understand like a Gulab jamun. Let me explain. Comments are of two types single line comments and multi line comments

Single line comment

// This is a single line comment

This comment is made in a single line

Multi line comments

/*This                                      is                          
a                   multiline comment       */

This comment is for multiple lines

The shortcut for to comment any line do this
Select any txt and press CTRL + /.

When you will see then you will have a question that what is that I was using it before to explain.

The statements that I use to tell you the syntax is a txt file do not try it to write it in a c file.

Basic structure of C

When we were testing our first program I tell you that I will tell you its meaning in the end of Chapter 1. Now listen
#include <stdio.h>

This is the preprosessive directives or you can also say it the header file of the C program it is written outside the main() function.

int main(){

This is the main function that returns the value. It tells the opreating system that you have to manage the memory in the RAM. When the programs run so it will execute the program in the memory that the opreating system has used.

{
    printf("Hello World\n");
    return 0;
}

This is the main body of the c program. It starts with the curly braces. and end in the last brace.

return 0;

It is the returning value or exit Let me tell you what is the meaning of this. When I was telling you about the main() function so we see that the output is returned by the this function If I would write it before the body the program will return nothing so the meaning of this is that the return 0 return the output to the main() function.

This was the end of the chapter 1 bye bye.

For the source code go to the next blog Next we will do some practice of Chapter 1.

And remember that when  you get the strong foundation in the basics so you will get easier to understand all the things. Bye Bye see you later. I hope this effort will make your basics perfect and do not forget to use ChatGPT for the conclusion.

Comments

Popular posts from this blog

Chapter 1 practice set

Chapter 1 source code