Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

> need help with c programming

views
     
flashang
post Nov 23 2021, 11:14 PM

Getting Started
**
Junior Member
117 posts

Joined: Aug 2021


CODE

#include <stdio.h>

char x[10];

void case1() {
printf( "example 1 : \n" );
printf( "enter string x : " );
scanf( "%s", x );
printf( "string %s \n", x );
printf( "address %i \n", x );

printf( "enter string &x : " );
scanf( "%s", &x );
printf( "string %s \n", &x );
printf( "address %i \n", &x );
}

/*
example 1 :
enter string x : str1
string str1
address 4215816
enter string &x : str2
string str2
address 4215816
----------
*/


scanf() // read data and store into location. location => pointer.

* note 1 : string is array of characters.
* note 2 : scanf accept pointer, using variable name "x" or "&x" will send in pointer.

smile.gif



flashang
post Nov 23 2021, 11:46 PM

Getting Started
**
Junior Member
117 posts

Joined: Aug 2021


CODE

int n[10];

void case2a() {
printf( "example 2a : \n" );
printf( "value of n %i \n", n );
printf( "value of n[0] n[1] %d %d \n", n[0], n[1] );
printf( "enter number n : " );
scanf( "%i", n );
printf( "value of n[0] [1] %d %d \n", n[0], n[1] );
printf( "---------- \n" );

printf( "value of &n %i \n", &n );
printf( "value of &[0] &n[1] %d %d \n", &n[0], &n[1] );
printf( "enter number &n : " );
scanf( "%i", &n );
printf( "value of n[0] n[1] %d %d \n", n[0], n[1] );
printf( "========== \n" );
}

/*
example 2a :
value of n 4215808
value of n[0] n[1] 0 0
enter number n : 456456
value of n[0] [1] 456456 0
----------
value of &n 4215808
value of &[0] &n[1] 4215808 4215812
enter number &n : 789789
value of n[0] n[1] 789789 0
==========
*/



* note 3 : n is array of int (32 bits is from -2147483648 to 2147483647)
* note 4 : using "n" or "&n" will use pointer.
* note 5 : address of n[0] and n[1] different are 4, int32 use 4 bytes.

smile.gif


flashang
post Nov 23 2021, 11:51 PM

Getting Started
**
Junior Member
117 posts

Joined: Aug 2021


CODE

int num;

void case2b() {
printf( "example 2b : \n" );
printf( "value of num %i \n", num );
printf( "enter number n (skip) \n" );
//scanf( "%i", num );
printf( "value of num %d \n", num );

printf( "value of &num %i \n", &num );
printf( "enter number n : " );
scanf( "%i", &num );
printf( "value of num %d \n", num );
printf( "---------- \n" );
}

/*
example 2b :
value of num 0
enter number n (skip)
value of num 0
value of &num 4215776
enter number n : 123123
value of num 123123
----------
*/


* note 6 : int num is a 32bit variable, default value assume is 0.
* note 7 : if scanf store value to "address 0" or "any other unknown address" will cause system crash.
* note 8 : for non-array variable, use &num to tell scanf store value to "this location"

smile.gif



 

Change to:
| Lo-Fi Version
0.0182sec    0.24    6 queries    GZIP Disabled
Time is now: 29th May 2022 - 10:32 AM