Beginners Tutorial : The C-language.

simplycoder
@simplycoder-NsBEdD โ€ข Oct 26, 2024

Hello this is simplycoder, and welcome to the series of C programing tutorials.

I request not to post questions or anything in this thread, as it will only disrupt the flow and co-ordination of the tutorials.


OK everyone, this tutorial is basically designed for programmers, basically new to computing or new to "The C-Language".
So now let us get started with the C.


History of C


  • C language was developed by Dennis Ritchie in early 70s at the Bell Laboratories for Unix operating system.
  • C is a procedural language.
  • C was influenced by B, ALGOL, FORTRAN and Assembly.
  • C influenced most of the modern world programing languages which include the mighty C++ and Java as well.

Above points are enough to answer the most common questions asked in vivas (oral-examinations) or objectives.

As of now, this much is enough for more, google for C.
Tutorial-1 will be posted in another dedicated post.
Until then take care
Ciao.

PS: These tutorials will be based on DevC++ IDE and on Windows platform
you can download DevC++ from here #-Link-Snipped-#
-------------------------------------------------------------------------------------------------------------------------------------
INDEX
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
#-Link-Snipped-#
-------------------------------------------------------------------------------------------------------------------------------------

โ€‹

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • simplycoder

    @simplycoder-NsBEdD • Jan 5, 2013

    In that case, kindly verify the it with my code, if you have copy pasted it, copy paste it again and let us know.

  • rahul69

    @rahul69-97fAOs • Jan 5, 2013

    #-Link-Snipped-#

    Regarding the line ,c*c=b*b+c*c.... i didnt know about the findsum function

    and

    it gives an error "undefined reference to' findsum' "

    From above quotes I guessed that u are probably considering findsum as pre-existing like Library functions, which is not true, this findsum function is a user defined function and hence u will have to also write the function definition (ie function code) in your program. This "undefined reference" error also comes when u use a function without including its definition, and only declaration. Hope it helps!!!๐Ÿ‘

  • SamiZzY

    @samizzy-ByLUpZ • Jan 5, 2013

    #include <stdio.h>
    #include <stdlib.h>
     
    int main(int argc, char *argv[])
    {int total_number,index,final_ans;
     
    printf("Give the number upto which the square of the numbers are to be added::\n");
     
    scanf("%d",&total_number);
     
    final_ans=0;
     
    for(index=1;index<=total_number;index++)
    {
    final_ans=final_ans+(index*index);
    }
    printf("The Sum of the squares of %d numbers is %d",total_number,final_ans);
      system("PAUSE"); 
      return 0;
    }

    the code you gave me , it worked but now i am even more confused.๐Ÿ˜•
    Above is the code which i wrote after observing yours(for finding sum of squares of n num and it works XD).
    the reason why it was giving error was because i copy pasted the code only uptill Main Method-End.
    I thought above and below main method-end in your program are two different codes , one for the sum of n numbers and the other for the sum of the squares of n numbers.
    confused confused confused.!

  • Jeffrey Arulraj

    @jeffrey-xA7lUP • Jan 5, 2013

    That main method end was in comment and SC used Function call in his program The findsum function was called in the main program.

    They are different functions But they are same programs

    PS: // any line beginning with this '//' is a comment line and they are not considered by the compiler They are given for your understanding and does not mark the end of the program

  • SamiZzY

    @samizzy-ByLUpZ • Jan 5, 2013

    rahul69#-Link-Snipped-#

    and

    From above quotes I guessed that u are probably considering findsum as pre-existing like Library functions, which is not true, this findsum function is a user defined function and hence u will have to also write the function definition (ie function code) in your program. This "undefined reference" error also comes when u use a function without including its definition, and only declaration. Hope it helps!!!๐Ÿ‘

    yeah i was beginning to think like that thanks for clarification. See the program worked now because i copied the whole code. so maybe it is defined somewhere ? i cant find it lol help

  • SamiZzY

    @samizzy-ByLUpZ • Jan 5, 2013

    ConquerorThat main method end was in comment and SC used Function call in his program The findsum function was called in the main program.

    They are different functions But they are same programs

    PS: // any line beginning with this '//' is a comment line and they are not considered by the compiler They are given for your understanding and does not mark the end of the program

    yes i knw that they are comments, i just used it so as to say below or above it.

  • simplycoder

    @simplycoder-NsBEdD • Jan 5, 2013

    SamiZzY
    #include <stdio.h>
    #include <stdlib.h>
     
    int main(int argc, char *argv[])
    {int total_number,index,final_ans;
     
    printf("Give the number upto which the square of the numbers are to be added::\n");
     
    scanf("%d",&total_number);
     
    final_ans=0;
     
    for(index=1;index<=total_number;index++)
    {
    final_ans=final_ans+(index*index);
    }
    printf("The Sum of the squares of %d numbers is %d",total_number,final_ans);
      system("PAUSE");
      return 0;
    }
    the code you gave me , it worked but now i am even more confused.๐Ÿ˜•
    Above is the code which i wrote after observing yours(for finding sum of squares of n num and it works XD).
    the reason why it was giving error was because i copy pasted the code only uptill Main Method-End.
    I thought above and below main method-end in your program are two different codes , one for the sum of n numbers and the other for the sum of the squares of n numbers.
    confused confused confused.!

    Check in function declaration section.

  • SamiZzY

    @samizzy-ByLUpZ • Jan 8, 2013

    sir, ... it seems i am stuck again but this time for sure, i know the logic.
    Below is the code for verifying an armstrong number(tut: 13 exercise 3) and thankfully it works.๐Ÿ˜‰

    #include <stdio.h>
    #include <stdlib.h>
     
    int main(int argc, char *argv[])
     
    {int input,index,final_ans,index2,n,temp,digit,original_input;
    printf("Give the number to be verified if it's an armstrong number or not ::\n");
    scanf("%d",&input);
    final_ans = 0;
    index=0;
    n=0;
    //FINDING THE NUMBER OF DIGITS-START.                                     
    while(temp!=0)          //when temp reaches 0 stop the loop
    {index=index+1;       
    temp=input/pow(10,index);
    n++;                    // n is initially 0 , placed n here as there will be an increment in n by +1 with each loop
     
    }
    //FINDING THE NUMBER OF DIGITS-END.
    original_input=input; // storing the input in another int as our integer will be changing in the main code
     
                   
    //FINDING THE FINAL ANSWER PART.(MAIN CODE)
    for(index2=1;index2<=(n);index2++)
    {
      digit = input%10;          //we get the unit place digit for the input
    final_ans = final_ans + pow(digit,n); //the digit is raised to nth power and added to other nth power digits.
    input=input/10;                      //since the input's last digit is found, in order to remove last digit we divide by 10.
                                           
    }
    if(final_ans==original_input)
    printf("The given number %d is a Armstrong number\n",original_input);
    else
    printf("The given number is not a Armstrong number\n");
     
      system("PAUSE");   
      return 0;
    }

    so one may think that, if verifying a number is possible then printing armstrong number is no biggie.
    I thought so too. i wrote the code, and i have no idea why it just prints 1 infinitely.

    #include <stdio.h>
    #include <stdlib.h>
     
    int main(int argc, char *argv[])
    {    int index,index2,final_ans,original_index,digit;
     
    for(index=100;index<=999;index++)
    {
    original_index=index;
    final_ans=0;
     
    for(index2=1;index2<=3;index2++)
    {
    digit=index%10;
    final_ans=final_ans+pow(digit,3);
    index=index/10;
    }
     
    if(final_ans==original_index)
    printf("%d\n",final_ans);
    }
     
      system("PAUSE");   
      return 0;
    }
    

    Above is the code for printing armstrong number from 100 to 999(tut 13 exercise 4).
    It would be very helpful if you could correct me. Thankyou in advance.

  • Jeffrey Arulraj

    @jeffrey-xA7lUP • Jan 9, 2013

    for(index2=1;index2<=3;index2++)
    {
    digit=index%10;
    final_ans=final_ans+pow(digit,3);
    index=index/10;
    }

    instead of the variable index use original_index here that should solve the problem here

  • SamiZzY

    @samizzy-ByLUpZ • Jan 9, 2013

    i replaced the l.h.s index with original index int the last line(is that what you meant?๐Ÿ˜• ) , and the output was 24 and 81. pretty strange because index changes from 100 to 999 but thankyou for the reply mr.jeffrey ๐Ÿ˜€

  • Jeffrey Arulraj

    @jeffrey-xA7lUP • Jan 9, 2013

    for(index2=1;index2<=3;index2++)
    {
    digit=index%10;// Replace this index
    final_ans=final_ans+pow(digit,3);
    index=index/10;// and also here both the index has to be varied
    }

    Cos your program is having a logical error since you are manipulating the the control variable of the outer loop in the inner most loops So you are getting an error here

  • SamiZzY

    @samizzy-ByLUpZ • Jan 9, 2013

    thankyou sir.! noting what you said , replaced all the var index with original index in the 2nd loop
    and at the bottom if condition replaced original index with var index and by god's grace it worked .!
    thankyou again.

  • Jeffrey Arulraj

    @jeffrey-xA7lUP • Jan 9, 2013

    SamiZzYthankyou sir.! noting what you said , replaced all the var index with original index in the 2nd loop
    and at the bottom if condition replaced original index with var index and by god's grace it worked .!
    thankyou again.

    It is my pleasure to help you Pls avoid Siring me I ama student just like you

    We are all are here to help you and learn from you

  • Ashish Saklani

    @ashish-saklani-yoI0A0 • Feb 19, 2013

    thank u simplycoder this tutorials will help me in programming.

  • simplycoder

    @simplycoder-NsBEdD • Feb 20, 2013

    Ashish Saklanithank u simplycoder this tutorials will help me in programming.

    Its glad to hear.

  • lit-857

    @lit-857-hXUEGJ • Mar 27, 2013

    sir u should give the ans of exersies

  • simplycoder

    @simplycoder-NsBEdD • Mar 27, 2013

    lit-857sir u should give the ans of exersies

    The best part about programming is that you know the correct answer.
    If you follow the tutorial throughout then I think you wouldn't find it difficult(challenging has a different meaning.)

    Still if you don't get the output, show your work and then we can debug it or see where it goes wrong.

    Tip:The best way to solve is to execute it by hand for small input.

  • lit-857

    @lit-857-hXUEGJ • Apr 2, 2013

    what d/f b/w float and double datatype

  • Jeffrey Arulraj

    @jeffrey-xA7lUP • Apr 2, 2013

    lit-857what d/f b/w float and double datatype

    float datatype allows only 4 bytes of memory Whereas the double type allows 8 bytes

    Only the range of these two differs not their other types

  • lit-857

    @lit-857-hXUEGJ • Apr 4, 2013

    i know very well but i want to get understend with exmple

  • Ganesh MSD

    @ganesh-msd-Gf3RlP • Apr 7, 2013

    Nice article...!! This is simply awesome. So useful.

  • Neeraj Sharma

    @neeraj-iAaNcG • Jul 1, 2013

    lit-857i know very well but i want to get understend with exmple

    As already stated before, the main difference lies in the range. Float is single precision and double is double precision floating number. It is called double precision as it is twice of a normal float variable.

    As per your request here is an example:

    Suppose ABC person is very rich and has a huge amount of money (so much that it cant be represented in a single precision number). If you want to store that amount of money in a float variable, then it will truncate the double precision number to single precision which in turn will reduce the amount of money that the person holds. Isn't it a problem? Yes it is. That is the main reason for existence of double precision data types i.e. to store huge values.

    The example that I gave is quite vague but I think it will help you in understanding the difference clearly..

  • Vishal Sharma

    @vishal-pysGmK • Aug 9, 2013

    try coding this!!! A good question!

    Write a C program to Remove even numbers and fill it with zeros.
    Note: You can use only one loop for your logic.
    Two variables apart from the array.
    Input array 1,2,3,4,5,6
    Output 1,3,5,0,0,0

  • Vishal Sharma

    @vishal-pysGmK • Aug 13, 2013

    Vishal0203try coding this!!! A good question!

    Write a C program to Remove even numbers and fill it with zeros.
    Note: You can use only one loop for your logic.
    Two variables apart from the array.
    Input array 1,2,3,4,5,6
    Output 1,3,5,0,0,0


    no one is answering to this?

  • Nayan Goenka

    @nayan-Dhpt4N • Aug 13, 2013

    Vishal0203no one is answering to this?


    I didnt read this. I will post my code soon enough. Give me some time to code. By the way, you want the complete code or just function?

  • Nayan Goenka

    @nayan-Dhpt4N • Aug 13, 2013

    Here is my code. ๐Ÿ˜ Its quite tricky to get the output as you wanted. Odds first then 0's ๐Ÿ˜› Anyways it was good exercise with C++ for me.

    #include<iostream.h>
    #include<conio.h>
    void main()
      {
        clrscr();
        int input[6],output[6],i,j=0;
        cout<<"\n Enter the Inputs :";
     
     
        while (j>=0)
          {
        for(i=0;i<6;i++)
          {
            cin>>input[i];
            if( input[i] % 2 != 0)
              {
            output[j] = input[i];
            j++;                                                              }
          }
        break;
        }
     
     
        for(j;j<6;j++)
          {
        output[j]=0;
          }
     
        cout<<"\n The output is: ";
     
        for(j=0;j<6;j++)
          cout<<" "<<output[j];
     
        getch();
        }
    
  • Vishal Sharma

    @vishal-pysGmK • Aug 13, 2013

    Nayan GoenkaHere is my code. ๐Ÿ˜ Its quite tricky to get the output as you wanted. Odds first then 0's ๐Ÿ˜› Anyways it was good exercise with C++ for me.

    #include<iostream.h>
    #include<conio.h>
    void main()
      {
        clrscr();
        int input[6],output[6],i,j=0;
        cout<<"\n Enter the Inputs :";
     
     
        while (j>=0)
          {
        for(i=0;i<6;i++)
          {
            cin>>input[i];
            if( input[i] % 2 != 0)
              {
            output[j] = input[i];
            j++;                                                              }
          }
        break;
        }
     
     
        for(j;j<6;j++)
          {
        output[j]=0;
          }
     
        cout<<"\n The output is: ";
     
        for(j=0;j<6;j++)
          cout<<" "<<output[j];
     
        getch();
        }
    

    Was I not clear??? read the question! just 1 loop and 2 variables apart from array!

  • Nayan Goenka

    @nayan-Dhpt4N • Aug 13, 2013

    Vishal0203Was I not clear??? read the question! just 1 loop and 2 variables apart from array!



    Meh, Put the for loop outside while, inside. It will work that way too. And if you are using 2 arrays, you have to use two loops atleast.

    If you want ONE loop then the output you will get will be 103050 and not 135000

  • Vishal Sharma

    @vishal-pysGmK • Aug 14, 2013

    Nayan GoenkaMeh, Put the for loop outside while, inside. It will work that way too. And if you are using 2 arrays, you have to use two loops atleast.

    If you want ONE loop then the output you will get will be 103050 and not 135000

    hahahahahahha... ๐Ÿ˜
    that's the challenge bro don't tell me that it cant be done. It can be done! try!
    one loop.. one array.. 2 supporting variables

  • Vishal Sharma

    @vishal-pysGmK • Aug 14, 2013

    Nayan GoenkaMeh, Put the for loop outside while, inside. It will work that way too. And if you are using 2 arrays, you have to use two loops atleast.

    If you want ONE loop then the output you will get will be 103050 and not 135000


    and yeah! the loop should be common to take inputs arrange values and to display output!

  • simplycoder

    @simplycoder-NsBEdD • Aug 14, 2013

    I would take a try to crack this,
    ย 
    #includeย "stdio.h"
    ย 
    intย main(intย argc,char**argv)
    {
    intย index=0;ย //ย Variableย 1
    intย temp_index=0;ย //ย Varibleย 2
    ย 
    //ย Sizeย consideredย asย 6ย hardcodedย :(
    for(index=1,temp_index=1;index<=6||temp_index<=6/2;index++)ย //ย Oneย Loop.
    {
    //ย Printย theย oddย numbersย first.
    if((index&1)&&(index<=6))
    {
    printf("%d",index);
    }
    //ย Printย 0ย forย theย evenย numberย later.
    elseย if(index>6)
    {
    temp_index++;
    printf("0");
    }
    ย 
    }
    printf("\n");
    returnย 
    0;
    }
    Didn't utilize array part as well I have hard coded the values.

  • Vishal Sharma

    @vishal-pysGmK • Aug 14, 2013

    and my code for the question is this... kinda similar! and for n values!

    #include<stdio.h>
    int ar[100],i = 0; //array and 1 var
     
    int main() {
        int n; //2nd var
        printf("enter range : ");
        scanf("%d",&n);
        printf("enter the numbers : ");
        while(i < n) {
            if(i >= n/2) {
                printf("%d, ",ar[i]);
                i++;
                continue;
            }
            scanf("%d",&ar[i]);
            if(ar[i]%2 == 0) {
            }
            else {
                ar[i] = ar[i];
                printf("%d, ",ar[i]);
                i++;
            }
        }
        return 0;
    }
  • simplycoder

    @simplycoder-NsBEdD • May 4, 2014

    Re: Beginners Tutorial : The C-language:: Tutorial-27
    Pointers in Cโ€‹I would break this main tutorials in many sub tutorials in order to highlight each of the concept related to pointers.
    In this tutorial I would be covering the basic definition,usage,need and the Address Of Operator in C POINTERS.
    ---------------------------------------------------------------------------------------------------
    If I have to point out the feature in C language, which is most widely misunderstood by begineers in C, then I wouldnot have second thoughts to say its POINTERS.
    Basically begineers have difficulty in understanding the pointer terminology implemented by C.
    There is no programing language, which doesnot use pointers. However, very few (C being one of them) gives programmers a chance to use it in day to day coding.

    Pointers are like double edge swords and programmer must take care to use it properly.

    • #What are pointers:

    A pointer is a variable which has address of the memory location of another variable.
    eg. Suppose I declare an integer 'A' and compile the program. The compiler would store a finite memory for this integer at some location. Every memory location has unique address. So its safe to say that a memory location is like a house and like every house has unique address, every memory location would have unique address.

    Now I would like to store the address of the memory location where integer 'A' has been stored in a variable 'X'.
    This variable 'X' is called as pointer.

    • Basic Pointers:


    1. Address Of Pointer:

    Assume a declaration like
    int x=4;
    When we compile the program, compiler does the following tasks:

    ->Reserve space in memory to hold the integer value.
    ->Associate the name x with the above memory location.
    ->Save value 4 in this memory location.

    Lets say that compiler chose the memory location with address '1000' to store the variable x.
    Then the pointer of x would have value 1000 in it.

    Let us write a sample code for this.

    //Code
    
    #include <iostream>
    int main(int argc,char**argv)
    {
      
    int x=4;
    printf(" Value of x : %d\n",x);
    printf(" Address of x : %d\n",&x);
       system("PAUSE");
       return 0;
    }
    
    
    //Output :
    Value of x : 4
    Address of x : 2686788
    Press any key to continue . . .
    

    The '&' operator that is being used here, is called as the 'Address Of' operator.
    This operator, returns the address of the memory location where in the value of the variable is stored.
    Please note the output of the address may(and would in most of the cases) differ from the output given here.

    I leave the task of reading further about Address Of operator and linking it to the scanf functionality, to the reader.
    โ€‹In the next tutorial, I shall write on Value At Address (*) operator.

    Until the next time,take care.
    Ciao.

    โ€‹

  • Vishal Sharma

    @vishal-pysGmK • Jun 2, 2014

    I thought of sharing this with everyone. A good way to program and test your programs. Most of beginners generally code every logic inside the main() and to test their program they execute the code n number of times (n test cases) each time entering a test case. That's really annoying for the person who has written the code and they may skip most of the test cases thinking they'll get a correct output for all of them. But, this can be done in an interesting way where the evaluation of the code is done by the code itself i.e. no need of executing the code again n again.

    A programmer needs to understand that it is always a good practice to divide the problem in as many functions as possible, keeping in mind that each function performs only a particular task.
    I'll not make the post much longer so we'll have a quick look on an example.
    Let us pick up a small program like, Sum of the elements of an array. Now how to start? Well, just define a structure first (I'll explain why we are doing it)

    struct test
    {
        int input[10];    // involves your test case arrays
        int terms;         // the number of term in your array
        int output;        // the output which you are expecting
    } testVals[4] = {   // these are your test cases you can add as many you like (4 here)
                                {{1,2,3,4,5,6,7,8,9,10} , 10, 55},  // initializing input, terms, expected output
                                {{2,4,6,8} , 4, 20},
                                {{3,6,9} , 3,  18},
                                {{1,2,3,4,5} , 5, 15}
                    };
    

    I've added comments and it's self explanatory. Once we are ready with the structure which holds our inputs, we need to have a function which will solve our base problem that is to find the sum of elements of an array. So, lets code that as well

    int sumFinder(int *input, int terms)
    {
        int sum = 0;
        for(int i = 0 ; i < terms ; i++)
             sum += input[i];
        return sum;
    }
    

    So, our base function is ready which takes the input of an array and the number of terms in the array, finds sum and returns the sum obtained.
    We still don't have a function which passes test cases to our base function. Let's code that as well.

    void testCases()
    {
        int result;
        for(int i = 0 ; i < 4 ; i++)      // for passing 4 test cases
        {
             result = sumFinder(testVals[i].input,testVals[i].terms);  // sending the values
             if(result == testVals[i].output)    // checking the obtained result
                 printf("PASSED\n");
             else
                 printf("FAILED\n");
        }
    }
    

    so our testCases() is pretty powerful which passes all the test cases 1 by 1 and even checks whether we have arrived to a correct solution or not, hence all we need to do is to fill in the structure with as many test cases you like and remaining processing is done by the code.
    Yeah, we are missing our main function. Lets see the complete code together,

    #include "stdio.h"
    
    struct test
    {
        int input[10];    // involves your test case arrays
        int terms;         // the number of term in your array
        int output;        // the output which you are expecting
    } testVals[4] = {   // these are your test cases you can add as many you like (4 here)
                                {{1,2,3,4,5,6,7,8,9,10} , 10, 55},  // initializing input, terms, expected output
                                {{2,4,6,8} , 4, 20},
                                {{3,6,9} , 3,  18},
                                {{1,2,3,4,5} , 5, 15}
                           };
    
    int sumFinder(int *input, int terms)
    {
        int sum = 0;
        for(int i = 0 ; i < terms ; i++)
             sum += input[i];
        return sum;
    }
    
    void testCases()
    {
        int result;
        for(int i = 0 ; i < 4 ; i++)      // for passing 4 test cases
        {
             result = sumFinder(testVals[i].input,testVals[i].terms);  // sending the values
             if(result == testVals[i].output)    // checking the obtained result
                 printf("PASSED\n");
             else
                 printf("FAILED\n");
        }
    }
    
    int main()
    {
       testCases();
       return 0;
    }
    

    So that's the complete code which looks huge but as I said its very good practice as you just need to add more test specs and every other thing is dealt. The same practice can be used for other codes as well, you just need to change the things you pass from structure, your result checker. (you can use a separate function for checking as well) that's all for now! If you have any questions, add comments ๐Ÿ˜€

  • Raman Muniyappan

    @raman-muniyappan-T7mihd • Dec 6, 2014

    thankksss these are very help ful