DEV Community

Matembu Emmanuel Dominic
Matembu Emmanuel Dominic

Posted on • Updated on

Basics of C Programming!

There is nothing worth learning like C programming which ideally provides you with the basic knowledge of other programming languages whereas some term it to be the Latin of other programming languages.
The execution of a C program begins from the main() function whereas running from top to bottom. So you can define a program as:

#include <stdio.h>
#include <stdlib.h>
int main(){
}
Enter fullscreen mode Exit fullscreen mode

The first two declared statements with "#include" which is an import statement, importing a standard input-output header file plus the standard library header file, and yet the third statement is the main function declared.
NB: How does a C program work?

  • All valid C programs must contain the main() function.
  • Code execution begins from the start of the main() function. However, we believe that C programming is strongly typed if we try changing the return type of the main function, no error raises because the compiler doesn't treat it as an error since, prior to the 1999 standard, C did permit the return type of a function to be omitted and it would default to int. Then with C++ which is derived from C, so early or pre-standard versions of C++ had the same rule, where omitting the return type is an error.

Comments:

/*
Multiple lines commenting
This allows us to take notes for our program
*/
// single of line commenting

Enter fullscreen mode Exit fullscreen mode

Format Specifiers:

int - represented by %d
char - represented by %c
float - represented by %f
double - represented by %lf
Sample:
int age = 18;
printf("%d", age); // There are many Format Specifiers you can 
use as we are going to see
Enter fullscreen mode Exit fullscreen mode

Escape sequence:

new line - represented by \n
sample:
printf("\nManuel");
printf("Manuel\'s Name");
Enter fullscreen mode Exit fullscreen mode

A sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly. Escape sequences consist of two or more characters, the first of which is the backslash "\"

Printf:

In C programming, printf() is one of the main output functions. The function sends formatted output to the screen.
The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations. To use printf() in our program, we need to include stdio.h header file using the #include statement.

printf("Manuel");
Enter fullscreen mode Exit fullscreen mode

Scanf:

printf("Enter any Number: ");
scanf("%d", &num2);
printf("You enter %d", num2);
Enter fullscreen mode Exit fullscreen mode

NB: In C programming, scanf() is one of the commonly used function to take input from the user. 

  • The scanf() function reads formatted input from the standard input such as keyboards. Notice, that we have used &num2 inside scanf(). It is because &num2 gets the address of num2, thus the value entered by the user is stored in that address.

Variables:

float number1 = 11.6;
 double number2 = 13.8;
 printf("number1 = %f", number1);
 printf("number1 = %.1f", number1); // this prints the variable 
to one decimal place.
 printf("number2 = %lf", number2);
 char letter = 'A';
 printf("%c", letter);
 //The value of a variable can be changed.
 letter = 'K';
 printf("%c", letter);
Enter fullscreen mode Exit fullscreen mode

Rules for naming a variable:
 * A variable name can only have letters, digits, and underscore.
 * The first character of a variable name must be either a letter or an underscore.
 * There is no rule on how long a variable name can be, however, you may run into problems in some compilers if the variable name is longer than 31 characters.

Constant:

const int age = 18;
//Here, age is a constant of int type, here,the constant/variable name is assigned an integer value 18
This will raise an error if we try re-assigning the constant value to 17:
const int age = 18;
age = 17;
printf("%c", age);
Enter fullscreen mode Exit fullscreen mode

Operators:

•Mathematical: +, -, /, *, %
•Assignment: =, and so on
•Relational: >, >=, <, <=, ==, !=
•Conditional: ?:
•Logical: !, &&, ||
•Bitwise: &, |, ^, <<, >>, ~
•Unary: &, *, ++, --, sizeof
Example:
float num3;
double num4;
printf("Enter a number: ");
scanf("%f", &num3);
printf("Enter another number: ");
scanf("%lf", &num4);
printf("num3 + num4 = %lf", num3+num4);
Enter fullscreen mode Exit fullscreen mode

Conditional Statement:

How if statement works?
The if statement evaluates the test expression inside the parenthesis ().
If the test expression is evaluated to be true, statements inside the body of if are executed.
If the test expression is evaluated to false, statements inside the body of if are not executed.

if(age <= 17) {
    printf("You\'re below 18 years!");
}
Enter fullscreen mode Exit fullscreen mode

If the test expression is evaluated to be true, the body print, however, is skipped. But to check for the false, we provide the else.

if(age <= 17) {
    printf("You\'re below 18 years!");
}else {
    printf("You\'re above 18 years!");
}
Enter fullscreen mode Exit fullscreen mode

Loops:

NB: In programming, a loop is used to repeat a block of code until the specified condition is met. however if the test expression is false, the loop terminates (ends). However, the below code indicates curly brackets which are optional but recommendable to define clearly a given block for the loop.
C programming has three types of loops:
for loop.

// for (initializationStatement; testExpression; updateStatement) { body }
int i;
for (i=1; i<11; ++i){
    printf("%d", i);
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

while loop.

//while (testExpression) { body }
int j=7;
while (j>=5) {
    printf("%d", j);
    printf("\n");
    --j;
}
Enter fullscreen mode Exit fullscreen mode

do…while loop.

//do { body }while (testExpression); 
int total=0;
int k;
do {
    printf("Enter a number: ");
    scanf("%lf", &k);
    total += k;
}while(k != 0);
printf("%d", total);
Enter fullscreen mode Exit fullscreen mode

Switch Statement:

printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &sign);
printf("\n");
printf("Enter two operands: ");
scanf("%lf %lf", &num8, &num9);
switch (sign){
    case '+':
        printf("%.1lf + %.1lf = %.1lf",num8, num9, num8+num9);
        break;
    case '-':
        printf("%.1lf - %.1lf = %.1lf",num8, num9, num8-num9);
        break;
    case '*':
        printf("%.1lf * %.1lf = %.1lf",num8, num9, num8*num9);
        break;
    case '/':
        printf("%.1lf / %.1lf = %.1lf",num8, num9, num8/num9);
        break;
    // operator doesn't match any case constant +, -, *, /
    default:
        printf("Error! operator is not correct");
}
Enter fullscreen mode Exit fullscreen mode

Functions:

void checkEvenNumber(){
    int num;
    printf("Enter any Number: ");
    scanf("%d", &num);
    if(num%2 == 0) {
        printf("%d is an Even Number", num);
    }else {
        printf("%d is an Odd Number", num);
    }
}
int main(){
    checkEvenNumber();
}
Enter fullscreen mode Exit fullscreen mode

When the compiler encounters checkEvenNumber();, control of the program jumps to And, the compiler starts executing the codes inside checkEvenNumber().
The control of the program jumps back to the main() function once the code inside the function definition is executed.

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

Your first program doesn't need either #include statement since it does nothing. (Most "first programs" print "hello, world" which yours doesn't.)

Most of your printf examples are somewhat wrong since they don't include the often-wanted \n at the end.

You wrote:

The first letter of a variable should be either a letter or an underscore.

No; the first character of a variable name must be either a letter or underscore.

You are missing several operators, e.g., ! for logical. If you're doing a "basics" article, your audience won't have any idea what "and so on" means for assignment operators.

You don't mention that the expressions for the for loop are optional. None of the loops require the use of {}.

Collapse
 
emmanueldominic profile image
Matembu Emmanuel Dominic • Edited

Sure, thanks for the feedback and about the \n on printf, I was simply adding a new line at the printf statement whereas {} on a loop, I have indicated it being optional however recommendable for beginners/programmers to use curly brackets to simpliy define a given block of code on the loop. Otherwise thanks once again for the point of correction.