Jump to content

Getting Started with C


Recommended Posts

C is the old and popular high-level language developed Dennis Ritchie in 1972. Though, it is derived from language B which was developed by Ken Thompson.
So, Let's get started.
First of all, you need to download some IDE. Experts though use sublime text 3 for it but beginner should start with DevC++. 
The name of C program ends with extension .c e.g myProgram.c .
Here is the basic C program

 

#include  <stdio.h>                /* Header

int main(void)
{
  puts("Hello world");
  return 0;
} 

In above code:

#include tells the compiler where to find the meanings of standard identifier. I used "printf" for it.


Identifier
       It is the name given to the variable, constant , function in the C program. There are two types of Identifiers:
1- Standard Identifier
    "printf" , "scanf" and library like "Stdio.h" and "math.h" are standard identifiers. You can't change them
2- User-defined Identifiers
         You can give them any name like "onPlay" "getMarks" etc.

Rules for Identifiers:
 

  • The first character must not be any number rather it must be alphabet or ( _ ). using (  _ ) is non-recommended though.
  • Reserved keywords like char, double, float can't be used as Identifier


Keywords:
   Keywords are designed by the developer of Language and we can't change them. It help us a lot to make a program. In C, there are following keywords:

 

auto
do
goto
break
char
default
for
float
short
union
void
int
if
signed
unsigned
sizeof
enum
/* and more


Variable
    Now, let's come to Variable.. It is the named memory location or memory cell. It stores the program's input data and its results after execution.
   These Variable are created in RAM thus it is called "Random access memory" so data stored in RAM is temporary. Example of Variables are:

 

int  marks;
char grade;

here, int and char are keywords while marks and grade are variable names.

 

 

Let's make a simple program

#include <conio.h>

#include <stdio.h>

void main()

 {

   short  a = 10;               // I will name my variable a.

   clrscr();

   printf("%d \n" , a);          

    a = a + 1;

   printf("%d \n", a);

    a = a - 1;

   printf("%d \n" , a);

   getch();

  }

 

You can use words instead of "a" though.  

 

Output

10
11
10

"\n" is the symbol of new line. It is type of encoding "enter/return" key.

"%d" is a keyword for parser in printf function. It describes that here should be printed value from variable passed as next argument to function printf.

  • Like 1
  • Upvote 2

76561198178929243.png

 

Full-Stack Web Developer

Link to comment
Share on other sites

Dear Dopaa

 

It is a nice idea to put "Getting start guide" here.

Unfortunatelly There are some faults.

Firstly "main" is not a void function. This function should returns integer. Following the standard we have:
 

Quote

C99 5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

 

Secondly, printf is quite a complicated function. An ideal function for printing text is "puts". "puts" for default adds "new line" character at end of the text passed as argument.

 

So your first example should looks like that:

#include  <stdio.h>          /* Header */

int main(void)
{
  puts("Hello world");
  return 0;
} 

I've also added return 0. It is a normal exit from application. Any other value indicates application error.

 

Thirdly. "C" language is case sensitive. Unix systems are also case-sensitive in file names. So there is a difference between "Stdio.h" and "stdio.h". Proper is the last one.

 

Fourthly in your example of program there is #include <conio.h>. This library is avaible only on some platforms. It is not a part of standard libraries. Compilation under linux will fail for example. Instead of using getch() from conio.h better way is to use getchar(). This one is a part of "stdio.h"

 

Please also, describe something more that your application is doing.
We both know that it adds 1 to variable "a" and then subtracts 1 from the result of the last operation. The program result is as follows:

10
11
10

You should also say something more about what "\n" and "%d" are for.
"\n" is the symbol of new line. It is type of encoding "enter/return" key.

"%d" is a keyword for parser in printf function. It describes that here should be printed value from variable passed as next argument to function printf.


Besides, this is a really good example. The language "C" should be widely promoted

 

--
Regards
Karol_Domag

 

Link to comment
Share on other sites

1 hour ago, [C-S] karol_domag said:

Dear Dopaa

 

It is a nice idea to put "Getting start guide" here.

Unfortunatelly There are some faults.

Firstly "main" is not a void function. This function should returns integer. Following the standard we have:
 

 

Secondly, printf is quite a complicated function. An ideal function for printing text is "puts". "puts" for default adds "new line" character at end of the text passed as argument.

 

So your first example should looks like that:


#include  <stdio.h>                /* Header

int main(void)
{
  puts("Hello world");
  return 0;
} 

I've also added return 0. It is a normal exit from application. Any other value indicates application error.

 

Thirdly. "C" language is case sensitive. Unix systems are also case-sensitive in file names. So there is a difference between "Stdio.h" and "stdio.h". Proper is the last one.

 

Fourthly in your example of program there is #include <conio.h>. This library is avaible only on some platforms. It is not a part of standard libraries. Compilation under linux will fail for example. Instead of using getch() from conio.h better way is to use getchar(). This one is a part of "stdio.h"

 

Please also, describe something more that your application is doing.
We both know that it adds 1 to variable "a" and then subtracts 1 from the result of the last operation. The program result is as follows:


10
11
10

You should also say something more about what "\n" and "%d" are for.
"\n" is the symbol of new line. It is type of encoding "enter/return" key.

"%d" is a keyword for parser in printf function. It describes that here should be printed value from variable passed as next argument to function printf.


Besides, this is a really good example. The language "C" should be widely promoted

 

--
Regards
Karol_Domag

 

 

Fixed, thank you. (:

  • Upvote 1

76561198178929243.png

 

Full-Stack Web Developer

Link to comment
Share on other sites

  • 4 years later...

In this example, I can provide a straightforward illustration related to variable types.
 

  • Integer Types: Integer types represent whole numbers and include int, short, long, and unsigned int.
  • Floating-Point Types: Floating-point types, such as float and double, are used for numbers with decimal points.
  • Character Type: The char type is used to store single characters, like letters or symbols.
  • Boolean Type: The _Bool type is used for Boolean values, typically representing true or false.

 

#include <stdio.h>

int main() {
    // Integer Types
    int integerNumber = 10;
    short shortNumber = 5;
    long longNumber = 1000;
    unsigned int unsignedInteger = 20;

    // Floating-Point Types
    float floatingNumber = 3.14;
    double doubleFloating = 2.71828;

    // Characters
    char character = 'A';

    // Boolean Values
    _Bool booleanValue = 1;  // 1 represents true

    // Printing Variables to the Console
    printf("Integer: %d\n", integerNumber);
    printf("Short: %hi\n", shortNumber);
    printf("Long: %ld\n", longNumber);
    printf("Unsigned Integer: %u\n", unsignedInteger);
    printf("Floating-Point: %f\n", floatingNumber);
    printf("Double Floating-Point: %lf\n", doubleFloating);
    printf("Character: %c\n", character);
    printf("Boolean Value: %d\n", booleanValue);

    // Basic Mathematical Operations
    int sum = integerNumber + shortNumber;
    float product = floatingNumber * doubleFloating;

    // Printing Operations to the Console
    printf("Sum: %d\n", sum);
    printf("Product: %f\n", product);

    return 0;
}


 

Karakteriniz düzgün olsun ki gerisini hallederiz..

My first condition is that your character is not broken...

AlparslanK

 

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.