Hello today I will explain to you guys a few things about coding and how to think when you want to learn a language like C or C++!

What you want to do is not very easy so it need some practice:

  1. Do Little scripts every day to remember the language like if you spoke with someone and test it with the script emulators.
  2. Always try to learn your commands in groups or blocks.

you can also try all these methods:

1. Look at the Example Code

Reading is usually about the words on the page, but learning to program is about code. When you’re first learning to program, you should make sure to look at, and try to understand, every example. When I first learned to program, I would sometimes read the code examples before the text, and try to figure out what they did. It doesn’t always work, but it did force me to look at the example very carefully, and it often helped make the writeups clearer.

If you want to see what sample code looks like, you can read this site’s introductory programming tutorial. This tutorial spends a great deal of time talking about the sample code to help you work through exactly what the code does.
2. Don’t Just Read Example Code–Run It

But when you’re reading a programming tutorial (or book), it’s easy to look at the sample code and say “I get it, I get it, that makes sense”. Of course, you might get it, but you might not get it, and you just don’t know it. There’s only one way to find out–do something with that code.

If you haven’t already, get a compiler like Code::Blocks set up.

Then type the sample code into a compiler–if you type it, instead of copying and pasting it, you will really force yourself to go through everything that is there. Typing the code will force you to pay attention to the details of the syntax of the language–things like those funny semicolons that seem to go after every line.

Then compile it and run it. Make sure it does what you think it does.

Then change it. Software is the most easily changed machinery on the planet. You can experiment easily, try new things, see what happens; the changes will happen almost immediately, and there is no risk of death or mayhem. The easiest way to learn new language features is to take some code that works one way, and change it.
3. Write your Own Code as Soon as Possible

Once you understand something about the language–or even if you’re still getting your head around it–start writing sample programs that use it. Sometimes it’s hard to find good ideas for what programs to write. That’s OK, you don’t have to come up with every idea at the beginning.

You can find some programming challenges on this site.

You can also reimplement the examples from the book or tutorial you are reading. Try to do so without looking back at the sample code; it won’t be as easy as it seems. This technique can work especially well if you tweak the sample code.

If you can’t think of a small program to write, but you have in mind a larger program you want to implement, like a game, you could start building small pieces that you can later use for a game. Whether you use them later or not, you will get the same useful experience.
4. Learn to Use a Debugger

I already talked about the importance of debugging in The 5 Most Common Problems New Programmers Face–And How You Can Solve Them. But it bears repeating; the sooner you learn good debugging techniques, easier it will be to learn to program.

The first step in doing so is to learn how to use a tool called a debugger, which allows you to step through your code.

A debugger will allow you to step line by line through a piece of code. It will let you see the values of variables, and whether the code inside an if statement is executed.

A debugger can help you quickly answer questions about what your code is doing:
int main()
{
 int x;
 int y;
 if( x > 4 ) // <-- what is the value of x here?
 {
 y = 5; // <-- did this line of code execute?
 }
}

A final word about debuggers: the first time you learn about a debugger, it will take you longer to fix the problems with your code. After the tenth or so bug, it will really start to pay off. And believe me, you will have way more than ten bugs in your programming career.

I often saw students unwilling to use a debugger. These students really made life hard on themselves, taking ages to find very simple bugs. The sooner you learn to use a debugger, the sooner it will pay off.
5. Seek out More Sources

If you don’t understand something, there’s a good possibility the way it was explained just didn’t click.

First, look for alternative explanations. The internet is filled with information about programming, and some explanations work better for different people; you might need pictures, someone else might not. There are also lots of good books with detailed explanations.

But if that doesn’t work, the easiest way to figure out where your misunderstanding lies is to ask someone else. But try to go beyond saying, “I don’t understand. Please explain.” You’re likely to get a link back to the same text you didn’t understand. Instead, rephrase your understanding of the text in your words. The more your question reveals about what you are thinking, the easier it will be for a knowledgeable expert to answer it. Programmers sometimes have a reputation for being grumpy about answering questions, but I think the reason is that they want to make progress in a conversation, and that requires both sides to put in effort. If you ask a smart, detailed question that shows you are thinking, you will generally get good results.

