C programming

Mahi!
5 min readDec 3, 2019

--

Compiler:

Download Dev Cpp

Book:

The C programming language — Brian W. Kernighan & Dennis M. Ritchie

To search explanation of anything use ctrl+f then %keyword-keyword% so you don’t have to scroll and find it manually. If it is explained below it’ll be there. try searching for hello world by using %hello-world%.

%Hello-World%

#include <stdio.h>
main(){
printf("Hello, World!\n");
}

Running a C program:

  • we first create a text file that ends with extension .c like hello.c
  • then we compile the text file using a compiler (which includes the linker as well) which gives a .obj file like a.obj the obj file has the higher level text converted to machine language.
  • then we run the a.obj file and it gives the output

General terminology

%variable%

A pointer(reference) to memory location which can hold a value. In other words it is a human readable name for some value that we store like a number, string, array etc. and later use in the program.

%arguments% %parameters%

They are the variables or values that are used to pass data to a function. they are passed inside the parenthesis () while calling a function. empty parenthesis ()or (void) means no parameters are passed.

%void%

void is a type which means nothing in simple terms. We can’t declare a variable of void type but void can be a return type where the function doesn’t return anything and also we can use it when a function doesn’t accept any parameters.

void main(void){}

%statement%

Defines a single/more operations in the program such as declaring a variable or assignment or doing some math or printing something or taking input and storing in some variable. It ends with a semi-colon;

void main(void){
int x;
x++;
}

%function%

A function contains statement that specify computing operations. They are group/block of statements that do some specific task. It is useful if we have to do same thing again and again then we can call a function instead of writing all the statements again. The statements of a function are enclosed in braces {...}

%main()%

Starting point of execution of a program. You must have a main function in your program otherwise it won’t run.

%data-types%

data types tell what kind of value is stored by a variable. Once declared the type of the variable can’t be changed in C. The primitive data types are-

but most important that are used frequently are-

  • char — 8 bits(1 byte)
  • int — 32 bits(4 byte)
  • float — 32 bits(4 byte)
  • double — 64 bits(8 byte)

The data types tell how much memory should be reserved for the particular variable. We can use these primitive data types to build other larger data types.

%declaration%

A declaration is telling the program about a variable that you will use and the type of that variable. Declaration announces the properties of a variable and consists of type name(data type ) of the variable and the identifier name of the variable.

// Example:
int x; // int is the type name, x is the identifier name.

%#define%

It is a pre-processor directive which is used to define macros. #define is used to define constants in your program which cannot be changed later like a variable. The constants can be numbers, strings or expressions.

#define CONSTNAME value
#define CNAME (expression)

They don’t have a semi-colon ‘ ; ’ at the end. and are usually written after the #include lines.

// Examples:#define AGE 10 
#define AGE (20 / 2)
#define STRNAME "One String"

%Prototype%

A prototype is a function declaration without any body. i.e. only the return type, name of the function and parameters are declared and the definition is written elsewhere. Usually we write the prototype before the main() function and define the function after the main() because the program is executed from top to bottom so the main() should know about the function that is being called. if it is below main() then it can’t be called from inside main() as the main() function doesn’t know about it yet. It is similar to declaring a variable first and assigning value later.

void myAge(int x);int main(void)
{
myAge(12);
}
void myAge(int x)
{
printf("my age is %d ",x)
}

Loops

%while%

while loop executes all the statements inside until a given condition is true.

while(CONDITION){
//statements here
}

CONDITION can be a Boolean expression, i.e. the output of condition is either TRUE or FALSE.

If there is only one statement inside while loop then braces become optional.

Library files related terminology

%#include%

It is used to include any library files that are provided by C. They contain already written code and you call their functions that do standard tasks like taking input, printing output etc.

// Example -> #include <stdio.h>

%stdio%

This is a library file that includes the information about standard input and output that we use in our program like printf() and scanf()

%printf()%

A library function that takes a string as an argument and prints it on the screen.

printf("Hello, World! ");// Output ->
Hello, World!

To print multiple values(variables) with the string we pass the parameters to the printf() function along with the string. The string itself contains the format of the variables passed. ex- %d for int, %c for char.

int x=4;
printf("I am no %d ", x);
// Output ->
I am no 4

We can pass any no of variable after the string, they should be formatted properly according to their places.

To give a particular width to a floating point no or any other we add the size after the %.

float pi=3.1415;
printf("I am no '%6.3f' ", x);
// Output ->
I am no ' 3.141'

notice 2 spaces between the single quote and 3.141, also the 4 digits after decimal are truncated to 3.

The format %6.3f means that the total width of the variable to be printed will be at least 6 and out of which 3 digits will be after the deciaml point.

%getchar()%

It is used to get one character as an input. The function takes input in form of streams of characters. The characters are provided using a keyboard or a file.

c = getchar(); // reads one character

Output can be printed using printf or putchar

%putchar()%

prints a character to the screen.

putchar(c);

Special Characters

%\% — ( \ ) aka Escape character

It is called Escape character and tells the program that any character immediately after it has a special meaning.

%\n% — ( \n )

Used to print a one line space on the output file. It does the same thing as Enter Key on your keyboard. It is called the new line character

printf("google\ndd");// Output ->
google
dd

%\t% — ( \t )

gives some indentation to the characters that follow. Usually the indentation is four spaces.

printf("google\tdd");// Output ->
google dd

%\b% — ( \b )

to delete the last character of the output string after which the character(\b) is written.

printf("google\bdd ");// Output -> googldd    //Notice e is deleted

%\”% — ( \” )

To add a quote inside a string.

printf("the quote is \" this character");// Output -> the quote is " this character

%\\% — ( \\ )

Prints a single \

printf("the next character is \\ this character");// Output -> the next character is \ this character

%comments%

%/*-*/% Multiple line comments

/* This is a 
multi-line comment */

%//% Single line comments

// This is a single line comment

--

--