Tuesday, January 28, 2014

C programming




Lesson 2


Welcome back on learn 4 free programming!

Today we will learn how to get data from user and print it on screen.
Like last time we will open and save program (for example "lesson2.c"), remember to put .c on the end okay.

  1. So next thing you need to do is to include header files (for this lesson we will use stdio.h), like in the previous lesson.
  2. Open the starting "block" of program (int main() { and close it } ).
  3. Now type "char name;" (char is used for typing character-text, name is just a variable you can rename it in what ever you want).
  4.  Now let's ask user to type his name (printf("What is your name? : "). With this we are asking user to type his name.
  5. When user typed his name we have to "save" his name in variable that we define on the beginning. For first time we are using function "scanf" (scanf("%c", &name)).
  6. Now let's print user name on his screen (printf("\n Hello %c, welcome to C programming", name);)


example:

#include <stdio.h>

int main(){
     char name;

     printf("What is your name? :");   //asking user for his name
     scanf("%c", &name);   // adding user name to variable name
     printf("\n Hello %c, welcome to C programming", name);    // printing user name to screen

    getch();
}


// - This is used for writing comments in source code.

Okay that's it for today come back to learn more about C. I started this blog several days ago so i will be posting new lessons every day or two. See you next time :).

If something doesn't work you can email me on: sashans89@gmail.com and i will help you

No comments:

Post a Comment