There are plenty of places you can go to ask questions. You can always email me, or post on our message board, or ask an expert.

In the coming days, I’ll be writing more about how to learn to program effectively. Stay tuned, by subscribing to our RSS feed, signing up for email notifications, or following @alexallain on twitter.

INFORMATION:

C is one of the older programming languages. It was developed in the 70s, but it is still very powerful thanks to how low-level it is. Learning C is a great way to introduce yourself to more complex languages as well, and the knowledge you gain will be useful in almost every programming language and can help you get into app development. To learn how to start programming in C, see Step 1 below.

Step 1

Download and install a compiler. C code needs to be compiled by a program that interprets the code into signals that the machine can understand. Compilers are usually free, and different compilers are available for different operating systems.
For Windows, try Microsoft Visual Studio Express or MinGW.
For Mac, XCode is one of the best C compilers.
For Linux, gcc is one of the most popular options.

Step 2

Understand the basics. C is one of the older programming languages, and can be very powerful. It was designed for Unix operating systems, but has been ported and expanded for nearly all operating systems. The modern version of C is C++.
C is essentially comprised of functions, and in these functions you can use variables, conditional statements, loops to store and manipulate data.

Step 3:

Examine some basic code. Take a look at the (very) basic program below to get a good idea about how some of the various aspects of the language work together, and to get an idea of how programs function.

#include <stdio.h>
 
int main() 
{
 printf("Hello, World!n");
 getchar();
 return 0;
}
[1]

The #include command occurs before the program starts, and loads libraries that contain the functions you need. In this example, stdio.h lets us use the printf() and getchar() functions.
The int main() command tells the compiler that the program is running the function called “main” and that it will return an integer when it is finished. All C programs run a “main” function.
The {} indicate that everything inside them is part of the function. In this case, they denote that everything inside is a part of the “main” function.
The printf() function displays the contents of the parentheses on the user’s screen. The quotes ensure that the string inside is printed literally. The n sequence tells the compiler to move the cursor to the next line.
The ; denotes the end of a line. Most lines of C code need to end with a semicolon.
The getchar() command tells the compiler to wait for a keystroke input before moving on. This is useful because many compilers will run the program and immediately close the window. This keeps the program from finishing until a key is pressed.
The return 0 command indicates the end of the function. Note how the “main” function is an int function. This means that it will need an integer to be returned once the program is finished. A “0” indicates that the program has performed correctly; any other number will mean that the program ran into an error.

Step 4:

Try compiling the program. Enter the code into your code editor and save it as a “*.c” file. Compile it in your compiler, typically by clicking the Build or Run button.

Step 5:

Always comment on your code. Comments are part of the code that is not compiled, but allows you to explain what is happening. This is useful for reminding yourself what your code is for, and for helping other developers who might be looking at your code.
To comment in C place /* at the start of the comment and */ at the end.
Comment on all but the most basic parts of your code.
Comments can be used to quickly remove parts of your code without deleting them. Simply enclose the code you want to exclude with comment tags and then compile. If you want to add the code back, remove the tags.

Step 6:

Understand the function of variables. Variables allow you to store data, either from computations in the program or from user input. Variables need to be defined before you can use them, and there are several types to choose from.
Some of the more common variables include int, char, and float. Each one stores a different type of data.

Step 7:

Learn how variables are declared. Variables need to be established, or “declared”, before they can be used by the program. You declare a variable by entering the data type followed by the variable’s name. For example, the following are all valid variable declarations:

float x;
char name;
int a, b, c, d;

Note that you can declare multiple variables on the same line, as long as they are the same type. Simply separate the variable names with commas.
Like many lines in C, each variable declaration line needs to end with a semicolon.

Step 8:

Know where to declare variables. Variables must be declared at the beginning of each code block (The parts of your code that are enclosed in {} brackets). If you try to declare a variable later in the block, the program will not function correctly.

Step 9:

Use variables to store user input. Now that you know the basics of how variables work, you can write a simple program that will store the user’s input. You will be using another function in the program, called scanf. This function searches the input that is provided for specific values.

#include <stdio.h>
 
