Tag Archives: char

C Programming Tutorial – 12 – change the property of a variable:

Published by:

Note:

  • //Use (float) or (int) or … before any variable to change that variable to float or int or …:

Code:
—————————

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

int main()
{

int PriceOfPopcorn=100,sales=20,WorkDay=7;
float profitPerday;
profitPerday = ((float)PriceOfPopcorn*(float)sales)/(float)WorkDay;
printf(“profit per day is = $%.2f\n”,profitPerday);

return 0;

}

C Programming Tutorial – 8 – Get data from keyboard scanf():

Published by:

Note:

  • // scanf() :  get data from keyboard or user.
  • // Use & before all of the variables, except Array variables.

Code:
—————————

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

int main()
{

char firstname[20];
char lovename[20];
int NumberOfBaby;
printf(“What is your name?\a\n”);
scanf(“%s”,firstname);                                                           // scanf get data from keyboard or user
printf(“What is your love name?\a\n”);
scanf(“%s”,lovename);
printf(“How many baby you will have?\a\n”);
scanf(“%d”, &NumberOfBaby);                     // Use & before all of the variables except Array variables.
printf(“%s and %s are in love. They will have %d babies\n”, firstname,lovename ,NumberOfBaby );

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;

}