C is an ancient procedural Programming language developed in 1972 by Dennis Ritchie at AT&T’S Bell Laboratories in the United States.

it is an excellent and beginner-friendly programming language.

C Became Popular Because it is Easy to Use.

It Has Been Used to Develop Operating Systems, Databases, Applications, etc;

C is Strongly Associated with the Unix Operating system. As it was Developed to Write the Code of the Unix Operating System.

It is Very Fast Compared to other Programming Languages.

Why learn C Programming?

C is a Very Versatile Programming Language.

It is one of the Most Popular Programming Languages in the World.

It is Suitable for Both Applications and Technologies.

C Programming is one of the Fastest Programming languages.

C is Used to Build the Majority of Operating Systems.

First C Program

#include < stdio.h >

int main()
{
printf("Hello World");
return 0;
}

Working flow of a C Program

In accordance with the first C program discussed, let’s now learn about its execution or working flow.

C Programming Basics for Beginners in 2023

Any C program’s execution begins with the main function.

main() Function

int main()
{
printf("Hello Skills Ever");
return 0;
}

  • A Valid C Program must contain the main() function.
  • The C Compiler starts Executing Code from this Function.
  • The main() function as the starting point of program execution.

printf() Function

printf("Hello Skills Ever");

  • This Function displays the text inside Quotations.
  • it is always present in the main() Function.
  • they are used to Print or Display any Text on the Output Screen.

return 0 Function

return 0;
  • The C Program ends with this Statement.

What is #include< stdio.h >

stdio.h means Standard Input Output.

It is a header file that contains the definitions of functions that are only known to the compiler, such as printf and scanf.

Our Program needs this Header file to include the definition of the input/output-related function.

For example; printf, scanf, etc.

Whenever we want to use the printf or scanf function in our Program, we should include the stdio.h header file in our source code.

If we do not include the stdio.h header file in our source code, Compiler does not know the Definition of printf & scanf and will return an Error.

Why do We use #include?

#include is a C preprocessor directory.

This keyword instructs our programs to include (or import) a header file.

Literals and Variables

Similar to how we study constants and variables in math class at school, here too, the value of a constant remains the same while the value of a variable might fluctuate.

C Programming Basics for Beginners in 2023

Literals (constants)

Literals are fixed values that can be used directly in the program.

Eg:- 5, -12.5, ‘c’, etc

Literals are the constant Values assigned to the continuous variables.

Types of literals

There are many other types of literals, but we will look at three of them now.

Integer Literals

  • Numbers without a decimal part.

Eg:- 4,-6, 0, 135, etc

Floating-points Literals

  • Numbers that have a decimal point.

Eg- 4.5, 7.5, 2.0, -5.75 etc

Character Literals

  • A single character, enclosed within single Quotation marks.

Eg:-‘a’, ‘4’, ‘K’, etc.

We Can use these three literals directly in our program.

Variables

Every programming language has a concept called Variables that allows us to store data values like integers & Characters.

  • A Variable is the name of the memory Location.
  • It is used to store data.
  • Its Value Can be changed.
  • it can be reused many times in a program.

Eg:- int age = 20; Here age is Variable and 20 is its Value.

Rules for naming a Variable

  • A Variable name can only have letters, digits, and an underscore.
  • The first letter of a Variable should be either a letter or an underscore.
  • We Should Always create meaningful Variables names.

Eg: Age, _Age,Name, etc;

Types of Variables

  • Int – Stores Integers (whole numbers), without decimals such as 123 0r -123.
  • Float – Storing floating point numbers, with decimals Such as 19.95 or -19.95.
  • Char – Store’s single characters, such as ‘a’ or ‘5’, Surrounded by single Quotes.

Format Specifiers

Format specifiers are used in combination with the printf() function to notify compilers what type of data the variable is storing.

  • It is basically the placeholder for the Variable Value in the c language.
  • A format Specifier begins with a percentage sign (“%”) and is followed by a character.

int num = 15; // stores int value
float num = 1·5; // stores float value

Printf(“%d”, num); // prints num = 15
Printf(“%f”, num); // num = 1.5

Most commonly used format specifiers

Format SpecifiersType of Output
%d or %iInteger
%fFloating point
%cCharacter
%sString
%lfDouble
%LfLong Double

Data Types

Data Types are Declarations for Variables.

Data Types determine the type and size of data associated with Variables.

For Eg; Int Data Type

  • Int type can store only integers.
  • its size is typically 4 bytes.

Let us know some Basic Data Types

Int Data Type

  • the int data type can store only integer values.
  • the size of an int data type is usually 4 bytes.
  • it can store whole numbers without decimals.
  • it can take 2^32 distinct integer values from -2147483648 to 2147483647.

Eg : int age; age = 18;

Float & Double Data Type

  • Both Float and Double Data Types can store floating point numbers ( decimal numbers ).
  • the size of a float Variable is 4 bytes and the size of a double Variable is 8 bytes.
  • double Variables can store numbers with better precision.
  • float Variable is sufficient for storing 7 decimal digits, whether double is sufficient for 15 decimal digits.

Eg : double num = 45.60;
Float num = 45.60f;

Char Data Type

  • The Char data type is used for storing Character Values.
  • its size is usually 1 byte.
  • it can store single characters, letters, numbers, or ASCII Values.
  • we always have to put the character Value inside the Single Quotations.

char letter = ‘s’;

Long Data Type

  • It is used to store Large Values.
  • we can use the Long keyword before int and float(double).
  • The size of the Long int data type is usually 8 bytes.
  • The size of Long Double is at least 10 bytes.

Ex:- Long int num = 314748364;

Input and Output in c

Input and Output

  • printf – To print Output on the Screen.
  • scanf – to take input from the User.

Printf() Function

  • The printf() function sends the output to the Screen.
  • The printf() function displays the Text inside the Quotations.
  • this function is always present in the main() function.
printf("Hello Skills Ever");

  • the printf() function is a library Function.
  • for printf() to work we must include the #include< stdio.h > in our program.

scanf() function

  • In order to take input from the user and assign it to a variable, we can use the scanf function.
  • the scanf function takes input from the user.
  • scanf stands for ” Scan Formatted String”.
  • it reads data from stdin( standard input stream).
  • it accepts character, string, and numeric data from the user.
            ↱ Address of operator with variable i
scanf("%d",&i);
        ↳ Format specifier

  • & is the “Address of operator” and we always use it inside the scanf function with variable.

A Simple Program

#include < stdio.h >
int main()
{
    int a;
    printf("Enter the number a : ");
    scanf("%d", &a);

    printf("The Value of a is : %d", a);
    return 0;
}

If we enter 5 as input of a, we get 5 as output.

Comments

Comments are used in programs to clarify something about the program in plain language which is not executed.

it is a way for programmers to add notes to our program.

comments is statements that are not executed by the compiler and interpreter.

Types of Comments

There are two types of comments in c programming.

  • Single-line comment
  • multi-line comment

Example of Single line comment

//Hello this is skillsever

Example of Multi line comment

/* Hello Dear Users.
This is skillsever */

Keywords

There are some reserved keywords in c, whose meaning is already known to the compiler known as Keywords.

  • There are 32 Reserved words ( Keywords ) in C Language.
  • You can use those words as a Variable name, constant name, etc;

A List of 32 Keywords.

autodoubleintstruct
breaklongelseswitch
casereturnenumtype def
charregisterexternunion
constshortfloatunsigned
continuesignedforvoid
defaultsize ofgotovolatile
dostaticifwhile

Compilation and Execution

A Computer program that converts C Code into Machine Language code, so that the computer ( compiler ) can easily understand it.

A Program is written in plain text using combinations of instructions in a particular sequence.

C Programming Basics for Beginners in 2023

the compiler performs some basic checks and converts the c code into machine code.

Eg; Hello.c → C Compiler → Hello.exe

Library functions

c language has a lot of valuable library functions which is used to carry out certain tasks, for instance, the printf function is used to print values on the output screen.

the prototype and definitions of these functions are present in their respective header files., and to use these functions we need to include the header file in our program.

Take a look