Category Archives: C programming

C programming

C Programming Tutorial – 7 – Generate a header file and use it:

Published by:

Note:

  • //make sajjadinfo.h header file and use content of code 1.
  • add #include “sajjadinfo.h”    to your code.
  • “” means just look at current folder.

 

Code 1:    sajjadinfo.h
—————————

#define MYNAME “SB”
#define AGE 33

 

Code 2:
—————————

#include <stdio.h>
#include <stdlib.h>

#include “sajjadinfo.h”     // we create a Header file . “” means just look at current folder.

int main()
{

int HalfOfMyAge=(AGE/2) ;                              // we used sajjadinfo.h file constant data (AGE)
printf(“Half of age of %s is %d \n”, MYNAME , HalfOfMyAge);  // we used sajjadinfo.h file constant data (MYNAME)

return 0;

}

C Programming Tutorial – 6 – replace a string with other sting – strcpy():

Published by:

Note:

  • // replace a string with other sting:
    strcpy(food,”bacon”);

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char food[]=”tuna”;
printf(“The best food is %s\n”, food);
strcpy(food,”bacon”);                 // replace a string with other sting
printf(“The best food is %s\n”, food);

return 0;

}

C Programming Tutorial – 5 – Define an Array + replace a character in an array :

Published by:

Note:

  • //Define array:
    char name[15]= “Sajjad Baghaee”;
  • // replacing a character in an array : 
    name[2]= ‘C’;
  • Array start by 0 so S=0 a=1  j=2  j=3  a =4  d=5 =6  B=7  a=8  g=9  h=10  a=11  e=12  e=13

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char name[15]= “Sajjad Baghaee”;
printf(“This is %s \n”, name);
name[2]= ‘C’;                  // replacing the 1st character “j” in array “Sajjad Baghaee”  with “C”
name[3]=’c’;                   // replacing the 2nd character “j” in array “Sajjad Baghaee”  with “c”
printf(“This is %s\n”,name);

return 0;

}

C Programming Tutorial – 3 – what can we do with “printf()” ? :

Published by:

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

  • // Print “Hello world!” on the screen + goes to new line
    printf(“Hello world!\n”);

 

  • // Print “Hi Sajjad” on the screen + insert a tab space
    printf(“Hi Sajjad\t”);

 

  •  // Print “This is a tab test” on the screen + goes to new line + make a alarm sound
    printf(“This is a tab test\n\a”);

 

  • // Conversion string use %s to replace a string
    printf(“%s is an intelligent boy and %s\n” , “SAJJAD”,”programmer”);

 

  • // Conversion integer use %d to replace a integer number
    printf(“I ate %d eggs last morning\n”,2);

 

  •  // Conversion decimal use %f to replace a decimal number
    printf(“my weight is %f\n”, 80.467896212);

 

  •  // Conversion decimal use %.4f to replace a decimal number with 4 decimal number
    printf(“my weight is %.4f\n”, 80.467896212);

 

  • // Conversion decimal use %.2f to replace a decimal number with 2 decimal number
    printf(“my weight is %.2f\n”, 80.467896212);

return 0;

}

C Programming Tutorial – 2 – Hello World !

Published by:

1- Open Code::Blocks.

2- In left column, expand your “project” (e.g. learningProject) , go to “Sources” folder and click on “main.c”.

you will see some thing like this:

C Programming Tutorial – 1

if “main.c” is empty or contains other code, use following code.

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf(“Hello World!\n”);
return 0;
}

3- click on run as follow.

Run

4- You will see following result.

Hello world ! result

C Programming Tutorial – 1 – Introduction

Published by:

What is C (Programming Language)? 

C is an imperative (procedural) language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. Therefore, C was useful for many applications that had formerly been coded in assembly language, for example in system programming.

Despite its low-level capabilities, the language was designed to encourage cross-platform programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with few changes to its source code. The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers. [Wikipedia].


Setting Up Code Blocks

1- Download and install Code::Blocks on your compute (click here).

Code::Blocks is a free C, C++ and Fortran IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable.
Finally, an IDE with all the features you need, having a consistent look, feel and operation across platforms.
Built around a plugin framework, Code::Blocks can be extended with plugins. Any kind of functionality can be added by installing/coding a plugin. For instance, compiling and debugging functionality is already provided by plugins![codeblocks].

2- Create a folder. Your codes/projects will be store in this folder.

3- Open the Code Blocks software.

4- Go to File>File>Project.   (a new window opens up)

5- Select “Console application”  and click “GO” then “NEXT“.

6- Select “C” and click “NEXT“.

7- Give a title to your project. (e.g. learningProject).

8- Brows and select your folder which you created in step 2. and click “NEXT“.

9- In the next window do not change any thing. Just click on “Finish“.

That’s it. Your IDE is ready for writing your code.