int main()
{
 int x;
 
 printf( "Enter a number: " );
 scanf( "%d", &x );
 printf( "You entered %d", x );
 getchar();
 return 0;
}

The “%d” string tells scanf to look for integers in the user input.
The & before the variable x tells scanf where to find the variable in order to change it, and stores the integer in the variable.
The final printf command reads back the input integer to the user.

Step 10:

Manipulate your variables. You can use mathematical expressions to manipulate the data that you have stored in your variables. The most important distinction to remember for mathematical expressions is that a single = sets the value of the variable, while == compares the values on either side to see if they are equal.

x = 3 * 4; /* sets "x" to 3 * 4, or 12 */
x = x + 3; /* adds 3 to the original value of "x", and sets the new value as the variable */
x == 15; /* checks to see if "x" equals 15 */
x < 10; /* checks if the value of "x" is less than 10 */

Step 11:

Understand the basics of conditional statements. Conditional statements are what drive most programs. They are statements that are determined to be either TRUE or FALSE, and then acted upon based on the result. The most basic of the statements is the if statement.
TRUE and FALSE work differently in C than what you might be used to. TRUE statements always end up equaling a nonzero number. When you perform comparisons, if the result is TRUE then a “1” is returned. If the result is FALSE, then a “0” is returned. Understanding this will help you see how IF statements are processed.

Step 12:

Learn the basic conditional operators. Conditional statements revolve around the use of mathematical operators that compare values. The following list contains the most commonly used conditional operators.

> /* greater than */
< /* less than */
>= /* greater than or equal to */
<= /* less than or equal to */
== /* equal to */
!= /* not equal to */
10 > 5 TRUE
6 < 15 TRUE
8 >= 8 TRUE
4 <= 8 TRUE
3 == 3 TRUE
4 != 5 TRUE

The code:

Introduction
The C programming language is a general purpose programming language, which relates closely to the way machines work. Understanding how computer memory works is an important aspect of the C programming language. Although C can be considered as “hard to learn”, C is in fact a very simple language, with very powerful capabilities.

C is a very common language, and it is the language of many applications such as Windows, the Python interpreter, Git, and many many more.

C is a compiled language – which means that in order to run it, the compiler (for example, GCC or Visual Studio) must take the code that we wrote, process it, and then create an executable file. This file can then be executed, and will do what we intended for the program to do.

Our first program
Every C program uses libraries, which give the ability to execute necessary functions. For example, the most basic function called printf, which prints to the screen, is defined in the stdio.h header file.

To add the ability to run the printf command to our program, we must add following include derivative to our first line of the code:

#include <stdio.h>
Execute Code
The second part of the code is the actual code which we are going to write. The first code which will run will always reside in the main function.
int main() {
 ... our code goes here
}
Execute Code
The int keyword indicates that the function main will return an integer - a simple number. The number which will be returned by the function indicates whether the program that we wrote worked correctly. If we want to say that our code was run successfully, we will return the number 0. A number greater than 0 will mean that the program that we wrote failed.

For this tutorial, we will return 0 to indicate that our program was successful:

return 0;
Execute Code
Notice that every line in C must end with a semicolon, so that the compiler knows that a new line has started.

Last but not least, we will need to call the function printf to print our sentence.

Variables and Types
Data types
C has several types of variables, but there are a few basic types:

Integers – whole numbers which can be both positive and negative. Defined using char, int, short, long or long long.
Unsigned integers – whole numbers which can only be positive. Defined using unsigned char, unsigned int, unsigned short, unsigned long or unsigned long long.
Floating point numbers – real numbers (numbers with fractions). Defined using float and double.
Structures – will be explained later, in the Structures section.
The different types of variables define their bounds. A char can range only from -128 to 127, whereas a long can range from -2,147,483,648 to 2,147,483,647.

Note that C does not have a boolean type. Usually, it is defined using the following notation:

#define BOOL char
#define FALSE 0
#define TRUE 1
Execute Code
C uses arrays of characters to define strings, and will be explained in the Strings section.

Defining variables
For numbers, we will usually use the type int, which an integer in the size of a “word” the default number size of the machine which your program is compiled on. On most computers today, it is a 32-bit number, which means the number can range from -2,147,483,648 to 2,147,483,647 (same as long).

