c programming

C-Programming

Introduction to C

C is probably the most widely known programming language. Its the primary language for
computer science courses all over the world, and it’s probably the language that people learn the most in
school among with Python and Java.

C is not just what students use to learn programming. It’s not an academic language. And I would say it’s not the easiest language, because C is a rather low level programming language.

Today, C is widely used in embedded devices, and it powers most of the Internet servers,. We can say that C code runs a good portion of the entire world. Right now. Pretty remarkable.

When C was created, it was considered a high level language, because it was portable across machines.
Today we kind of give for granted that we can run a program written on a Mac on Windows or Linux,
perhaps using Node.js or Python. Once upon a time, this was not the case at all. What C brought to the
table was a language simple to implement, having a compiler that could be portable to different
machines.

I said compiler: C is a compiled programming language, like Go, Java, Swift or Rust. Other popular programming language like Python, Ruby or JavaScript are based on interpreter. The difference is consistent: a compiled language generates a binary file that can be directly executed and distributed.

C is not garbage collected. This means we have to manage memory ourselves. It’s a complex task and one that requires a lot of attention to prevent bugs, but it is also what makes C ideal to write programs for embedded devices like Arduino. C does not hide the complexity and the capabilities of the machine underneath. You have a lot of power,
once you know what you can do. I want to introduce the first C program now, which we’ll call “Hello, World!”

Hello World

In order to create a simple program which prints “Hello World”, which is the favorite program of the beginners use any text editor to create a file (hello.c). Well, the extension should be ‘.c’ always. Please keep that in mind

hello.c

#include <stdio.h>

int main(void)
{
    puts("hello world");
    return 0;
}
Lets break this program into smaller part to see what it is about

This line tells the compiler to include the contents of the standard library header file (always has the extensions .h) in the program. Headers are usually the files that contains the function declarations, macros and data types. This is the standard header always kept in a program so that we can access the puts function which we will discuss later.

This line starts the definition of a function. It states the name of the function (main), the type and number of arguments it expects (void, meaning none), and the type of value that this function returns (int). Program
execution starts in the main() function.

The curly braces are used in pairs to indicate where a block of code begins and ends. They can be used in a lot of ways, but in this case they indicate where the function begins and ends.

This line calls the puts() function to output text to standard output (the screen, by default), followed by a newline. The string to be output is included within the parentheses.

“Hello, World” is the string that will be written to the screen. In C, every string literal value must be inside the double quotes “…”.
In C programs, every statement needs to be terminated by a semi-colon (i.e. ;).

When we defined main(), we declared it as a function returning an int, meaning it needs to return an integer. In this example, we are returning the integer value 0, which is used to indicate that the program exited successfully. After the return 0; statement, the execution process will terminate

Original “Hello, World”

The following is the original “Hello, World!” program from the book The C Programming Language by Brian
Kernighan and Dennis Ritchie (Ritchie was the original developer of the C programming language at Bell Labs),
referred to as “K&R”

#include <stdio.h>
main()
{
     printf("hello, world\n");
}

Comments, Why we need them?

Any code of your own that you haven’t looked at for six or more months might as well have been written by someone else.

So if you are not doing some comments you are not a good progammer. So How do you do some awesome comment? Start with simple things. Basically its for you so that you don’t have to look after the code again and again. you may write anything in comment. Don’t worry it wont compile at all.

Commenting using the preprocessor

Well we don’t do it often since you are a beginner. Although if you want to know about it, here is the details. Large chunks of code can also be “commented out” using the preprocessor directives #if 0 and #endif. This is useful when the code contains multi-line comments that otherwise would not nest.

#if 0 /* Starts the "comment", anything from here on is removed by preprocessor */
/* A large amount of code with multi-line comments */ 
int foo()
{
  /* your favorite code */
  ...
  /* ... some comments the if statement ... */
  if (someTest) {
  /* some more useful / useless comments :D */
  return 1;
  }
  return 0;
}
#endif /* 0 */
/* code from here on is "uncommented" (included in compiled executable) *
Delimited comments /* */

So if you want a multi-line comment and you don’t want to use above one that’s fine. You can just use anything on your code in between /* and */.

/* this is some awesome comment
i need to do something here
keeping this comment for Mr. B to read it out */
Delimited comments //

Sometimes you want to use a single line comment and below can explain you how to use it properly. you can use this comments in a single line with code at first. Generally we do this a lot and always remember that we keep // to use a comment. Believe me I do often use this to comment the code that i don’t use. Also sometimes I use //todo so that I add some code later in future.

//some comment goes here
if(x==0) // i have kept this comment for no reason but you can put here anything
{
   //todo: some works to do by Mr. X
}

Operators

Relational operators

Generally, we all know about Relational Operator. It basically checks the relation between the two operands and returns t he values in the form of boolean (0 or 1); 0 for false and 1 for true, as simple as it is. Lets talk about the types of the relation that we face in programming

Equals “==” : Checks if the value are equal or not; returns 0 if not equal and returns 1 if equal
int a = 5;
int b = 5;
(a - b) == 0; //returns true
(a - b) == 5; // returns false 
Equals “!=” : Checks if the value are not equal or are equal; returns 1 if not equal and returns 0 if equal
int a = 5;
int b = 5;
(a - b) != 0; //returns false
(a - b) != 5; // returns true 
Not “!” : Often, we use this to check if the value of variable is equal to 0
!someValue // this actually represents (someValue == 0) 
Greater than “>” : we user this to check of the left side is greater than the right side
int a = 5;
int b = 10;
a > b; // this will return false 
b > a; // this will return true

Conditional operator / Ternary operator

Alternative to If then else with only 1 statements for the conditions.

Bitwise operator

Short circuit behavior of logical operator

Comma operator

Arithmetic operator

Access operator

sizeof operator

Cast operator

Function call operator

Increment / Decrement

Assignment operator

Logical operator

Pointer Arithmetic

_alignof

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site
%d bloggers like this: