0% found this document useful (0 votes)
13 views4 pages

Introduction to C Programming Basics

The document provides an introduction to writing a first program in C language. It explains the main parts of a simple C program including: including the stdio.h header file, the main function, opening and closing curly brackets, using the printf function to output text, and returning 0 at the end to indicate the program executed successfully. It also summarizes what variables are in C, how to declare and define them, and that variables are only accessible within the block they are declared in.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Introduction to C Programming Basics

The document provides an introduction to writing a first program in C language. It explains the main parts of a simple C program including: including the stdio.h header file, the main function, opening and closing curly brackets, using the printf function to output text, and returning 0 at the end to indicate the program executed successfully. It also summarizes what variables are in C, how to declare and define them, and that variables are only accessible within the block they are declared in.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EduInCS Mob: 8013241616

a place to learn computer Science ( for [Link].(CS), BCA, [Link].(CS), [Link](IT),[Link].(CS) students)
----------------------------------------------------------------------------------------------------------------------------------

Study Material: 1
C Language Introduction

1.1 Writing first program in C:


Line 1. #include <stdio.h>

Line 2. int main(void)

Line 3. {

Line 4. printf("EduInCS");

Line 5. return 0;

Line 6. }

Line 1: [#include <stdio.h>]


In a C program, all lines that start with # are processed by preprocessor which is a program
invoked by the compiler. In a very basic term, preprocessor takes a C program (xyz.c) and
produces another C program (xyz.i) called expanded source code. Compiler (Lexical Analysis)
use this expanded source code as input and generate Assembly language code. The produced
program has no lines starting with #, all such lines are processed by the preprocessor. In the
above example, preprocessor copies the preprocessed code of stdio.h to our file. The .h files are
called header files in C. These header files generally contain definition of functions. We need
stdio.h for the function printf() used in the program.

Line 2 [ int main(void) ]


There must to be starting point from where execution of compiled C program begins. In C, the
execution typically begins with first line of main(). The void written in brackets indicates that the
main doesn’t take any parameter.( main() can be written to take parameters also ).
The int written before main indicates return type of main(). The value returned by main indicates
status of program termination.

Line 3 and 6: [ { and } ]


In C language, a pair of curly brackets define a scope and mainly used in functions and control
statements like if, else, loops. All functions must start and end with curly brackets.

Line 4 [ printf(“EduInCS”); ]

EduInCS EduInCS HeadfasT


Saharpur Vidyasagar Lane Nilgung Road , Sodepur Sodepur Barasat Road
Near APC College, Kol 700110 Near GNIT college , kol 700114 Near Sodepur Station, Kol 700110
EduInCS Mob: 8013241616
a place to learn computer Science ( for [Link].(CS), BCA, [Link].(CS), [Link](IT),[Link].(CS) students)
----------------------------------------------------------------------------------------------------------------------------------
printf() is a standard library function to print something on standard output. The semiolon at the
end of printf indicates line termination. In C, semicolon is always used to indicate end of
statement.

Line 5 [ return 0; ]
The return statement returns the value from main(). The returned value may be used by operating
system to know termination status of your program. The value 0 typically means successful
termination.

1.2 Variables in C
A variable in simple terms is a storage place which has some memory allocated to it. So
basically a variable used to store some form of data. Different types of variables require different
amounts of memory and have some specific set of operations which can be applied on them.

Variable Declaration:
A typical variable declaration is of the form:

type variable_name;
or for multiple variables:
type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers and the
underscore ‘_’ character. However, the name must not start with a number.

Difference b/w variable declaration and definition


Variable declaration refers to the part where a variable is first declared or introduced before its
first use. Variable definition is the part where the variable is assigned a memory location and a
value. Most of the times, variable declaration and definition are done together
See the following C program for better clarification:

#include <stdio.h>
int main()
{
// declaration and definition of variable 'a123'
char a123 = 'a';

// This is also both declaration and definition as 'b' is allocated


// memory and assigned some garbage value.
float b;

// multiple declarations and definitions


int _c, _d45, e;

EduInCS EduInCS HeadfasT


Saharpur Vidyasagar Lane Nilgung Road , Sodepur Sodepur Barasat Road
Near APC College, Kol 700110 Near GNIT college , kol 700114 Near Sodepur Station, Kol 700110
EduInCS Mob: 8013241616
a place to learn computer Science ( for [Link].(CS), BCA, [Link].(CS), [Link](IT),[Link].(CS) students)
----------------------------------------------------------------------------------------------------------------------------------

// Let us print a variable


printf("%c \n", a123);

return 0;
}

1.3 Keywords in C
Keywords are specific reserved words in C each of which has a specific feature associated with
[Link] are a total of 32 keywords in C:
auto break case char const continue

default do double else enum extern

float for goto if int long

register return short signed sizeof static

struct switch typedef union unsigned void

volatile while

1.4 Block Scope:


A Block is a set of statements enclosed within left and right braces ({ and } respectively). Blocks
may be nested in C (a block may contain other blocks inside it). A variable declared in a block is
accessible in the block and all inner blocks of that block, but not accessible outside the block.

What if the inner block itself has one variable with the same name?
If an inner block declares a variable with the same name as the variable declared by the outer
block, then the visibility of the outer block variable ends at the pint of declaration by inner block.

int main()
{
{
int x = 10, y = 20;
{
// The outer block contains declaration of x and y, so
// following statement is valid and prints 10 and 20
printf("x = %d, y = %d\n", x, y);
{
// y is declared again, so outer block y is not accessible
// in this block
int y = 40;

EduInCS EduInCS HeadfasT


Saharpur Vidyasagar Lane Nilgung Road , Sodepur Sodepur Barasat Road
Near APC College, Kol 700110 Near GNIT college , kol 700114 Near Sodepur Station, Kol 700110
EduInCS Mob: 8013241616
a place to learn computer Science ( for [Link].(CS), BCA, [Link].(CS), [Link](IT),[Link].(CS) students)
----------------------------------------------------------------------------------------------------------------------------------
x++; // Changes the outer block variable x to 11
y++; // Changes this block's variable y to 41

printf("x = %d, y = %d\n", x, y);


}

// This statement accesses only outer block's variables


printf("x = %d, y = %d\n", x, y);
}
}
return 0;
}
Output:

x = 10, y = 20
x = 11, y = 41
x = 11, y = 20

Can variables of block be accessed in another subsequent block?


No, a variable declared in a block can only be accessed inside the block and all inner blocks of
this block. For example, following program produces compiler error.

int main()
{
{
int x = 10;
}
{
printf("%d", x); // Error: x is not accessible here
}
return 0;
}
Output:

error: 'x' undeclared

EduInCS EduInCS HeadfasT


Saharpur Vidyasagar Lane Nilgung Road , Sodepur Sodepur Barasat Road
Near APC College, Kol 700110 Near GNIT college , kol 700114 Near Sodepur Station, Kol 700110

Common questions

Powered by AI

In C, a variable declared within a block is accessible throughout the block and its nested inner blocks but not outside these blocks. If an inner block redeclares a variable with the same name as one in an outer block, the inner block's declaration overrides access to the outer block's variable. This means the visibility of the outer block's variable ends at the point of the inner block's declaration. For example, an outer block variable 'x' would be inaccessible if 'x' is redeclared in an inner block .

In C programming, variable names cannot start with a number as per the language's rules for identifier syntax. A variable name beginning with a digit leads to a compilation error because identifiers in C are required to start with a letter (uppercase or lowercase) or an underscore followed by letters, numbers, or underscores .

In C programming, a semicolon ';' is used to terminate statements. Its role is crucial as it signifies the end of an instruction to be executed. Omitting a semicolon at the end of a statement can result in syntax errors, as the compiler expects statement separation .

The preprocessor in C is responsible for processing directives before actual compilation. It takes a C program file (e.g., xyz.c) and generates an expanded source code file (e.g., xyz.i) by replacing all preprocessing directives with the required operations. These steps are managed before the compiler's lexical analysis phase. Specifically, header files such as 'stdio.h' are included by the preprocessor, which copies the precompiled code from the header file into the source code file. This is crucial because functions like 'printf()' are defined in these header files .

The 'main()' function is the entry point of every C program where execution begins. Its structure typically includes 'int main(void)' which signifies that the function returns an integer status code and takes no parameters (though it can be parameterized). The 'main()' function contains the program's primary instructions, concluding with 'return 0;', commonly used to indicate successful termination to the operating system .

The 'printf()' function in C is part of the standard I/O library used to send formatted output to the standard output stream, typically the console. It allows for the conversion and formatting of data types and integrates them into strings using specifiers such as '%d' for integers, '%f' for floats, and '%c' for characters. For example, 'printf("Value: %d", number);' would replace '%d' with the integer value of 'number' before outputting .

Header files in C, denoted by '.h' extension, contain definitions of functions and macros to be shared across multiple source files. By including a header file in various parts of a program with '#include', code becomes reusable and modular since existing functionality can be incorporated without redefining or re-implementing it. This enables separate compilation, leading to more organized and manageable code bases .

In C programming, variable declaration is the action of introducing a variable name and type before its usage, whereas definition allocates memory and assigns a value to the variable. Although declaration and definition often occur simultaneously, such as 'int a = 5;', they can occur separately. For example, declaring an 'extern int a;' in one source file allows its use without defining memory, whereas 'int a;' would also define it .

Memory allocation size affects the range and precision of data types in C. For instance, 'int', 'float', 'char', and 'double' allocate 4, 4, 1, and 8 bytes typically on a 32-bit system, respectively, dictating their use cases, such as choosing 'int' for integer arithmetic or 'double' for high-precision computations. The memory footprint also impacts operations; e.g., larger types involve more memory cycles but provide greater accuracy and computational capability. This differentiation is crucial when optimizing performance and memory usage .

In C programming, keywords such as 'if', 'else', 'switch', 'case', 'break', 'continue', 'goto', and 'return' alter the program flow. 'if' and 'else' manage conditional branching, 'switch' and 'case' facilitate multi-way branching, 'break' exits loops or switch cases prematurely, 'continue' skips the remainder of a loop's body to begin a new iteration, 'goto' transfers control to a specified label, and 'return' exits the current function returning a value if necessary .

You might also like