To define the variables foo and bar, we need to use the following syntax:

int foo;
int bar = 1;
Execute Code
The variable foo can be used, but since we did not initialize it, we don't know what's in it. The variable bar contains the number 1.

Now, we can do some math. Assuming a, b, c, d, and e are variables, we can simply use plus, minus and multiplication operators in the following notation, and assign a new value to a:

int a = 0,b = 1,c = 2,d = 3, e = 4;
a = b - c + d * e;
printf("%d", a); /* will print 1-2+3*4 = 11 */

Strings
Defining strings
Strings in C are actually arrays of characters. Although pointers in C is in advanced subject explained later on, we will use pointers to a character array to define simple strings, in the following manner:

char * name = "John Smith";
 Execute Code

This method creates a string which we can only use for reading. If we wish to define a string which can be manipulated, we will need to define it as a local character array:

char name[] = "John Smith";
 Execute Code

This notation is different because it allocates an array variable so we can manipulate it. The empty brackets notation [] tells the compiler to calculate the size of the array automatically. This is in fact the same as allocating it explicitly, adding one to the length of the string:

char name[] = “John Smith”;
/* is the same as */
char name[11] = “John Smith”;
Execute Code
The reason that we need to add one, although the string John Smith is exactly 10 characters long, is for the string termination, a special character (equal to 0) which indicates the end of the string. The end of the string is marked because the program does not know the length of the string – only the compiler knows it according to the code.


String formatting with printf
We can use the printf command to format a string together with other strings, in the following manner:

char * name = "John Smith";
 int age = 27;
/* prints out 'John Smith is 27 years old.' */
 printf("%s is %d years old.n", name, age);
 Execute Code
 Notice that when printing strings, we must add a newline (n) character so that our next printf statement will print in a new line.

String comparison
The function strncmp compares between two strings, returning the number 0 if they are equal, or a different number if they are different. The arguments are the two strings to be compared, and the maximum comparison length. There is also an unsafe version of this function called strcmp, but it is not recommended to use it. For example:

char * name = "John";
if (strncmp(name, "John", 4) == 0) {
 printf("Hello, John!n");
} else {
 printf("You are not John. Go away.n");
}

For loops
For loops in C are straightforward. They supply the ability to create a loop – a code block that runs multiple times. For loops require an iterator variable, usually notated as i.

For loops give the following functionality:

Initialize the iterator variable using an initial value
Check if the iterator has reached its final value
Increases the iterator
For example, if we wish to iterate on a block for 10 times, we write:

int i;
 for (i = 0; i < 10; i++) {
 printf("%dn", i);
 }
 Execute Code

This block will print the numbers 0 through 9 (10 numbers in total).

For loops can iterate on array values. For example, if we would want to sum all the values of an array, we would use the iterator i as an the array index:

int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
int i;
for (i = 0; i < 10; i++) {
 sum += array[i];
}
/* sum now contains a[0] + a[1] + ... + a[9] */
printf("Sum of the array is %dn", sum);

While loops
While loops are similar to for loops, but have less functionality. A while loop continues executing the while block as long as the condition in the while holds. For example, the following code will execute exactly ten times:

int n = 0;
while (n < 10) {
 n++;
}
Execute Code
While loops can also execute infinitely if a condition is given which always evaluates as true (non-zero):
while (1) {
 /* do something */
}
Execute Code
Loop directives
There are two important loop directives that are used in conjunction with all loop types in C - the break and continue directives.

The break directive halts a loop after ten loops, even though the while loop never finishes:

int n = 0;
while (1) {
 n++;
 if (n == 10) {
 break;
 }
}
Execute Code
In the following code, the continue directive causes the printf command to be skipped, so that only even numbers are printed out:
int n = 0;
while (n < 10) {
 n++;
/* check that n is odd */
 if (n % 2 == 1) {
 /* go back to the start of the while block */
 continue;
 }
/* we reach this code only if n is even */
 printf("The number %d is even.n", n);
}

More Is Coming soon!

Don’t forget to comment!

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.